作业5.2
package case1; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Scanner; public class no1 { public static void main(String[] args) { int x,y; String z ,p,p1; Scanner inr = new Scanner(System.in); BufferedReader in=new BufferedReader(new InputStreamReader(System.in)) ; System.out.print("请输入第一个数字:") ; while(true) { try { p=in.readLine(); //如果转换异常则输入的不是数字 x=Integer.parseInt(p); if(x>0) { System.out.println("\n") ; } break; } catch(Exception e) { System.out.println("\n错误") ; } } System.out.print("请输入第二个数字:") ; while(true) { try { p1=in.readLine(); //如果转换异常则输入的不是数字 y=Integer.parseInt(p1); if(y>0) { System.out.println("\n") ; } break; } catch(Exception e) { System.out.println("\n错误") ; } } System.out.println("请输入运算符(+,-,*,/):"); z = inr.nextLine(); System.out.println("\n"+x+" "+z+" "+y+"="); no2 test = new no2(); test.case1(z, x, y); } } package case1; public class no2 { public void case1(String cc, double a, double b) { jia test1 = new jia(); if (cc.equals("+")) { test1.jia1(a , b); } else if (cc.equals("-")) { test1.jian1(a , b); } else if (cc.equals("*")) { test1.chen1(a , b); } else if (cc.equals("/")) { if (b != 0) { test1.chu1(a , b); } else { System.out.println("division by zero!"); throw new ArithmeticException(); } } else { System.out.println("运算符不符合!"); } } } package case1; public class jia { /** * @param args */ public static double jia1( double a, double b) { double anw=0; anw=a+b; System.out.println(a +"+"+ b); System.out.println(anw); return anw; } public static double jian1( double a, double b) { double anw=0; anw=a-b; System.out.println(a +"-"+ b); System.out.println(anw); return anw; } public static double chen1( double a, double b) { double anw=0; anw=a*b; System.out.println(a +"*"+ b); System.out.println(anw); return anw; } public static double chu1( double a, double b) { double anw=0; anw=a/b; System.out.println(a +"/"+ b); System.out.println(anw); return anw; } } package case1; import static org.junit.Assert.*; import org.junit.Test; public class jiaTest { @Test public void testJia1() { double w; jia we=new jia(); w=jia.jia1(1,2); } @Test public void testJian1() { double w; jia we=new jia(); w=jia.jian1(3,1); } @Test public void testChen1() { double w; jia we=new jia(); w=jia.chen1(3,1); } @Test public void testChu1() { double w; jia we=new jia(); w=jia.chu1(4,2); } }
运行截图:
心得:
看了何广强同学写的测试之后才清楚明白要如何做测试,非常感谢他,之前老师讲得一直不明白,也不知道她在讲什么,这次认真研究何广强同学的才能自己写出测试,
虽然比较简单,本来也想在用户输入数字和运算符那里也加入测试,但还没做出了,出现了其他错误,于是没有加入,只是在no1类里的用户输入时那个代码那里加入了
捕获异常,用户输入非数字时提示错误并重新输入,但连分数也提示错误,用Integer.parseInt(p);转换成数字可以,但不能转换到分数,还有就是后面的tdd测试也未
能完成。