提问人:Pasquale Meo 提问时间:9/21/2022 最后编辑:Arun SudhakaranPasquale Meo 更新时间:9/21/2022 访问量:68
错误答案捕手
Wrong Answer catcher
问:
这次我在这里问你如何为这个小程序制作一个控制器。
基本上,我打乱了数组列表并使用 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);
}
}
答:
0赞
enverefe12
9/21/2022
#1
“rispostaGiusta == 1342”的平均值在数字上等于 1342。因此,您需要将其用于大于或小于 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
0赞
0xe82de
9/21/2022
#2
如果我正确理解了您的问题,请尝试如下所示。 如果没有,请发表评论。
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;
}
}
0赞
YJR
9/21/2022
#3
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);
}
}
我对您的代码进行了一些更改,因为我知道这将为您提供预期的答案。
评论