Java 你想做什么?(爪哇)

Java 你想做什么?(爪哇),java,loops,do-while,Java,Loops,Do While,我正在做一个基本上是电影测试的程序,从一个文本文件中读取电影标题/发行日期/类型/总金额,我很好地工作了,问题是让这个do-while循环在任何时候都正常工作,用户在两次提示时都输入no(在每个问题之后,最后) 非常感谢您的任何建议/提示 以下是我所拥有的: private static void movieQuiz (Movies[] movieRecord){ int score = 0; Scanner keyboard = new Scanner (System.in

我正在做一个基本上是电影测试的程序,从一个文本文件中读取电影标题/发行日期/类型/总金额,我很好地工作了,问题是让这个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();
}
private static void moviequick(Movies[]movieRecord){
智力得分=0;
扫描仪键盘=新扫描仪(System.in);
字符串用户选择;
System.out.println(“您已选择电影测验!说明”
+“如下所示:程序将输出\n其中一个的标题”
+“有史以来票房最高的电影,你的工作是”
+“猜测发布日期\n将添加一个点”
+对于每个正确答案的分数,您的总分数将
+“在结束时展示,或直到你想放弃为止。享受乐趣并”
+“祝你好运!”;
int检验=0;
下载电影(movieRecord);
System.out.println(“准备好开始了吗?”);
userChoice=keyboard.next();
//如果他们想再次运行,请执行此操作
做{
对于(int count=0;count拿一个

您需要学习Java中断并在代码中实现它。

您能试试这个吗

     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');

    }
int评分=0;
扫描仪键盘=新扫描仪(System.in);
字符串用户选择;
System.out.println(“您已选择电影测验!说明”
+“如下所示:程序将输出\n其中一个的标题”
+“有史以来票房最高的电影,你的工作是”
+“猜测发布日期\n将添加一个点”
+对于每个正确答案的分数,您的总分数将
+“在结束时展示,或直到你想放弃为止。享受乐趣并”
+“祝你好运!”;
int检验=0;
下载电影(movieRecord);
System.out.println(“准备好开始了吗?”);
userChoice=keyboard.next();
//如果他们想再次运行,请执行此操作
做{

对于(int count=0;count)您能向过路人解释一下您的代码与原始代码之间的区别吗?一看,我只能看到您删除了最后几条语句。我实际上将最后几条语句放在了while循环中(接近末尾)并在用户决定明确停止执行时添加了一个中断。这些语句仅在用户在开始提问后要求离开游戏时执行。请在main中尝试:Scanner k=new Scanner(System.in);String u;System.out.println(“ready?”);u=k.next();do{for(int count=0;count D.Urvoy,非常感谢您的帮助,它现在肯定能工作了..看起来break语句才是真正需要的。别误会,我完全知道break语句,但出于我目前无法理解的原因,我的教授警告我们一般不要使用break语句,并尝试找到其他选择。不管怎样,我成功了。谢谢大家