标签:
package NM; import java.util.*; public class SurF { public static void main (String[] agrs){ Scanner sc=new Scanner(System.in); Random r=new Random(); int op= 0; int n = 0; double a,b,c,d; String[] s={"+","-","*","/"}; while(true){ System.out.println("选择功能:\n0.退出1.简单的加减乘除运算 2.含分数运算"); op= sc.nextInt(); if (0 == op) {break;} if (1 == op) { int count1=0,count2=0; System.out.println("请输入题目个数(5分/题):"); int num=sc.nextInt(); System.out.println("请输入算数范围:"); int min=sc.nextInt(); int max=sc.nextInt(); String[] ask=new String[num]; for(int i=0;i<num;i++){ a=(int)(Math.random()*(max-min)+min); int l= r.nextInt(s.length);//随机数,小于数组的长度数, 0~3 b=(int)(Math.random()*(max-min+1)+min); ask[i]=String.valueOf(a+s[l]+b+"="); System.out.print(ask[i]); System.out.print(""); int answer=sc.nextInt(); if("+"==s[l]){ if(answer==a+b){ System.out.println("this is right!"); count1++; } else { System.out.println("this is wroing!"+"正确答案是:"+(a+b)); count2++; } } else if("-"==s[l]){ if((a-b)>-11){ if(answer==a-b){ System.out.println("this is right!"); count1++; } else { System.out.println("this is wroing!"+"正确答案是:"+(a-b)); count2++; } } else{ a=(int)(Math.random()*(max-min)+min);; b=(int)(Math.random()*(max-min)+min);; if(answer==a-b){ System.out.println("this is right!"); count1++; } else { System.out.println("this is wroing!"+"正确答案是:"+(a-b)); count2++; } } } else if("*"==s[l]){ if(answer==a*b){ System.out.println("this is right!"); count1++; } else { System.out.println("this is wroing!"+"正确答案是:"+(a*b)); count2++; } } else if("/"==s[l]){ if(answer==a/b){ System.out.println("this is right!"); count1++; } else { System.out.println("this is wroing!"+"正确答案是:"+String.format("%.2f",a/b)); count2++; } } } System.out.println("此题对了:"+count1+" "+"错了:"+count2+" "+"得分为:"+5*count1); } else if (2 == op) { int count1=0,count2=0; double CountAnswer1,CountAnswer2,CountAnswer; System.out.println("请输入题目个数(5分/题):"); int num=sc.nextInt(); System.out.println("请输入算数范围:"); int min=sc.nextInt(); int max=sc.nextInt(); String[] ask=new String[num]; for(int i=0;i<num;i++){ a=(int)(Math.random()*(max-min)+min); b=(int)(Math.random()*(max-min)+min); c=(int)(Math.random()*(max-min)+min); d=(int)(Math.random()*(max-min)+min); int l= r.nextInt(s.length);//随机数,小于数组的长度数, 0~3 ask[i]=String.valueOf("("+a+"/"+b+")"+s[l]+"("+c+"/"+d+")"+"="); System.out.print(ask[i]); System.out.print(""); double answer=sc.nextDouble(); if("+"==s[l]){ CountAnswer1=(int)a/b; CountAnswer2=(int)c/d; CountAnswer=(double)CountAnswer1+CountAnswer2; if(CountAnswer==answer){ System.out.println("this is right!"); count1++; } else { System.out.println("this is wroing!"+"正确答案是:"+String.format("%.2f",CountAnswer)); count2++; } } if("-"==s[l]){ CountAnswer1=(int)a/b; CountAnswer2=(int)c/d; CountAnswer=(double)CountAnswer1-CountAnswer2; if(CountAnswer==answer){ System.out.println("this is right!"); count1++; } else { System.out.println("this is wroing!"+"正确答案是:"+String.format("%.2f",CountAnswer)); count2++; } } if("*"==s[l]){ CountAnswer1=(int)a/b; CountAnswer2=(int)c/d; CountAnswer=(double)CountAnswer1*CountAnswer2; if(CountAnswer==answer){ System.out.println("this is right!"); count1++; } else { System.out.println("this is wroing!"+"正确答案是:"+String.format("%.2f",CountAnswer)); count2++; } } if("/"==s[l]){ CountAnswer1=(int)a/b; CountAnswer2=(int)c/d; CountAnswer=(double)CountAnswer1/CountAnswer2; if(CountAnswer==answer){ System.out.println("this is right!"); count1++; } else { System.out.println("this is wroing!"+"正确答案是:"+String.format("%.2f",CountAnswer)); count2++; } } } System.out.println("此题对了:"+count1+" "+"错了:"+count2+" "+"得分为:"+5*count1); } else{ System.out.println("输入有误,请重新输入;"); } } } }
标签:
原文地址:http://www.cnblogs.com/wst-2015/p/4398023.html