package main.mymath; import java.util.InputMismatchException; import java.util.Scanner; public class MyCalculator { //Addition public void add(int num1,int1 num2) { int result; result = num1 + num2; if(result > Integer.MAX_VALUE || result < Integer.MIN_VALUE){ throw new IllegalArgumentException("输入数字导致溢出"); }else { System.out.println(result); } } //Multiplication public void subtraction(double num1,double num2) { System.out.println(num1 - num2); } //Division public void divide(double num1,double num2) { if(num2==0) { throw new IllegalArgumentException("分母不能为零"); }else { System.out.println(num1 / num2); } } public void calc() { Scanner sc = new Scanner(System.in); System.out.println("premier nombre:"); int num1 = sc.nextInt(); System.out.println("deuxieme nombre:"); int num2 = sc.nextInt(); System.out.println("choix【+,-,/】"); char op = sc.next().charAt(0); switch (op) { case '+': add(num1, num2); break; case '-': subtraction(num1, num2); break; case '/': divide(num1, num2); break; default: System.out.println("erreur!"); break; } } public static void main(String[] args) { MyCalculator c = new MyCalculator(); c.calc(); } }
时间: 2024-03-18 16:38:43 浏览: 112
好的,根据你提供的代码,以下是一个可能的测试类示例:
```
package test.mymath;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import java.io.ByteArrayInputStream;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;
import org.junit.Before;
import org.junit.Test;
import main.mymath.MyCalculator;
public class MyCalculatorTest {
private MyCalculator calculator;
@Before
public void setUp() {
calculator = new MyCalculator();
}
@Test
public void testAdd() {
assertEquals(5, calculator.add(2, 3));
assertEquals(-1, calculator.add(2, -3));
assertEquals(0, calculator.add(0, 0));
assertThrows(IllegalArgumentException.class, () -> calculator.add(Integer.MAX_VALUE, 1));
}
@Test
public void testSubtraction() {
assertEquals(2.0, calculator.subtraction(5.0, 3.0), 0.0);
assertEquals(-2.0, calculator.subtraction(-5.0, -3.0), 0.0);
assertEquals(0.0, calculator.subtraction(0.0, 0.0), 0.0);
assertEquals(0.1, calculator.subtraction(0.3, 0.2), 0.0);
}
@Test
public void testDivide() {
assertEquals(2.0, calculator.divide(6.0, 3.0), 0.0);
assertEquals(-2.0, calculator.divide(6.0, -3.0), 0.0);
assertEquals(0.0, calculator.divide(0.0, 1.0), 0.0);
assertThrows(IllegalArgumentException.class, () -> calculator.divide(6.0, 0.0));
}
@Test
public void testCalc() {
String input = "2\n3\n+\n";
System.setIn(new ByteArrayInputStream(input.getBytes()));
Scanner scanner = new Scanner(System.in);
calculator.calc(scanner);
assertEquals("5\n", systemOut().getHistory());
input = "5.0\n3.0\n-\n";
System.setIn(new ByteArrayInputStream(input.getBytes()));
scanner = new Scanner(System.in);
calculator.calc(scanner);
assertEquals("2.0\n", systemOut().getHistory());
input = "4.0\n2.0\n/\n";
System.setIn(new ByteArrayInputStream(input.getBytes()));
scanner = new Scanner(System.in);
calculator.calc(scanner);
assertEquals("2.0\n", systemOut().getHistory());
}
@Test
public void testCalcInvalidInput() {
String input = "invalid\ninput\n";
System.setIn(new ByteArrayInputStream(input.getBytes()));
Scanner scanner = new Scanner(System.in);
assertThrows(NoSuchElementException.class, () -> calculator.calc(scanner));
}
private SystemOut systemOut() {
return new SystemOut();
}
private static class SystemOut extends java.io.ByteArrayOutputStream {
public String getHistory() {
return new String(this.toByteArray());
}
}
}
```
在上面的测试类示例中,我们使用JUnit框架来测试MyCalculator类的三个方法:add(),subtraction()和divide(),并使用标准输入输出模拟用户交互,测试calc()方法是否能够正确的计算加、减、除三种运算。同时,我们还测试了一些输入无效数据的情况,来保证程序的健壮性。
阅读全文