[英]Wrong Answer catcher
我這次來問你如何為這個小程序制作一個controller。
基本上,我打亂數組列表並使用 sysout,而不是要求將其按順序排列,並且我使用帶有 if 的 do-while 來捕捉答案。
我現在想做的是捕捉是否存在兩個錯誤答案中的任何一個。
就像,如果數字 5 或 6 出現,則表明存在錯誤。 但我不知道該怎么做。
這里的代碼:
public class Test {
public static void main(String[] args) {
java.util.Random randomGenerator = new java.util.Random();
Scanner scan = new Scanner(System.in);
List<String> coffeeList = Arrays.asList("1 pick it","2 drink it","3 cook","4 put it in a cup","5 salt it","6 dream it");
Collections.shuffle(coffeeList);
for(String value : coffeeList)
System.out.println(value);
System.out.println("\n" + "ora metti in ordine la lista indicando i numeri");
int rispostaGiusta = 0;
boolean flag = false;
rispostaGiusta = scan.nextInt();
do {
if(rispostaGiusta == 1342) {
System.out.println("\n" + "la risposta è giusta");
flag = true;
}
else {
System.out.println("\n" + "la rispsota non è giusta ritenta");
rispostaGiusta = scan.nextInt();
}
} while(!flag);
}
}
“rispostaGiusta == 1342”的平均值在數值上 rispostaGiusta 等於一千三百四十二。 所以你需要用它來檢查大於或小於 4 和 1 的數字:
if(rispostaGiusta > 4 || rispostaGiusta < 1) {
System.out.println("\n" + "la rispsota non è giusta ritenta");
rispostaGiusta = scan.nextInt();
}
else {
System.out.println("\n" + "la risposta è giusta");
flag = true;
}
上面的代碼塊是檢查“4和1之間的數字”(1 < rispostaGiusta < 4)
或者更確切地說,當您使用它時,僅用於控制某些 4 個數字:
if(rispostaGiusta == 1 || rispostaGiusta == 2 || rispostaGiusta == 3 || rispostaGiusta == 4) {
System.out.println("\n" + "la risposta è giusta");
flag = true;
}
else {
System.out.println("\n" + "la rispsota non è giusta ritenta");
rispostaGiusta = scan.nextInt();
}
在此塊中,檢查掃描的數字是 1 或 2 或 3 或 4
如果我正確理解您的問題,請嘗試如下。 如果沒有,請發表評論。
public class Test {
public static void main(String[] args) {
// ...
rispostaGiusta = scan.nextInt();
do {
if (invalid(rispostaGiusta) {
throw new IllealArgumentException();
} else if (correct(rispostaGiusta)) {
System.out.println("\n" + "la risposta è giusta");
flag = true;
} else {
System.out.println("\n" + "la rispsota non è giusta ritenta");
rispostaGiusta = scan.nextInt();
}
} while(!flag);
}
private static boolean correct(int input) {
return input == 1342;
}
private static boolean invalid(int input) {
while (input > 0) {
int number = input % 10;
if (number == 5 || number == 6) {
return true;
}
input /= 10;
}
return false;
}
}
public class Test {
public static void main(String[] args) {
String rightOrde = '1342'
java.util.Random randomGenerator = new java.util.Random();
Scanner scan = new Scanner(System.in);
List<String> coffeeList = Arrays.asList("1 pick it","2 drink it","3 cook","4 put it in a cup","5 salt it","6 dream it");
Collections.shuffle(coffeeList);
for(String value : coffeeList)
System.out.println(value);
System.out.println("\n" + "ora metti in ordine la lista indicando i numeri");
int rispostaGiusta = 0;
int i =0;
boolean flag = false;
rispostaGiusta = scan.nextInt();
do {
if(rispostaGiusta.toString() == rightOrder[i]) {
System.out.println("\n" + "la risposta è giusta");
flag = true;
}
else {
System.out.println("\n" + "la rispsota non è giusta ritenta");
if(rightOrder.length()==i-1){
flag=true;
}
else{
rispostaGiusta = scan.nextInt();
}
}
i++;
} while(!flag);
}
}
據我了解,我對您的代碼進行了一些更改,這將為您提供預期的答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.