我正在编写一个程序,主要询问用户是否
这就是我想出来的
我收到错误,说“我找不到符号”(加仑/等级/成员)
我该如何解决?
这是我的错误消息的屏幕截图 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 );
}
}
答案 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();
........}