variables - 代码的最后一部分中的错误与其余代码完全相同

标签 variables if-statement compiler-errors survey

我在代码的最后部分遇到了变量初始化错误。我觉得很奇怪,因为它的格式与其余代码没有什么不同。我已对其进行了数次审核,但没有发现问题。我想我对此需要另一组眼光。

有任何想法吗?

 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块周围都缺少方括号,因此相应的message变量将始终设置为“无效选项”。

最后一个应该看起来像这样:

else 
{
    choice = "invalid";
    message = "An invalid option";
}

关于variables - 代码的最后一部分中的错误与其余代码完全相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39027459/

相关文章:

c++:error C2440: '<function-style-cast>': 无法从 'A<TYPE>' 转换为 'B<TYPE>'

linux - glibc 交叉编译配置错误

c# - Task.Factory.StartNew() 重载

javascript - 如何确保一个变量是一个对象

Javascript、jquery - 使用变量名调用数组位置

if-statement - 分离 G1 ; G2 vs. If-then-else Cond -> G1 ; G2

javascript - 如果使用 js 在 html 中选中复选框,则发出警报()

java - 生成方法中无法识别变量

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

java - 错误: incompatible types:byte cannot be converted to Boolean (illegal start of expression)