Java简单计算器,了解开关和方法兼容性

时间:2014-08-01 04:26:58

标签: java if-statement methods switch-statement

我在暑期班上有一个任务是制作一个简单的计算器而且我遇到了一些问题。

开头有一个菜单询问用户想要做什么,其中一个选项是退出,其中一个选项是平方用户输入。

我遇到的问题是我无法想出用开关退出程序的方法,所以我把它从开关中取出并做了一个有条件的,那就好了。所以,我想这不是什么大问题,除非有人想告诉我如何将它包含在交换机中。我现在遇到的问题是平方用户输入。问题是程序询问用户他们想要做什么,然后要求他们输入两个操作数。但是,我只希望它要求一个操作数。所以,我把它放在条件中,它只要求一个操作数,但它似乎不再使用方形方法和计算结果。

非常感谢任何帮助!!

import java.util.Scanner;

public class Driver {

public static void main(String[] args) {

    Menu m = new Menu();
    m.getMenu();

  }    
}       



class Menu {    
    public void getMenu() {
        Scanner scan = new Scanner(System.in);

        System.out.println("Welcome to 00 Calculator menu, this calculator has the following options:");
        System.out.println("1 - Add " + "\n" + "2 - Subtract" + "\n"
                + "3 - Multiply" + "\n" + "4 - Divide" + "\n" 
                + "5 - Square" + "\n" + "6 - Modulus" + "\n" + "0 - Quit" );
        int optionInt = scan.nextInt();

         if (optionInt == 0) {
            System.out.println("Goodbye!"); 
            return;

        }else if (optionInt == 5) {
            System.out.println("Enter the first number :");
            double num1 = scan.nextDouble();
        }else {
            System.out.println("Enter the first number :");
            double num1 = scan.nextDouble();
            System.out.println("Enter the second number :");
            double num2 = scan.nextDouble();

            double result;
            Action option = new Action();


            switch (optionInt) {
            case 1:
                result = option.addValues(num1, num2);
                option.displayResult(result);
                break;
            case 2:
                result = option.subtractValues(num1, num2);
                option.displayResult(result);
                break;
            case 3:
                result = option.multiplyValues(num1, num2);
                option.displayResult(result);
                break;  
            case 4:
                result = option.divideValues(num1, num2);
                option.displayResult(result);
                break;  
            case 5:
                result = option.squareValues(num1, num2);
                option.displayResult(result);
                break;  
            case 6:
                result = option.modulusValues(num1, num2);
                option.displayResult(result);
                break;          
            default:
                System.out.println("You entered an incorrect option.  Goodbye.");
           } 
        }   
    }
}


class Action {

public double divideValues(double d1, double d2) {
    double result = d1 / d2;
    return result;
}

public double multiplyValues(double d1, double d2) {
    double result = d1 + d2;
    return result;
}

public double subtractValues(double d1, double d2) {
    double result = d1 - d2;
    return result;
}

public double addValues(double d1, double d2) {
    double result = d1 + d2;
    return result;
}

public double squareValues(double d1, double d2) {
    double result = Math.pow(d1, 2);
    return result;
}

public double modulusValues(double d1, double d2) {
    double result = d1 % d2;
    return result;
}

public void displayResult(double result) {
    System.out.println("The answer is " + result);
 }

}

4 个答案:

答案 0 :(得分:3)

由于您的要求很复杂,退出选项最好留在它的位置......

现在,如果您不选择0,则有两种可能性,您需要根据选项需求请求一个或两个值。

在这种情况下,如果是optionInt != 5,那么你需要两个值,但是你总是需要一个,所以你总是可以要求,例如......

