为了账号安全,请及时绑定邮箱和手机立即绑定

用while循环一直计算两个随机数的和,直到输入错误答案

用while循环一直计算两个随机数的和,直到输入错误答案

慕仙95086 2018-09-19 22:15:02
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");  }}
查看完整描述

3 回答

  • 3 回答
  • 0 关注
  • 916 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信