四则运算升级1
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package NM; 2 import java.util.*; 3 public class First { 4 public static void main (String[] agrs){ 5 double a,b,c,d; 6 int count1=0,count2=0; 7 int i; 8 String[] s={"+","-","*","/"}; 9 Random r=new Random(); 10 Scanner sc=new Scanner(System.in); 11 int op= 0; 12 while(true){ 13 System.out.println("选择功能:\n0.退出1.简单的加减乘除运算 2.复合运算 3.含真分数运算"); 14 op= sc.nextInt(); 15 if (0 == op) {break;} 16 if (1 == op) { 17 System.out.println("(5分/道)请输入题目数目:"); 18 int n=sc.nextInt(); 19 System.out.println("请输入算数的最大值:"); 20 int m=sc.nextInt(); 21 for(i=0;i<n;i++){ 22 a=(int) (Math.random()*(m+1)); 23 b=(int) (Math.random()*(m+1)); 24 int l= r.nextInt(s.length);//随机数,小于数组的长度数, 0~3 25 System.out.print(a+s[l]+b); //打印随机字符 26 System.out.print("="); 27 double A=sc.nextDouble(); 28 if("+"==s[l]){ 29 if(A==a+b){ 30 System.out.println("this is right!"); 31 count1++; 32 } 33 else { 34 System.out.println("this is wroing!"+"正确答案是:"+(a+b)); 35 count2++; 36 } 37 } 38 else if("-"==s[l]){ 39 if((a-b)>-11){ 40 if(A==a-b){ 41 System.out.println("this is right!"); 42 count1++; 43 } 44 else { 45 System.out.println("this is wroing!"+"正确答案是:"+(a-b)); 46 count2++; 47 } 48 } 49 else{ 50 a=(int) (Math.random()*(m+1)); 51 b=(int) (Math.random()*(m+1))+1; 52 } 53 } 54 else if("*"==s[l]){ 55 if(A==a*b){ 56 System.out.println("this is right!"); 57 count1++; 58 } 59 else { 60 System.out.println("this is wroing!"+"正确答案是:"+(a*b)); 61 count2++; 62 } 63 } 64 else if("/"==s[l]){ 65 if(A==a/b){ 66 System.out.println("this is right!"); 67 count1++; 68 } 69 else { 70 System.out.println("this is wroing!"+"正确答案是:"+String.format("%.2f",a/b)); 71 count2++; 72 } 73 } 74 } 75 System.out.println("此题对了:"+count1+" "+"错了:"+count2+" "+"得分为:"+5*count1); 76 } 77 else if (2 == op) { 78 79 80 81 82 83 84 } 85 else if (3 == op) { 86 87 } 88 else{ 89 System.out.println("输入有误,请重新输入;"); 90 } 91 } 92 93 } 94 }
![](http://images0.cnblogs.com/blog2015/734892/201503/301302255601861.png)
使用的是工具是:eclipse
补上昨天的成果,用零碎的时间来写代码的,所以具体完成的时间不是很清楚,做的也很仓促,没有过多的考虑代码的整合。第二第三个功能,想的比较混乱,还没有做出来,随后回补上的。坚持,争取有进步~~~
补上昨天的成果,用零碎的时间来写代码的,所以具体完成的时间不是很清楚,做的也很仓促,没有过多的考虑代码的整合。第二第三个功能,想的比较混乱,还没有做出来,随后回补上的。坚持,争取有进步~~~