if (optionInt == 0) {
    System.out.println("Goodbye!"); 
    return;
} else {
    System.out.println("Enter the first number :");
    double num1 = scan.nextDouble();
    double num2 = 0;
    if (optionInt != 5) {
        System.out.println("Enter the second number :");
        num2 = scan.nextDouble();
    }
    double result;
    Action option = new Action();


    switch (optionInt) {
    case 1:
        result = option.addValues(num1, num2);
        option.displayResult(result);
        break;
    case 2:
        result = option.subtractValues(num1, num2);
        option.displayResult(result);
        break;
    case 3:
        result = option.multiplyValues(num1, num2);
        option.displayResult(result);
        break;  
    case 4:
        result = option.divideValues(num1, num2);
        option.displayResult(result);
        break;  
    case 5:
        result = option.squareValues(num1); // You will need to update your Action class
        option.displayResult(result);
        break;  
    case 6:
        result = option.modulusValues(num1, num2);
        option.displayResult(result);
        break;          
    default:
        System.out.println("You entered an incorrect option.  Goodbye.");
   } 

}   

阅读完代码之后,您甚至不需要检查退出选项,因为一旦您以任何方式完成操作,您的方法就会退出,因此您可以简单地执行类似操作。 ..

if (optionInt != 0) {  
    // Process other options...
}

//... method will exit of it's own accord, you don't need to do anything...

<强>更新

另一种方法可能是编写一种方法,在需要时提示用户输入所需的值,例如......

public double getDouble(String prompt) {
    System.out.println(prompt);
    Scanner scan = new Scanner(System.in);
    double value = scan.nextDouble();
    scan.nextLine();
    return value;
}

public void getMenu() {
    Scanner scan = new Scanner(System.in);

    System.out.println("Welcome to 00 Calculator menu, this calculator has the following options:");
    System.out.println("1 - Add " + "\n" + "2 - Subtract" + "\n"
            + "3 - Multiply" + "\n" + "4 - Divide" + "\n"
            + "5 - Square" + "\n" + "6 - Modulus" + "\n" + "0 - Quit");
    int optionInt = scan.nextInt();
    scan.nextLine();

    double result;
    Action option = new Action();
    switch (optionInt) {
        case 0:
            System.out.println("Good bye!");
            break;
        case 1:
            result = option.addValues(
                    getDouble("Enter the first number :"),
                    getDouble("Enter the second number :")
            );
            option.displayResult(result);
            break;
        case 2:
            result = option.subtractValues(
                    getDouble("Enter the first number :"),
                    getDouble("Enter the second number :")
            );
            option.displayResult(result);
            break;
        case 3:
            result = option.multiplyValues(
                    getDouble("Enter the first number :"),
                    getDouble("Enter the second number :")
            );
            option.displayResult(result);
            break;
        case 4:
            result = option.divideValues(
                    getDouble("Enter the first number :"),
                    getDouble("Enter the second number :")
            );
            option.displayResult(result);
            break;
        case 5:
            result = option.squareValues(
                    getDouble("Enter a number :"),
            );
            option.displayResult(result);
            break;
        case 6:
            result = option.modulusValues(
                    getDouble("Enter the first number :"),
                    getDouble("Enter the second number :")
            );
            option.displayResult(result);
            break;
        default:
            System.out.println("You entered an incorrect option.  Goodbye.");
    }
}

答案 1 :(得分:1)

switch (optionInt) {
        case 0: // simply add a case for 'quit'
            return;
        case 1:
            result = option.addValues(num1, num2);
            option.displayResult(result);
            break;

答案 2 :(得分:1)

我想如果你想把选项退回到你的交换机中,而不是使用&#34; return&#34;你可以使用System.exit(0);

这应该让你退出计算器,让来电者知道一切都好了。如果有问题,那么你应该使用System.exit(1); &lt; ---如果用户选择退出,则没有问题。

答案 3 :(得分:0)

为了让您的代码更有效我不会在您的方法squareValues(double d1,double d2)中知道,如果您是two arguments,为什么会有utilizing only one。但是要使代码正常工作,请使用

替换以下代码段
else if (optionInt == 5) {
            System.out.println("Enter the first number :");
            double num1 = scan.nextDouble();

这一个

else if (optionInt == 5) {
            System.out.println("Enter the first number :");
            double num1 = scan.nextDouble();
Action option = new Action();
 result = option.squareValues(num1, num2);//num2 is useless here,remove it from your squareValues method too
            option.displayResult(result);

}

并从case 5:声明中删除switch