代码改变世界

5.1 四则运算单元测试j

2015-05-05 18:40  可乐1230  阅读(105)  评论(1编辑  收藏  举报

由于上个星期请假没上课,这个星期回来才知道作业,时间比较赶,个人能力又不足,作业质量不是很好

Calculator.java



import java.util.Scanner;


public class Calculator {
    public static void main(String[] args) {
        int test(int n)
{
    int ranswer=0;            
    int uanswer=0;            
    int t=0;                
    char operation;            
    int num1=0;                
    int num2=0;                



    switch(n)
    {
        case 1:
            operation='+';
            break;

        case 2:
            operation='-';
            break;

        case 3:
            operation='*';
            break;

        case 4:
            operation='/';
            break;
    }


    if((operation=='-') && (num1<num2))
    {
        t=num1;
        num1=num2;
        num2=t;
    }


    if(operation=='/')
    {
        if(num2==0)
        {
            num2=1;
        }
        num1=num1*num2;            
    }

    printf("%d%c%d= ",num1,operation,num2);
    scanf("%d",&uanswer);


    switch(operation)
    {
        case '+':
        ranswer=num1+num2;
        break;

        case '-':
        ranswer=num1-num2;
        break;

        case '*':
        ranswer=num1*num2;
        break;

        case '/':
        ranswer=num1/num2;
        break;
    }
        
        Core core = new Core();
        core.calc(n, num1, num2);
    }
}
Core.java


public class Core {
    public void calc(String n, double num1, double num2) {
        if (n.equals("1")) {
            System.out.println(num1 + num2);
        } else if (n.equals("2")) {
            System.out.println(num1 - num2);
        } else if (n.equals("3")) {
            System.out.println(num1 * num2);
        } else if (n.equals("4")) {
            if (num2!= 0) {
                System.out.println(num1 / num2);
            } else {
                System.out.println("分母不能为零!");
            }
        } else {
            System.out.println("输入不符合要求!");
        }
    }
}