2017-04-05 73 views
-3

我想從主要方法調用方法gasCost。我給用戶選擇輸入一個數字以產生一定的計算,在這種情況下用戶已經選擇了選項3.整數加侖_氣體在主要方法中定義,所以我將它傳遞給gasCost方法。如何用main方法中的參數調用一個方法if語句?

我在else語句後得到一個錯誤,無法編譯。說.class期望變量名gallons_Gas開始和加侖_Gas說後;預期。我做錯了什麼,我該如何解決這個問題?

public class Deryck_HW2_TripCalculatorMenu 

{

public static void main(String[] args) 
{ 
    //Get a scanner instance 
    Scanner userInput = new Scanner(System.in); 

    //Ask for gallons of gas consumed 
    System.out.print("Gallons of Gas Consumed: "); 

    //Save gallons of gas consumed 
    int gallons_Gas = userInput.nextInt(); 

    //Give user options menu 
    menu(); 

    //Ask user choice 
    System.out.print("Choose from options above (enter number 1-4): "); 

    //Get user choice 1-4 
    int choice = userInput.nextInt(); 

    //Test choice 
    if (choice == 1) 
    { 
     //avgSpeed(); 
    }  
    else if (choice == 2) 
    { 
     //mpg(); 
    }  
    else if (choice == 3) 
    { 
     gasCost(int gallons_Gas); 
    } 
    else 
    { 
     System.out.print("Error: selection invalid. Restart program and enter a number between 1 and 4."); 
    }  
} 


public static void gasCost(int gallons_Gas) 
{ 
    //Get a scanner instance 
    Scanner userInput = new Scanner(System.in); 

    //Ask cost of gallon of gas 
    System.out.print("What is the cost per gallon of gas? "); 

    //Save cost per gallon 
    double gallon_Cost = userInput.nextDouble(); 

    //Calculate total cost 
    double total_cost = (gallons_Gas * gallon_Cost); 

    System.out.print("Total cost of gas for this trip was $" + total_cost); 

} 
+0

好像你' if-statement'甚至不在方法 –

+0

if語句包含在main方法中以及其他一些東西中,只是不想發佈整個東西。 – Deryck

+0

'gas_cost'!='gasCost' – Andreas

回答

0
gasCost(int gallons_Gas); 

應該

int gallons_Gas; 
if (...) { 
    gasCost(gallons_Gas); 
} 

不能在方法調用的參數列表內聲明的int。

+0

非常感謝,完全陌生。 – Deryck

1

我不知道這是一個僞代碼還是真正的代碼。如果是真碼,有幾個錯誤:

gasCost(int gallons_Gas); 

您應該知道形式參數和實際參數之間的差異。在實際參數中不需要變量類型,而不是形式參數。鏈接:

What is a formal parameter in Java?

因此,該代碼應該是這樣的:

之後,你要聽的評論人:可以肯定的地方是else if語句可是,如果你把它用錯誤的方式,它不會工作。

希望它可以幫助

1

試圖瞭解這些行的意思,並注意如何調用方法,如何傳遞變量,在聲明變量等等

import java.util.Scanner; 
public class GasCalculator { 
    public static void main(String[] args) { 
     final int GALLONS_GAS = 100; // change this to whatever. It's good practice to use constants for variables that does not need to be immuted. 
     Scanner sc = new Scanner(System.in); 
     int user_choice = -1; 
     try { 
      user_choice = sc.nextInt(); 
     } catch (Exception e) { 
      System.out.println("Only enter integers."); 
      main(args); 
      return; 
     } 
     switch(user_choice) { 
      case 1: 
       // do something. 
       break; 
      case 2: 
       // do something. 
       break; 
      case 3: 
       gasCost(GALLONS_GAS); 
       break; 
      default: 
       System.out.println("Bad input"); 
       main(args); 
       break; 
     } 
     sc.close(); 
    } 
    public static void gasCost(int gallons_Gas) { 
    //Get a scanner instance 
    Scanner userInput = new Scanner(System.in); 

    //Ask cost of gallon of gas 
    System.out.print("What is the cost per gallon of gas? "); 

    //Save cost per gallon 

     // should try using a try-catch here to handle InputMismatchException 
    double gallon_Cost = userInput.nextDouble(); 
    //Calculate total cost 
    double total_cost = (gallons_Gas * gallon_Cost); 

    System.out.print("Total cost of gas for this trip was $" + total_cost); 
    userInput.close(); 
    return; 
    } 
} 
+0

我是否應該使用switch語句僅僅因爲它比一堆if-else語句更簡潔? – Deryck

+0

@Deryck不,你不應該僅僅因爲它的整潔就使用開關盒。你應該使用Switch case,因爲它比if-else if-else語句的列表更快。編譯器可以生成一個跳轉表,其中每個if都被測試。在你的情況下,你的個人喜好,但只是另一種向你展示如何選擇性執行代碼的方式。 – noobcoder

相關問題