标签:
1.设计思路
1.输入
1)将条件以字符串形式输入,通过判断字符串来执行相应的功能;
2.计算
1)建一个主类,和一个计算类,其中计算类执行各种条件的运行;
计算类包括的方法:求随机数的函数,求加法的函数,求减法的函数,求乘法的函数,求除法的函数,和一个综合运算函数;
2)基本:加减法的输出,在无外部条件的时候保证能够输出加减法;
(1)在计算类设置一个带参的函数,这个参数就是区分加减法和乘除法的关键;
具体步骤:将各种符号封装在一个长度为4的数组中,通多产生0-2和0-4的随机数来控制输出加减法还是输出乘除法;
3)是否有乘除法:如果有乘除法,将上边运算的函数中的参数改为由2改为4;
4)是否加减有无负数:在求加减法的函数中加入判断语句,通过判断加减法得数是否小于0;
5)是否除法有余数:在求除法的函数中添加判断求余是否为0;
3.输出
将类中求各种算式的方法的返回值设置为String类型,通过在主函数中进行换行相加得到最后结果,输出结果;
2.代码
package 四则运算2; import java.util.Random; import java.util.Scanner; public class FourOperation { public static void main(String args[]) { Yunsuan a=new Yunsuan(); @SuppressWarnings("resource") Scanner scanner=new Scanner(System.in); String strNull = null; String temp; /*.................................................................................................*/ System.out.println("请选择计算数量:"); int n=scanner.nextInt(); //输入输出算式的个数 /*...................................................................................................*/ System.out.println("请选择以下条件:是否需要乘除法 "); System.out.println("请输入:y1/n1"); String i1=scanner.next(); //输入是否需要乘法 /*....................................................................................................*/ System.out.println("选择数值范围:1. 0-100, 2. 0-200"); int i2=scanner.nextInt(); if(i2==1) { i2=100; } /*选择数值范围,其中i2是为了后边isOrNotMultiplicationAndDivision(4,i2,i3,i4) 调用,进行取范围运算*/ if(i2==2) { i2=200; } /*.....................................................................................................*/ System.out.println("是否选择加减是有负数:y2/n2"); String i3=scanner.next(); //选择是否加减有负数 /*.......................................................................................................*/ System.out.println("是否选择除法有余数:y3/n3"); String i4=scanner.next(); //选择除法是否有余数 /*............................................................................................*/ if(i1.equals("y1")) { for(int i=0;i<n;i++) { temp=a.isOrNotMultiplicationAndDivision(4,i2,i3,i4); if(temp=="") { n=n+1; strNull=strNull+temp; } else { strNull=strNull+temp+"\n"; } } } if(i1.equals("n1")) { for(int i=0;i<n;i++) { temp=a.isOrNotMultiplicationAndDivision(2,i2,i3,i4); if(temp=="") { n=n+1; //求算式的运算 strNull=strNull+temp; } else { strNull=strNull+temp+"\n"; } } } System.out.println(strNull); } }/*......................................................................................................*/ class Yunsuan{ private String firstNumber; private String secondNumber; private String number; char fuhao[]={‘+‘,‘-‘,‘*‘,‘/‘}; public String randomNumber(int i)//求随机数的函数 { Random random=new Random(); int temp=Math.abs(random.nextInt()%2); if(temp==1) { number="("+String.valueOf(Math.abs(random.nextInt()%i))+"/"+String.valueOf(Math.abs(random.nextInt()%i)+")"); } else { int f=random.nextInt(); number=String.valueOf(Math.abs(f%i)); } return number; } public String addtion(int i,String s)//求加法的函数 { firstNumber=randomNumber(i); secondNumber=randomNumber(i); double firstNumberToInteger = 0.0 ; double secondNumberToInteger = 0.0; if(firstNumber.contains("/"))//判断是否为分数,如果是要进行计算成小数便于后边判断是否为负数 { String firstNumberToInteger1=firstNumber.substring(1, firstNumber.indexOf("/")); String firstNumberToInteger2=firstNumber.substring(firstNumber.indexOf("/")+1,firstNumber.length()-1); firstNumberToInteger=(Double.parseDouble(firstNumberToInteger1))/(Double.parseDouble(firstNumberToInteger2)); } else { firstNumberToInteger=Double.parseDouble(firstNumber); } if(secondNumber.contains("/"))//同上 { String secondNumberToInteger1=secondNumber.substring(1, secondNumber.indexOf("/")); String secondNumberToInteger2=secondNumber.substring(secondNumber.indexOf("/")+1,secondNumber.length()-1); secondNumberToInteger=(Double.parseDouble(secondNumberToInteger1))/(Double.parseDouble(secondNumberToInteger2)); } else { secondNumberToInteger=Double.parseDouble(secondNumber); } if(s.equals("y2")) { return firstNumber+"+"+secondNumber+"="; } if(s.equals("n2")) { double n=firstNumberToInteger+secondNumberToInteger; if(n<0) { return ""; } else { return firstNumber+"+"+secondNumber+"="; } } return ""; } public String subtraction(int i,String s)//求减法的函数 { firstNumber=randomNumber(i); secondNumber=randomNumber(i); firstNumber=randomNumber(i); secondNumber=randomNumber(i); double firstNumberToInteger = 0.0 ; double secondNumberToInteger = 0.0; if(firstNumber.contains("/"))//同加法 { String firstNumberToInteger1=firstNumber.substring(1, firstNumber.indexOf("/")); String firstNumberToInteger2=firstNumber.substring(firstNumber.indexOf("/")+1,firstNumber.length()-1); firstNumberToInteger=(Double.parseDouble(firstNumberToInteger1))/(Double.parseDouble(firstNumberToInteger2)); } else { firstNumberToInteger=Double.parseDouble(firstNumber); } if(secondNumber.contains("/"))//同加法 { String secondNumberToInteger1=secondNumber.substring(1, secondNumber.indexOf("/")); String secondNumberToInteger2=secondNumber.substring(secondNumber.indexOf("/")+1,secondNumber.length()-1); secondNumberToInteger=(Double.parseDouble(secondNumberToInteger1))/(Double.parseDouble(secondNumberToInteger2)); } else { secondNumberToInteger=Double.parseDouble(secondNumber); } if(s.equals("y2")) { return firstNumber+"-"+secondNumber+"="; } if(s.equals("n2")) { double n=firstNumberToInteger-secondNumberToInteger; if(n<0) { return ""; } else { return firstNumber+"-"+secondNumber+"="; } } return ""; } public String multiplication(int i)//求乘法的函数 { firstNumber=randomNumber(i); secondNumber=randomNumber(i); return firstNumber+"*"+secondNumber+"="; } public String division(int i,String s)//求除法的函数 { firstNumber=randomNumber(i); secondNumber=randomNumber(i); if(s=="y3") { return firstNumber+"/"+secondNumber+"="; } if(s=="n3") { if(firstNumber.contains("/")||secondNumber.contains("/")) { return firstNumber+"/"+secondNumber+"="; } else { if((Integer.parseInt(firstNumber))%(Integer.parseInt(secondNumber))==0) { return firstNumber+"/"+secondNumber+"="; } else { return ""; } } } return ""; } public String isOrNotMultiplicationAndDivision(int i,int j,String s1,String s2)//进行运算的函数 { Yunsuan b=new Yunsuan(); String str = null; Random random=new Random(); int k=random.nextInt(); int demp=Math.abs(k%i); switch(fuhao[demp]) { case ‘+‘: { str=b.addtion(j,s1); }break; case ‘-‘: { str=b.subtraction(j,s1); }break; case ‘*‘: { str=b.multiplication(j); }break; case ‘/‘: { str=b.division(j,s2); } } return str; } }
3.运行结果截图
4.项目计划总结
时间记录日志
缺陷记录日志
标签:
原文地址:http://www.cnblogs.com/wanggang-java/p/5295710.html