0

抱歉,如果这是一个愚蠢的问题,但我对此完全陌生,只是尝试一下。我的代码卡住了....此后,程序结束,无论选择 1 还是 2。我知道我缺少一些简单的东西....任何输入表示赞赏。我已经复制并粘贴了我认为问题所在的部分。

    System.out.println("Is this information correct? Enter 1 if it is correct, and 2 to change");
    Scanner inputCorrect = new Scanner(System.in);
    int pick = inputCorrect.nextInt();
    boolean isCorrect = false;

    while (isCorrect = false){

          while (!(pick == 1) && (!(pick ==2)))
              System.out.println("That is not a valid entry please try again");

              if (pick == 1){

                  isCorrect = true;
              }

              if (pick == 2){
                  System.out.println("Enter 1 to change your name, 2 to change your age or 3 to change your gender");
                  Scanner inputChange = new Scanner(System.in);
                  int change = inputChange.nextInt();

                    if (change ==1){
                        Scanner inputNewName = new Scanner(System.in);
                        System.out.println("Enter the correct name: ");
                        String correctedName = inputNewName.next();
                        you.setName(correctedName);
                        System.out.println(you);
                        isCorrect = true;
                    }

                    if (change ==2) {
                        Scanner inputNewAge = new Scanner(System.in);
                        System.out.println("Enter the correct age: ");
                        int correctedAge = inputNewAge.nextInt();
                        you.setAge(correctedAge);
                        System.out.println(you);
                        isCorrect = true;

                    }

                    if (change == 3) {
                        Scanner inputNewGender = new Scanner (System.in);
                        System.out.println("Enter the correct gender: ");
                        char correctedGender = inputNewGender.next().charAt(0);
                        you.setGender(correctedGender);
                        System.out.println(you);
                        isCorrect = true;

                    }   
          }     
      } 
4

3 回答 3

10

在你的 while 循环中

while (isCorrect = false){

您正在使用赋值运算符,=所以它总是错误的。

您希望比较运算符==比较值是否相同。

while (isCorrect == false){

由于它已经是boolean,您可能只想单独使用isCorrect

while (!isCorrect)
于 2013-05-14T18:40:47.110 回答
3

您在 while 语句表达式中使用了赋值运算符,该运算符将始终计算为false. 使用==运算符来比较boolean

while (isCorrect == false){

或更好

while (!isCorrect){
于 2013-05-14T18:40:53.800 回答
2

您可以通过以下方式帮助自己:

 while( false == isCorrect )

如果您输入错误的相等运算符,这将引发编译错误。

于 2013-05-14T18:43:12.370 回答