软件工程个人作业01
Posted xiaohaigege666
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了软件工程个人作业01相关的知识,希望对你有一定的参考价值。
一个能自动生成三十道小学四则运算题目的 “软件”,要求:除了整数以外,还要支持真分数的四则运算(需要验证结果的正确性)、题目避免重复、可定制出题的数量。
程序的设计思想:
将整数四则运算和分数四则运算分别写成一个类,然后在测试类中利用了随机数数生成0和1分别调用两个类,在控制台输出题目。
序代源程码:
package cal; import java.util.Random; import java.util.Scanner; class jis { public static int flag=0; public void show1() { double right = 0; double wrong = 0; for (int i = 0; i < 10; i++) { System.out.println("第"+(i+1)+"道题,请在下方输入答案:"); //生成两个随机数 Random r1 = new Random(); int x = r1.nextInt(100)+1; Random r2 = new Random(); int y = r2.nextInt(100)+1; //生成随机运算符 + - * / Random r3 = new Random(); int z = r3.nextInt(4); char[] chs = {\'+\',\'-\',\'*\',\'/\'}; String Operator = String.valueOf(chs[z]); //生成题目 if(Operator.equals("+")){ System.out.println(x+"+"+y+"="); boolean b = add(x,y); if(b == true){ right++; System.out.println("bingo! "); }else{ wrong++; System.out.println("oh~no "); } System.out.println("the right answer: "+(x+y)); }else if(Operator.equals("-")){ System.out.println(x+"-"+y+"="); boolean b =minus(x,y); if(b == true){ right++;System.out.println("bingo! "); }else{ wrong++;System.out.println("oh~no "); } System.out.println("the right answer: "+(x-y)); }else if(Operator.equals("*")){ System.out.println(x+"×"+y+"="); boolean b =times(x,y); if(b == true){ right++;System.out.println("bingo! "); }else{ wrong++;System.out.println("oh~no "); } System.out.println("the right answer: "+(x*y)); }else{ System.out.println(x+"÷"+y+"="); boolean b =divide(x,y); if(b == true&&flag==0){ right++;System.out.println("bingo! "); } else if(b == false&&flag==0) { wrong++;System.out.println("oh~no "); } System.out.println("the right answer: "+((float)x /(float) y)); } System.out.println("-------------------------------"); } System.out.println("您一共做对了"+right+"道题."); System.out.println("您一共做错了"+wrong+"道题."); if(wrong > 0){ System.out.println("正确率为"+(right/(wrong+right))*100+"%"); }else{ System.out.println("Unbelievable!"); } } private static boolean add(int x,int y) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int num1 = sc.nextInt(); int result = x + y; if(num1 == result){ return true; }else{ return false; } } private static boolean minus(int x,int y) { // TODO Auto-generated method stub if(x<y) { int t=x; x=y; y=t; } Scanner sc = new Scanner(System.in); int num1 = sc.nextInt(); int result = x - y; if(num1 == result){ return true; }else{ return false; } } private static boolean times (int x,int y) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int num1 = sc.nextInt(); int result = x * y; if(num1 == result){ return true; }else{ return false; } } private static boolean divide(int x,int y) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int num1 = sc.nextInt(); if(x%y==0) { int result =x/y; if(num1==result) { return true; } else { return false; } } else { flag=1; } return true; } }
package cal; import java.util.Random; import java.util.Scanner; class demojisuan { public static String s1=""; public static String s2=""; public static String s3=""; public static String s4=""; public void show2() { double right = 0; double wrong = 0; for(int i=0;i<10;i++) { System.out.println("第"+(i+1)+"道题,请在下方输入答案:"); Random r1 = new Random(); int x1 = r1.nextInt(10)+1; int x2 = r1.nextInt(10)+1; Random r2 = new Random(); int y1 = r2.nextInt(10)+1; int y2 = r2.nextInt(10)+1; //生成随机运算符 + - * / Random r3 = new Random(); int z = r3.nextInt(4); char[] chs = {\'+\',\'-\',\'*\',\'/\'}; String Operator = String.valueOf(chs[z]); //生成题目 if(Operator.equals("+")){ System.out.println(x1+"/"+y1+"+"+x2+"/"+y2+"="); boolean b = fracAdd(x1,y1,x2,y2); if(b == true){ right++; System.out.println("bingo! "); }else{ wrong++; System.out.println("oh~no "); } System.out.println("the right answer: "+s1); } else if(Operator.equals("-")){ System.out.println(x1+"/"+y1+"-"+x2+"/"+y2+"="); boolean b = fracSub(x1,y1,x2,y2); if(b == true){ right++;System.out.println("bingo! "); }else{ wrong++;System.out.println("oh~no "); } System.out.println("the right answer: "+s2); }else if(Operator.equals("*")){ System.out.println(x1+"/"+y1+"*"+x2+"/"+y2+"="); boolean b =fracMul(x1,y1,x2,y2); if(b == true){ right++;System.out.println("bingo! "); }else{ wrong++;System.out.println("oh~no "); } System.out.println("the right answer: "+s3); }else{ System.out.println(x1+"/"+y1+"/"+x2+"/"+y2+"="); boolean b =fractDiv(x1,y1,x2,y2); if(b == true){ right++;System.out.println("bingo! "); }else{ wrong++;System.out.println("oh~no "); } System.out.println("the right answer: "+s4); } System.out.println("-------------------------------"); } System.out.println("您一共做对了"+right+"道题."); System.out.println("您一共做错了"+wrong+"道题."); if(wrong > 0){ System.out.println("正确率为"+(right/(wrong+right))*100+"%"); }else{ System.out.println("Unbelievable!"); } } static boolean fracAdd(int first_numerator,int first_denominator,int second_numrator,int second_denominator){ //以下代码能够在控制台上显示结果 //需要调用求最大公约数的函数 //需要调用求最小公倍数的函数 int denominator; int numerator; if(first_denominator==second_denominator) //分母相同时加分子 { denominator=first_denominator; numerator=first_numerator+second_numrator; } else //否则同分比较分子 { denominator=first_denominator*second_denominator; numerator=first_numerator*second_denominator+first_denominator*second_numrator; } int gcd = gcd(numerator,denominator); denominator = denominator / gcd; numerator = numerator / gcd; Scanner sc = new Scanner(System.in); String num1 = sc.next(); String result = numerator+"/"+denominator; s1=result; if(num1.equals(result)){ return true; }else{ return false; } } static boolean fracSub(int first_numerator,int first_denominator,int second_numrator,int second_denominator){ //以下代码能够在控制台上显示结果 //需要调用求最大公约数的函数 int denominator; int numerator; if(first_denominator==second_denominator) //分母相同时加分子 { denominator=first_denominator; numerator=first_numerator-second_numrator; } else //否则同分比较分子 { denominator=first_denominator*second_denominator; numerator=first_numerator*second_denominator-first_denominator*second_numrator; } int gcd = gcd(numerator,denominator); denominator = denominator / gcd; numerator = numerator / gcd; Scanner sc = new Scanner(System.in); String num1 = sc.next(); String result = numerator+"/"+denominator; s2=result; if(num1.equals(result)){ return true; }else{ return false; } } static boolean fracMul(int first_numerator,int first_denominator,int second_numerator,int second_denominator){ //以下代码能够在控制台上显示结果 //需要调用求最大公约数的函数 int denominator; int numerator; denominator=first_denominator*second_denominator; numerator=first_numerator*second_numerator; int gcd = gcd(numerator,denominator); denominator = denominator / gcd; numerator = numerator / gcd; Scanner sc = new Scanner(System.in); String num1 = sc.next(); String result = numerator+"/"+denominator; s3=result; if(num1.equals(result)){ return true; }else{ return false; } } static boolean fractDiv(int first_numerator,int first_denominator,int second_numerator,int second_denominator){ //以下代码能够在控制台上显示结果 //需要调用求最大公约数的函数 int denominator; int numerator; numerator = first_numerator*second_denominator; denominator = first_denominator*second_numerator; int gcd = gcd(numerator,denominator); denominator = denominator / gcd; numerator = numerator / gcd; Scanner sc = new Scanner(System.in); String num1 = sc.next(); String result = numerator+"/"+denominator; s4=result; if(num1.equals(result)){ return true; }else{ return false; } } static int gcd(int x,int y){ //最大公约数 int r; while( y!= 0) { r = x%y; x = y; y = r; } return x; } }
package cal; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub jis jis=new jis(); jis.show1(); demojisuan jisuan=new demojisuan(); jisuan.show2(); } }
运行结果截图:
以上是关于软件工程个人作业01的主要内容,如果未能解决你的问题,请参考以下文章