import java.util.Scanner;
public class Main {
public static void main(String[] args){
method();
y();
boolean x = true;
if(x == y()){
System.out.println("You're right!");
}else{
System.out.println("You're wrong!");
main(args);
}
}
public static void method(){
System.out.println("a+b=5 If a=2 what does b equal?");
}
public static boolean y(){
try{
Scanner s = new Scanner(System.in);
String input = s.nextLine();
int input2 = Integer.parseInt(input);
int b = 3;
if(input2 == b) {
return true;
}else{
return false;
}
}
catch(Exception e) {
return false;
}
}
}
所以,我在eclipse中運行這個,我需要輸入3(正確的答案,你是對的應該被打印出來)兩次才能工作。 我想要做的是提出一個問題,並說如果錯誤的答案你錯了,但不斷讓輸入被寫入,並說如果正確的答案並在那裏停止,你是對的。java在執行命令之前需要運行兩次
a+b=5 If a=2 what does b equal? // first output
3 // my input + enter, nothing happens
3 // i enter the input again
You're right! // it only works the second time
你爲什麼要調用'y()'兩次? – rgettman