与其余代码完全相同的最后一部分代码出错
Error in the last part of code that is exactly the same as the rest of the code
我在代码的最后部分遇到变量启动错误。我觉得很奇怪,因为它的格式与其余代码没有什么不同。我检查了好几次,只是没有发现问题所在。我想我需要另一双眼睛来看待这个问题。
有人有想法吗?
import java.util.Scanner;
public class Survey
{
public static void main(String[] args)
{
int surveyChoice;
String choice;
String choice2;
String choice3;
String choice4;
final int FIRST_CHOICE = 1;
final int SECOND_CHOICE = 2;
final int THIRD_CHOICE = 3;
final int FOURTH_CHOICE = 4;
final int FIFTH_CHOICE = 5;
final int SIXTH_CHOICE = 6;
final int SEVENTH_CHOICE = 7;
final int EIGHTH_CHOICE = 8;
final String FIRST_PICK = "Love";
final String SECOND_PICK = "Money";
final String THIRD_PICK = "Long Safe Life";
final String FOURTH_PICK = "Short Fun Life";
final String FIFTH_PICK = "Few Very Close and Trusted Friends";
final String SIXTH_PICK = "Internationaly Fameous";
final String SEVENTH_PICK = "Pass away comfortably while sleeping";
final String EIGHTH_PICK = "Quick Sudden Death doing what you love";
String message;
String message2;
String message3;
String message4;
Scanner input = new Scanner(System.in);
System.out.println("What would you rather have?");
System.out.println("Enter 1 for Love or 2 for Money.");
surveyChoice = input.nextInt();
if(surveyChoice == 1)
{
choice = FIRST_PICK;
message = "Love";
}
else if(surveyChoice == 2)
{
choice = SECOND_PICK;
message = "Money";
}
else
choice = "invalid";
message = "An invalid option";
System.out.println("How would you rather live?");
System.out.println("Enter 3 for Long Safe Life or 4 for Short Fun Life.");
surveyChoice = input.nextInt();
if(surveyChoice == 3)
{
choice2 = THIRD_PICK;
message2 = "Long Safe Life";
}
else if(surveyChoice == 4)
{
choice2 = FOURTH_PICK;
message2 = "Short Fun Life";
}
else
choice2 = "invalid";
message2 = "An invalid option";
System.out.println("What kind of relationships would you rather have?");
System.out.println("Enter 5 for Few Very Close and Trusted Friends or 6 for Internationaly Fameous.");
surveyChoice = input.nextInt();
if(surveyChoice == 5)
{
choice3 = FIFTH_PICK;
message3 = "Few Very Close and Trusted Friends";
}
else if(surveyChoice == 6)
{
choice3 = SIXTH_PICK;
message3 = "Internationaly Fameous";
}
else
choice3 = "invalid";
message3 = "An invalid option";
System.out.println("How would you rather die?");
System.out.println("Enter 7 for Pass away comfortably while sleeping or 8 for Quick Sudden Death doing what you love.");
surveyChoice = input.nextInt();
if(surveyChoice == 7)
{
choice4 = SEVENTH_PICK;
message4 = "Pass away comfortably while sleeping";
}
else if(surveyChoice == 8)
{
choice4 = EIGHTH_PICK;
message4 = "Quick Sudden Death doing what you love";
}
else
choice = "invalid";
message = "An invalid option";
System.out.println("You want " + message);
System.out.println("You want " + message2);
System.out.println("You want " + message3);
System.out.println("You want " + message4);
System.out.println("You will have " + choice);
System.out.println("You will have " + choice2);
System.out.println("You will have " + choice3);
System.out.println("You will have " + choice4);
}
}
正如@OpiesDad 所指出的,您在最后一个块中的变量名称不匹配。
此外,我首先想到的是每个 if-then-else 块中最后一个 else 块周围缺少括号,因此适当的消息变量将始终设置为 "An invalid option"。
最后一个 else 应该是这样的:
else
{
choice = "invalid";
message = "An invalid option";
}
我在代码的最后部分遇到变量启动错误。我觉得很奇怪,因为它的格式与其余代码没有什么不同。我检查了好几次,只是没有发现问题所在。我想我需要另一双眼睛来看待这个问题。
有人有想法吗?
import java.util.Scanner;
public class Survey
{
public static void main(String[] args)
{
int surveyChoice;
String choice;
String choice2;
String choice3;
String choice4;
final int FIRST_CHOICE = 1;
final int SECOND_CHOICE = 2;
final int THIRD_CHOICE = 3;
final int FOURTH_CHOICE = 4;
final int FIFTH_CHOICE = 5;
final int SIXTH_CHOICE = 6;
final int SEVENTH_CHOICE = 7;
final int EIGHTH_CHOICE = 8;
final String FIRST_PICK = "Love";
final String SECOND_PICK = "Money";
final String THIRD_PICK = "Long Safe Life";
final String FOURTH_PICK = "Short Fun Life";
final String FIFTH_PICK = "Few Very Close and Trusted Friends";
final String SIXTH_PICK = "Internationaly Fameous";
final String SEVENTH_PICK = "Pass away comfortably while sleeping";
final String EIGHTH_PICK = "Quick Sudden Death doing what you love";
String message;
String message2;
String message3;
String message4;
Scanner input = new Scanner(System.in);
System.out.println("What would you rather have?");
System.out.println("Enter 1 for Love or 2 for Money.");
surveyChoice = input.nextInt();
if(surveyChoice == 1)
{
choice = FIRST_PICK;
message = "Love";
}
else if(surveyChoice == 2)
{
choice = SECOND_PICK;
message = "Money";
}
else
choice = "invalid";
message = "An invalid option";
System.out.println("How would you rather live?");
System.out.println("Enter 3 for Long Safe Life or 4 for Short Fun Life.");
surveyChoice = input.nextInt();
if(surveyChoice == 3)
{
choice2 = THIRD_PICK;
message2 = "Long Safe Life";
}
else if(surveyChoice == 4)
{
choice2 = FOURTH_PICK;
message2 = "Short Fun Life";
}
else
choice2 = "invalid";
message2 = "An invalid option";
System.out.println("What kind of relationships would you rather have?");
System.out.println("Enter 5 for Few Very Close and Trusted Friends or 6 for Internationaly Fameous.");
surveyChoice = input.nextInt();
if(surveyChoice == 5)
{
choice3 = FIFTH_PICK;
message3 = "Few Very Close and Trusted Friends";
}
else if(surveyChoice == 6)
{
choice3 = SIXTH_PICK;
message3 = "Internationaly Fameous";
}
else
choice3 = "invalid";
message3 = "An invalid option";
System.out.println("How would you rather die?");
System.out.println("Enter 7 for Pass away comfortably while sleeping or 8 for Quick Sudden Death doing what you love.");
surveyChoice = input.nextInt();
if(surveyChoice == 7)
{
choice4 = SEVENTH_PICK;
message4 = "Pass away comfortably while sleeping";
}
else if(surveyChoice == 8)
{
choice4 = EIGHTH_PICK;
message4 = "Quick Sudden Death doing what you love";
}
else
choice = "invalid";
message = "An invalid option";
System.out.println("You want " + message);
System.out.println("You want " + message2);
System.out.println("You want " + message3);
System.out.println("You want " + message4);
System.out.println("You will have " + choice);
System.out.println("You will have " + choice2);
System.out.println("You will have " + choice3);
System.out.println("You will have " + choice4);
}
}
正如@OpiesDad 所指出的,您在最后一个块中的变量名称不匹配。
此外,我首先想到的是每个 if-then-else 块中最后一个 else 块周围缺少括号,因此适当的消息变量将始终设置为 "An invalid option"。
最后一个 else 应该是这样的:
else
{
choice = "invalid";
message = "An invalid option";
}