123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- package com.lovecoding.J20250511;
- import java.util.Random;
- import java.util.Scanner;
- /**
- * @author WanJl
- * @version 1.0
- * @title Demo02
- * @description 100以内加减法:
- * 随机生成两个整数。输出到控制台,格式如下:
- * 53+75=?
- * 控制台中输入结果:
- * 128
- * 恭喜,回答正确。请听下一题:
- * xx+yy=?
- * zz
- * 如果回答错误,则提示:回答错误,请听下一题:
- * 直到输入:exit。则提示:
- * 结束加法练习,....程序结束
- *
- *
- * @create 2025/5/11
- */
- public class Demo02 {
- public static void main(String[] args) {
- Random ran=new Random();
- Scanner sc=new Scanner(System.in);
- while (true){
- int a=ran.nextInt(100)+1;
- int b=ran.nextInt(100)+1;
- System.out.println("请计算"+a+"+"+b+"=?");
- String inStr=sc.nextLine();
- if (inStr.equals("exit"))
- break;
- int result=Integer.parseInt(inStr); //Integer.parseInt(字符串),把字符串类型的整数转换成int类型的整数
- if (result==(a+b))
- System.out.println("回答正确,"+a+"+"+b+"="+result+",请听下一题");
- else
- System.out.println("回答错误,"+a+"+"+b+"!="+result+",请听下一题");
- }
- System.out.println("结束答题...");
- }
- }
|