首页 > 代码库 > PairWork -四则运算

PairWork -四则运算

产生随机数,进行简单的四则运算。
组内成员:于淼 刘芳芳
package nenu.softWareProject;import java.util.Random;import java.util.Scanner;public class PairWork1_calculator { public static void main(String[] args) { int n=10; int max=100; int min=1; Random random = new Random(); for(int o=1;o<=n;o++){ //产生小于等于30的随机数 int a = random.nextInt(max)%(max-min+1) + min; int b = random.nextInt(max)%(max-min+1) + min; //产生1 2 3 4随机数分别代表加减乘除 Random sig = new Random(); int s = sig.nextInt(4); //加法 if(s==1){ int ansJia=a+b; System.out.println(a +"+" +b+"="); Scanner scan1=new Scanner(System.in); int ans =scan1.nextInt(); if(ans==ansJia){ System.out.println("真棒!你做对了"); }else{ System.out.println("在仔细想想,你可以做对的!"); } } //减 if(s==2){ if(a>b){ int ansJian=a-b; System.out.println(a +"-" +b+"="); Scanner scan2=new Scanner(System.in); int ans =scan2.nextInt(); if(ans==ansJian){ System.out.println("真棒!你做对了"); }else{ System.out.println("在仔细想想,你可以做对的!"); } }else{ int ansJian=b-a; System.out.println(b +"-" +a+"="); Scanner scan2=new Scanner(System.in); int ans =scan2.nextInt(); if(ans==ansJian){ System.out.println("真棒!你做对了"); }else{ System.out.println("在仔细想想,你可以做对的!"); } } } //乘 if(s==3){ int ansCheng=a*b; System.out.println(a +"*" +b+"="); Scanner scan3=new Scanner(System.in); int ans =scan3.nextInt(); if(ans==ansCheng){ System.out.println("真棒!你做对了"); }else{ System.out.println("在仔细想想,你可以做对的!"); } } //除 if(s==4){ double ansChu=(double)(Math.round(a/b)/100.0); System.out.println(a +"*" +b+"="); Scanner scan4=new Scanner(System.in); int ans =scan4.nextInt(); if(ans==ansChu){ System.out.println("真棒!你做对了"); }else{ System.out.println("在仔细想想,你可以做对的!"); } } } } }

运行结果为:
技术分享

 

 

PairWork -四则运算