[英]Do while loops? (JAVA)
我正在做一個實質上是電影測驗的程序,從可以正常工作的文本文件中讀取電影標題/發行日期/類型/毛利率,問題是可以隨時退出以使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中的中斷,並在代碼中實現。
你可以試試這個嗎?
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');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.