Java程序 - 加油站泵

时间:2014-05-05 11:47:18

标签: java

我正在编写一个程序,主要询问用户是否

  1. 他们是会员(是/否),如果是,则可享受10%折扣
  2. 选择天然气等级(3个等级3个价格) 然后返回包含小计和税+总计
  3. 的发票

    这就是我想出来的

    我收到错误,说“我找不到符号”(加仑/等级/成员)

    我该如何解决?

    这是我的错误消息的屏幕截图 http://puu.sh/8A60K/f9201eb57e.jpg

    import java.util.Scanner;
    
    
    public class Kang_Invoice2 {
    
        public static void main(String[] args) {
    
            Scanner input = new Scanner(System.in);
            System.out.println("Are you a member?");
            memberYN = input.nextLine();
            System.out.println("Quality of Gas:");
            grade = input.nextInt();
            System.out.println("Gallons sold:");
            gallon = input.nextDouble();
    
            double subtotal;
            double countyTaxRate = 0.07;
            double cityTaxRate = 0.0375;
            double price87 = 3.87;
            double price89 = 3.98;
            double price91 = 4.01;
            double total = subtotal * 1.1075;
            double countyTax = subtotal * countyTaxRate;
            double cityTax = subtotal * cityTaxRate;
            double gallon;
            int grade ;
    
            if(memberYN.equalsIgnoreCase("yes"))
            {
                if (grade == 87)
                {subtotal = price87 * gallon * 0.9;};
                if (grade == 89);
                {subtotal = price89 * gallon * 0.9;};
                if (grade == 91);
                {subtotal = price91 * gallon * 0.9;};
            }
            elseif(memberYN.equalsIgnoreCase("no"));
            {   
                if (grade == 87)
                {
                    subtotal = price87 * gallon;
                };
                if  (grade == 89);
                {
                    subtotal = price89 * gallon ;
                };
                if (grade == 91);
                {
                    subtotal = price91 * gallon;
                };
            }
    
            System.out.println("INVOICE FOR GASOLINE");
            System.out.println("Member status" + ( memberYN.equalsIgnoreCase("yes") ? "yes" : "no")
            );
            System.out.println("Gasoline Sold/Price:" + gallon + "@" );
            System.out.println("\n");
            System.out.println("Subtotal : $" + subtotal);
            System.out.println("County Tax : $" + countyTax);
            System.out.println("City Tax : $" + cityTax);
            System.out.println("-------------");
            System.out.println("Total : $ " + total );
    
        }
    }
    

5 个答案:

答案 0 :(得分:1)

在声明之前,不能使用局部变量:

gallon = input.nextDouble();
...
double gallon;

避免这些问题的最佳方法是在第一次使用变量时声明变量:

double gallon = input.nextDouble();

答案 1 :(得分:0)

变量需要在使用之前声明,例如

String memberYN = input.nextLine();
 ^

答案 2 :(得分:0)

您需要定义变量。这是通过在您第一次引用它们时指定类型(有或没有初始化它们)来完成的。例如,gallon

System.out.println("Gallons sold:");
Double gallon = input.nextDouble();
// ^^^ Notice the type specification

答案 3 :(得分:0)

您忘记声明变量。

int memberYN,grade;

double gallon;

答案 4 :(得分:0)

您需要先声明变量才能获得输入

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    double gallon;
    int grade ;
    String memberYN;
    System.out.println("Are you a member?");
    memberYN = input.nextLine();
    System.out.println("Quality of Gas:");
    grade = input.nextInt();
    System.out.println("Gallons sold:");
    gallon = input.nextDouble();
    ........}