import java.util.*; public class First { public static void main (String[] agrs){ double a,b,c,d; int count1=0,count2=0; int i; String[] s={"+","-","*","/"}; Scanner sc=new Scanner(System.in); int op= 0; while(true){ System.out.println("选择功能:\n0.退出1.简单的加减乘除运算 2.复合运算 3.含真分数运算"); op= sc.nextInt(); if (0 == op) {break;} if (1 == op) { System.out.println("(5分/道)请输入题目数目:"); int n=sc.nextInt(); System.out.print("请输入算数的范围(由小到大):"); int min=sc.nextInt(); int max=sc.nextInt(); if(min>max){ System.out.print("输入出错"); break; } Random r= new Random(); for(i=0;i<n;i++){ a=r.nextInt(max)%(max-min+1) + min; b=r.nextInt(max)%(max-min+1) + min; int l= r.nextInt(s.length);//随机数,小于数组的长度数, 0~3 System.out.print(a+s[l]+b); //打印随机字符 System.out.print("="); double A=sc.nextDouble(); if("+"==s[l]){ if(A==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(A==a-b){ System.out.println("this is right!"); count1++; } else { System.out.println("this is wroing!"+"正确答案是:"+(a-b)); count2++; } } else{ a=r.nextInt(max)%(max-min+1) + min; b=r.nextInt(max)%(max-min+1) + min; if(A==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==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==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) {} else if (3 == op) { //分子 int q,p; //分母 int y,w; System.out.println("(5分/道)请输入题目数目:"); int n=sc.nextInt(); System.out.print("请输入算数的范围(由小到大):"); int min=sc.nextInt(); int max=sc.nextInt(); if(min>max){ System.out.print("输入出错"); break; } Random r= new Random(); for(i=0;i<n;i++){ q=r.nextInt(max)%(max-min+1) + min; p=r.nextInt(max)%(max-min+1) + min; y=r.nextInt(max)%(max-min+1) + min; w=r.nextInt(max)%(max-min+1) + min; int l= r.nextInt(s.length);//随机数,小于数组的长度数, 0~3 if((q<y && y!=0)||(p<w && w!=0)){ System.out.println(q+"/"+y+s[l]+p+"/"+w); //打印随机字符 System.out.print("="); double A=sc.nextDouble(); } } } else{ System.out.println("输入有误,请重新输入;"); } } } }
一开始,看到题目就开始做,没有详细的规划好,考虑自己的思路,想清楚需要定义几个类来实现功能,结果我所有的代码都在主函数里面了,导致我后来我越写越乱,错误百出,这样严重的养成了坏习惯。所以我决定还是放慢我的脚步,不在一味地追求能运行出结果就好了。我要学会如何运用类来编写我的程序,望老师指导,谢谢!
时间: 2024-12-12 13:01:55