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);
}
}
同伴: 董大为 http://www.cnblogs.com/case1/