2017-04-05 72 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

相关问题