- /*
- 4.从键盘接收两个整数,随机得到一个 + - * /符号中的一个求这两个数的结果。
- char[] ch={'+','-','*','/'};
- */
- import java.util.*;
- public class RandomDemo {
- public static void main(String[] args) {
- System.out.println("请从键盘输入两个整数:");
- Scanner sc = new Scanner(System.in);
-
- //从键盘输入两个整数
- int a = sc.nextInt();
- int b = sc.nextInt();
- //随即获取一个符号
- char[] ch={'+','-','*','/'};
- Random rd=new Random();
- char x = ch[rd.nextInt(ch.length)];
- //判断随即得到的运算符与那个匹配,并运算,输出结果
- switch (x) {
- case '+':
- System.out.println(a + "+" + b + "=" + (a+b)); break;
- case '-':
- System.out.println(a + "-" + b + "=" + (a-b)); break;
- case '*':
- System.out.println(a + "x" + b + "=" + (a*b)); break;
- case '/':
- System.out.println(a + "/" + b + "=" + (a/b)); break;
-
- }
- }
- }
复制代码 |