ITPub博客

首页 > IT职业 > > 用while循环一直计算两个随机数的和,直到输入错误答案

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

作者:dapan 时间:2019-04-12 17:42:15 0 删除 编辑

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");

  }

}


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/758/viewspace-3161681/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2020-08-07

  • 博文量
    4504
  • 访问量
    87236