2015-10-04 295 views
0

我正在做一個本質上是電影測驗的程序,從我正常工作的文本文件中讀取電影標題/發佈日期/流派/總體amt,問題是讓這個while while循環正常工作通過隨時退出用戶在兩次提示時(每個問題後,最後)都會輸入「否」 任何建議/提示,非常感謝!while while循環? (JAVA)

這是我有:

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(); 
} 

回答

0

乘坐break !!!!

您需要了解Java中的break並在代碼中實現它。

+0

不要誤會我的意思,我完全知道break語句,但對於當前逃避我的原因,我的教授告誡我們不要使用一般的break語句,並試圖找到其他的替代品。無論如何,我已經得到它的工作了,謝謝你們所有人 – cbsack

0

你可以試試嗎?

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

    } 
+0

請問你可以向過路人解釋你的代碼和原始代碼之間的差異?一眼看去,我只能看到你已經拿出了最後幾句話。 –

+0

我實際上把最後幾條語句放在while循環裏面(接近尾聲),並在用戶決定停止執行時添加一箇中斷。這些陳述僅在用戶首先提出問題後要求離開遊戲時才執行。 –

+0

試試這個: 掃描儀k =新的掃描儀(System.in); String u; System.out.println(「ready?」); u = k.next(); (int count = 0; count <= 5; count ++){ System.out.println(「gess number」); k.nextInt(); System.out.println(「Continue?」); u = k.next(); (u.charAt(0)=='n'){ System.out.println(「AGAIN?yes/no?」); u = k.next(); if(u.charAt(0)=='n') break; } } } while(u.charAt(0)=='y'); } –