Java新手在這裏, 我試圖編寫一個oracle機器,它的工作。但是,詢問用戶是否要繼續的最後部分不起作用。我會詢問用戶是否要繼續,如果他們沒有回覆「Y」,「Y」,「N」或「n」,他們將不得不再次重新運行測試。 讓我得到這個問題是這樣的:我在'Do'循環中做了什麼錯誤?
What is the question that you want to ask?... agafds
No! And Stop asking me!
Continue asking?(Y/N)...Y
Continue asking?(Y/N)...Y
Continue asking?(Y/N)...Y
Continue asking?(Y/N)...Y
Continue asking?(Y/N)...Y
從這:
import java.util.Scanner;
import java.util.Random;
import static java.lang.System.out;
public class firstClass {
public static void main(String args[]) {
Scanner kbScanner=new Scanner(System.in);
Random intRand=new Random();
String yOn;
do {
int oneten;
out.print("What is the question that you want to ask?... ");
kbScanner.nextLine();
oneten=intRand.nextInt(10)+1;
switch (oneten) {
case 1 :
out.println("Not a chance, buster.");
break;
case 2 :
out.println("Get a life man!");
break;
case 3 :
out.println("Why are you even asking me this?!");
break;
case 4 :
out.println("Depends on your thinking.");
break;
case 5 :
out.println("What are you seeking for?");
break;
case 6 :
out.println("Maybe... But don't trust me...");
break;
case 7 :
out.println("Do I look like I care?");
break;
case 8 :
out.println("Why would you even ask that?");
break;
case 9 :
out.println("The chances are as thin as searching for a needle in a haystack.");
break;
case 10 :
out.println("No! And Stop asking me!");
break;
}
do{
out.print("Continue asking?(Y/N)...");
yOn=kbScanner.nextLine();
}while (!yOn.equals("Y")||!yOn.equals("y")||!yOn.equals("N")||!yOn.equals("n"));
out.println("Continuing");
}while (yOn.equals("Y")||yOn.equals("y"));
}
}
所以說:問題是,不管用戶輸入那裏似乎沒有什麼區別。
擺脫內部的do-while循環。 – aquaraga
嘗試使用無限循環。 'while(true)' – kurumi
檢查輸入'Y'時'yOn' *真* *的值。它可能會附加cr + lf。 –