我想從主要方法調用方法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);
}
好像你' if-statement'甚至不在方法 –
if語句包含在main方法中以及其他一些東西中,只是不想發佈整個東西。 – Deryck
'gas_cost'!='gasCost' – Andreas