我只是一个初学者,任何事情都会有所帮助。所以基本上这个程序问一个问题,并根据输入,影响T恤的价格。此外,它还要求购买多少T恤也会影响价格。它一直运行,直到输入“退出”。
我遇到的问题是我希望输出具有之前从if / else if语句累积的总销售额和其他信息。但是当我编译它时,局部变量可能尚未初始化。任何帮助将不胜感激
import java.util.Scanner;
class ScannerTest{
public static void main (String args[]){
int b, c;
String endProgram = "exit";
String userInput;
java.util.Scanner input = new java.util.Scanner(System.in);
do{
System.out.println("\n Welcome to the SAC t-shirt sales software");
System.out.println("\n Does the student have an activity fee? Yes/No/Exit");
userInput = input.nextLine();
if (userInput.equalsIgnoreCase("yes")){
System.out.println("How many Tshirts do you want to buy?");
b = input.nextInt();
input.nextLine();
System.out.println("Cost:" + b*5 + " dollars");
}
else if(userInput.equalsIgnoreCase("no")){
System.out.println ("How many Tshirts do you want to buy?");
c = input.nextInt();
input.nextLine();
System.out.println("Cost:" + c*6 + " dollars");
}
}while (!userInput.equalsIgnoreCase(endProgram));
System.out.println ("Activity Card Sales:" + b + "Non-activity Card Sales:" + c + "Total Money Collected" + ((b*5)+(c*6)));
input.close();
}
}
答案 0 :(得分:0)
只需在乞讨b和c设置为0,你就没有问题。