【问题标题】:Do while loops? (JAVA)做while循环吗? (JAVA)
【发布时间】:2015-10-04 05:13:36
【问题描述】:

我正在做一个本质上是电影测验的程序,从我工作正常的文本文件中读取电影标题/发行日期/流派/总金额,问题是通过退出使这个 do while 循环正常工作在任何时候,用户在两次出现提示时都输入 no(在每个问题之后,最后) 非常感谢任何建议/提示!

这是我所拥有的:

private static void movieQuiz (Movies[] movieRecord){

    int score = 0; 
    Scanner keyboard = new Scanner (System.in);
    String userChoice;

    System.out.println("You have selected the Movie Quiz! The instructions"
            + " are as follows: The program will output \nthe title of one of "
            + "the highest grossing films of all time, your job is "
            + "to guess which year \nit was released. A point will be added "
            + "to your score for each correct answer, and your total \npoints will"
            + " display at the end or until you want to give up. Have fun and"
            + " good luck!\n");
    int test = 0;
    loadMovies(movieRecord);
    System.out.println("Are you ready to begin?");
    userChoice = keyboard.next();

    //do this if they want to run it again
    do {
      for (int count = 0; count <= movieRecord.length-1; count++) {
        System.out.println("Movie #" + (test+1) + ": " + movieRecord[count].movieTitle);
        System.out.println("Guess the year");
        int guess = keyboard.nextInt();

          if (guess == movieRecord[count].releaseYear){
            System.out.println("DING DING DING! (+1) \n" );
            score++;
          }

          else {
            System.out.println("Wrong (+0) \n");
          }

          System.out.println("Continue? (Currently on #" + (1 + test) + " out of "
          + movieRecord.length + ")");
          userChoice = keyboard.next();

          test++;

      }
    }   while (userChoice.charAt(0) == 'y');

      //display if they type 'no' at any time, ending the program
      System.out.println("Total Score is: " + score + " out of " + movieRecord.length);
        System.out.println("AGAIN? yes/no?");
        userChoice = keyboard.next();
}

【问题讨论】:

    标签: java loops do-while


    【解决方案1】:

    参加break!!!!

    您需要了解 Java 中的 break 并在您的代码中实现它。

    【讨论】:

    • 不要误会我的意思,我完全了解 break 语句,但出于目前我无法理解的原因,我的教授警告我们一般不要使用 break 语句并尝试寻找其他替代方案。无论如何,我得到了它的工作谢谢大家
    【解决方案2】:

    你能试试这个吗?

         int score = 0; 
            Scanner keyboard = new Scanner (System.in);
            String userChoice;
    
            System.out.println("You have selected the Movie Quiz! The instructions"
                    + " are as follows: The program will output \nthe title of one of "
                    + "the highest grossing films of all time, your job is "
                    + "to guess which year \nit was released. A point will be added "
                    + "to your score for each correct answer, and your total \npoints will"
                    + " display at the end or until you want to give up. Have fun and"
                    + " good luck!\n");
            int test = 0;
            loadMovies(movieRecord);
            System.out.println("Are you ready to begin?");
            userChoice = keyboard.next();
    
            //do this if they want to run it again
            do {
              for (int count = 0; count <= movieRecord.length-1; count++) {
                System.out.println("Movie #" + (test+1) + ": " + movieRecord[count].movieTitle);
                System.out.println("Guess the year");
                int guess = keyboard.nextInt();
    
                  if (guess == movieRecord[count].releaseYear){
                    System.out.println("DING DING DING! (+1) \n" );
                    score++;
                  }
    
                  else {
                    System.out.println("Wrong (+0) \n");
                  }
    
                  System.out.println("Continue? (Currently on #" + (1 + test) + " out of "
                  + movieRecord.length + ")");
                  userChoice = keyboard.next();
    
                  if(userChoice.charAt(0) == 'n'){
                  //display if they type 'no' at any time, ending the program
                  System.out.println("Total Score is: " + score + " out of " + movieRecord.length);
                    System.out.println("AGAIN? yes/no?");
                    userChoice = keyboard.next();
                    if (userChoice.charAt(0) == 'n')
                        break;
                  }
    
                  test++;
    
              }
            }   while (userChoice.charAt(0) == 'y');
    
        }
    

    【讨论】:

    • 您能否向路人解释一下您的代码与原始代码之间的区别是什么?一眼看去,我只看到你把最后几句话拿出来了。
    • 我实际上将最后几条语句放在了 while 循环内(接近尾声),如果用户决定明确停止执行,则添加一个中断。这些语句只有在用户最初提出问题后要求离开游戏时才会执行。
    • 在 main 中试试这个:Scanner k = new Scanner (System.in);字符串你; System.out.println("准备好了吗?"); u = k.next(); do { for (int count = 0; count
    • D. Urvoy,非常感谢您的帮助,它现在确实有效.. 看起来确实需要一个 break 语句。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    相关资源
    最近更新 更多