四则运算测试与封装5.2 5.3 结对

时间:2021-12-31 21:52:11

开发环境:eclipse

结对同伴 :王逸辉

同伴博客:http://www.cnblogs.com/wyh624945780/

程序代码:

import java.util.Random;
public class JiS {
private char[] ch = new char[] { '+', '-', '*', '/' };
private int a;
private int b;
private int e;
private int d;
private char c;
private String[]str=new String[10];
private String[]stb=new String[10];
private String ss="";
private String sb="";
public JiS(){

}
public String tiM(int a,int b,int e,int d){
Random r
= new Random();
boolean flag = true;
while (flag) {
// a=(int) (Math.random()*200-100);
//a=10000;
//b=(int) (Math.random()*200-100);
//e=(int) (Math.random()*200-100);
//d=(int) (Math.random()*200-100);
try{
if(a>1000||a<-1000||b>1000||b<-1000||e>1000||e<-1000||d>1000||d<-1000)
{
throw new YC();
}

}
catch(YC z)
{
z.fw();
}

c
= ch[r.nextInt(ch.length)];
// System.out.println(a+","+b+","+c);

switch (c) {
case '+':
try{
if(a>1000||a<-1000||b>1000||b<-1000||e>1000||e<-1000||d>1000||d<-1000)
{

throw new YC();
}
}
catch(YC z)
{
z.fw();
}
if (a + b <= 10000)
{
ss
=a + "+" + b + "=" + (a + b);
sb
=b + "+" + a + "=" + (a + b);
flag
= false;
}
break;
case '-':
if (a - b >= 0) {
ss
=a + "-" + b + "=" + (a - b);
sb
=ss;
flag
= false;
}
break;
case '*':
if (a * b <= 10000) {
ss
=a + "*" + b + "=" + a * b;
sb
=b + "*" + a + "=" + a * b;
flag
= false;
}
break;
case '/':
if (b != 0) {
ss
=a + "/" + b + "=" + a / b;
sb
=ss;
flag
= false;
}
break;
}
}
return ss;
}
public void tiS(){
loop:
for(int i=0;i<10;i++){
tiM(a,b,e,d);
str[i]
=ss;
stb[i]
=sb;
ss
="";
sb
="";
for(int j=0;j<i;j++){
if(str[j]==str[i]||str[j]==stb[i]){i--;continue loop;}
}
System.out.println(
"第"+(i+1)+"题:"+str[i]);
}
}
public static void main(String[] args) {
JiS js
=new JiS();
js.tiS();
}
}
import java.util.*;
public class YC extends Exception{
public void chuling()
{
System.out.printf(
"分母不能为零!");
}
public void fw()
{
System.out.print(
"数值超出范围!");
}
}

 测试代码:

import static org.junit.Assert.*;

import org.junit.Test;


public class JiSTest {

@Test
public void testTiM() throws Error {
JiS jis
=new JiS();
String result
=jis.tiM(10, 8, 5,2);
assertEquals(
3,result);
//fail("Not yet implemented");
}

}

 

异常调试:

一开始我们弄数值大一点,显示超出数值提示。
四则运算测试与封装5.2 5.3 结对四则运算测试与封装5.2 5.3 结对

 心得与体会:

 这次的程序测试比想象的难,通过和结对队友的共同努力,加上我们研究网上的例子,终于搞定了。