用while循环一直计算两个随机数的和,直到输入错误答案
import java.util.Scanner;
public class probelm2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num1, num2;
num1 = (int) (Math.random() * 10);
num2 = (int) (Math.random() * 10);
int result = num1 + num2;
System.out.print("What is " + num1 + " + " + num2 + "? ");
int answer = scanner.nextInt();
while (answer == result) {
System.out.print("What is " + ((int)(Math.random() * 10)) + " + " + ((int)(Math.random() * 10)) + "? ");
answer = scanner.nextInt();
}
System.out.println("Sorry. That is incorrect");
}
} |
免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。
版权声明:作者保留权利,不代表天盟立场。
|
|
|
|