如何使用main方法if参数调用方法?

时间:2017-04-05 20:56:57

标签: java methods parameters

我试图从main方法调用gasCost方法。我给用户选择输入一个数字来调出某个计算,在这种情况下用户选择了选项3.整数gallons_Gas在main方法中定义,所以我将它传递给gasCost方法。

我在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);

}

3 个答案:

答案 0 :(得分:1)

我不知道这是伪代码还是真正的代码。如果它是真正的代码,那么有几个错误:

gasCost(int gallons_Gas);

您应该知道形式参数和实际参数之间的差异。在实际参数中,不需要变量类型,而不是形式参数。链接:

What is a formal parameter in Java?

所以,代码应该是:

 int gallons_gas = 5; //Just for example

 gasCost(gallons_Gas);

在那之后,你应该在评论中听听这些人:确定if语句在哪里,如果你以错误的方式说它就不会工作。

希望有所帮助

答案 1 :(得分: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;
    }
}

答案 2 :(得分:0)

gasCost(int gallons_Gas);

应该是

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

您不能在方法调用的参数列表中声明一个int。