1.设计思路
1) 在原有的程序的基础上加上判断运算是否正确的函数;
(1)因为输出的算式中涉及到分数的运算,所以要写一个分数运算的类,进行调用;
(2)在判断的过程中首先先判断为哪种运算,然后判断结果;
(3) 在判断的函数中要设置两个函数,一个为输出的算式,另一个为输入的结果;
(4)在原有的基础上,在主程序中将算式设置为输出一个,然后输入结果判断完成后再输出另一个算式;
2)分数计算的函数,
(1)分数计算首先将分子分母从算式中截出来,进行分子分母的运算;
(2)约分计算:取分子分母的最大公约数,除以最大公约数,得到的分子分母就是约分后的分子分母;
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 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;itime2)//判断除法计算是否正确 { char ch[]=s.toCharArray(); int time=0; for(int i=0;i =oneNumber2) { temp=oneNumber2; } else { temp=oneNumber1; } for(int i=2;i<=temp;i++) { if(oneNumber1%i==0&&oneNumber2%i==0) { oneNumber1=oneNumber1/i; oneNumber2=oneNumber2/i; temp=temp/i; i=1; } } if(oneNumber2==1) { return String.valueOf(oneNumber1); } else { return (String.valueOf(oneNumber1)+"/"+String.valueOf(oneNumber2)); } } public String jiafa(String one,String two)//分数加法计算 { int fenzi,fenmu; int oneNumber1=stringtoInteger1(one); int oneNumber2=stringtoInteger2(one); int twoNumber1=stringtoInteger1(two); int twoNumber2=stringtoInteger2(two); if(oneNumber2==twoNumber2) { fenzi=oneNumber1+twoNumber1; fenmu=oneNumber2; } else { fenmu=oneNumber2*twoNumber2; fenzi=oneNumber1*twoNumber2+oneNumber2*twoNumber1; } return (String.valueOf(fenzi)+"/"+String.valueOf(fenmu)); } public String jianfa(String one,String two)//分数减法计算 { int fenzi,fenmu; int oneNumber1=stringtoInteger1(one); int oneNumber2=stringtoInteger2(one); int twoNumber1=stringtoInteger1(two); int twoNumber2=stringtoInteger2(two); if(oneNumber2==twoNumber2) { fenzi=oneNumber1-twoNumber1; fenmu=oneNumber2; } else { fenmu=oneNumber2*twoNumber2; fenzi=oneNumber1*twoNumber2-oneNumber2*twoNumber1; } return (String.valueOf(fenzi)+"/"+String.valueOf(fenmu)); } public String chengfa(String one,String two)//分数的乘法计算 { int fenzi,fenmu; int oneNumber1=stringtoInteger1(one); int oneNumber2=stringtoInteger2(one); int twoNumber1=stringtoInteger1(two); int twoNumber2=stringtoInteger2(two); fenzi=oneNumber1*twoNumber1; fenmu=oneNumber2*twoNumber2; return (String.valueOf(fenzi)+"/"+String.valueOf(fenmu)); } public String chufa(String one,String two)//分数的除法计算 { int fenzi,fenmu; int oneNumber1=stringtoInteger1(one); int oneNumber2=stringtoInteger2(one); int twoNumber1=stringtoInteger1(two); int twoNumber2=stringtoInteger2(two); fenzi=oneNumber1*twoNumber2; fenmu=oneNumber2*twoNumber1; return (String.valueOf(fenzi)+"/"+String.valueOf(fenmu)); } public int stringtoInteger1(String s)//数与字符串转换 { if(s.contains("(")) {} else { s="("+s+")"; } String oneNumberToInteger1=s.substring(1, s.indexOf("/")); int oneNumber1=Integer.parseInt(oneNumberToInteger1); return oneNumber1; } public int stringtoInteger2(String s) { if(s.contains("(")) {} else { s="("+s+")"; } String oneNumberToInteger2=s.substring(s.indexOf("/")+1,s.length()-1); int oneNumber2=Integer.parseInt(oneNumberToInteger2); return oneNumber2; }}
3.运行结果截图
4
.