我想从主要方法调用方法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