做一个基本的计算器

时间:2014-08-03 04:56:37

标签: java java.util.scanner

所以我是Java的新手并且练习我的技能我决定制作一个基本的计算器。所以这是我的代码:

/*Basic calculator.*/
package calculator;

import java.util.Scanner;

public class no_gui {

public static void main(String[] args) {

System.out.println("What math operation do you want to do?");
System.out.println("1. Addition, 2. Subtraction, 3. Multipilcation, 4. Division");
System.out.println("Enter number to corresponding math operation.");

Scanner calc = new Scanner(System.in);
double choice = calc.nextDouble();
double add1, add2, addAnswer, sub1, sub2, subAnswer, mult1, mult2, multAnswer, div1,     div2, divAnswer;

if (choice == 1){
System.out.println("Addition");
System.out.println("Enter first number: ");
add1 = calc.nextDouble();
System.out.println("Enter second number: ");
add2 = calc.nextDouble();
addAnswer = add1 + add2;
System.out.println("Your answer is " + addAnswer);

if(choice == 2){
System.out.println("Subtraction");
System.out.println("Enter first number: ");
sub1 = calc.nextDouble();
System.out.println("Enter second number: ");
sub2 = calc.nextDouble();
subAnswer = sub1 + sub2;
System.out.println("Your answer is " + subAnswer);

if(choice == 3){
System.out.println("Multipilcation");
System.out.println("Enter first number: ");
mult1 = calc.nextDouble();
System.out.println("Enter second number: ");
mult2 = calc.nextDouble();
multAnswer = mult1 + mult2;
System.out.println("Your answer is " + multAnswer);

if(choice == 4){
System.out.println("Divsion");
System.out.println("Enter first number: ");
div1 = calc.nextDouble();
System.out.println("Enter second number: ");
div2 = calc.nextDouble();
divAnswer = div1 + div2;
System.out.println("Your answer is " + divAnswer);
}else{
System.out.println("Re-run to use again.");
}
}
} 
}
}

}

每次我运行它并选择添加它都会在if语句中执行任何操作。但是当我尝试减法时,它只打印出2并终止程序。它以前工作但今天它停止了。我甚至尝试了不同的编码方法,但它仍然不起作用。

编辑:感谢所有回复的人。我的主要问题已得到解决。但是我也对switch语句有这个问题,我在主要问题中忘了提到它。我必须把所有东西都放在一边吗这会导致问题吗?

5 个答案:

答案 0 :(得分:3)

您已嵌套if子句。查看您的代码并从逻辑上考虑它。

if (choice == 1) {
   //add 
   if (choice == 2) {
     //subtract
   } 

}

您永远不会输入减法或任何其他选项,因为外部choice == 1永远不会成立。请改用此结构。

if (choice == 1) {
   //add     
}else if (choice == 2) {
     //subtract
   } 

或者更好,使用切换器声明。下面给出一个例子:

 int month = 8;
        String monthString;
        switch (month) {
            case 1:  monthString = "January";
                     break;
            case 2:  monthString = "February";
                     break;
            case 3:  monthString = "March";
                     break;
            case 4:  monthString = "April";
                     break;
            case 5:  monthString = "May";
                     break;
            case 6:  monthString = "June";
                     break;
            case 7:  monthString = "July";
                     break;
            case 8:  monthString = "August";
                     break;
            case 9:  monthString = "September";
                     break;
            case 10: monthString = "October";
                     break;
            case 11: monthString = "November";
                     break;
            case 12: monthString = "December";
                     break;
            default: monthString = "Invalid month";
                     break;
        }

示例来源:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

答案 1 :(得分:2)

您的问题是您对choice的检查都是嵌套的:

if (choice == 1){
    // ...
    if(choice == 2){
        // ...
        if(choice == 3){
            // ...
            if(choice == 4){
                // ...
            }else{
                System.out.println("Re-run to use again.");
            }
        }
    } 
}

如果choice为1,则执行该块。如果它不是1,那么应该执行的块都是无法访问的。

尝试重新安排这样的程序:

if (choice == 1){
    // ...
} else if(choice == 2){
    // ...
} else if(choice == 3){
    // ...
} else if(choice == 4){
    // ...
} else {
    System.out.println("Re-run to use again.");
}

答案 2 :(得分:1)

如果您点击"格式代码"您的问题将是不言而喻的。在你的IDE中......

您(不恰当地)嵌套了if块,如下所示:

if (choice == 1) {
    // do addition
    if (choice == 2) {
        // etc

显然,如果选择不是1,则永远不会输入相应的if块。

相反,让if阻止兄弟姐妹:

if (choice == 1) {
    // do addition
} else if (choice == 2) {
    // etc

答案 3 :(得分:0)

因为您没有正确关闭if块。你应该:

    if (choice == 1) {
        System.out.println("Addition");
        System.out.println("Enter first number: ");
        add1 = calc.nextDouble();
        System.out.println("Enter second number: ");
        add2 = calc.nextDouble();
        addAnswer = add1 + add2;
        System.out.println("Your answer is " + addAnswer);
    } else if (choice == 2) {
        System.out.println("Subtraction");
        System.out.println("Enter first number: ");
        sub1 = calc.nextDouble();
        System.out.println("Enter second number: ");
        sub2 = calc.nextDouble();
        subAnswer = sub1 + sub2;
        System.out.println("Your answer is " + subAnswer);
    } else if (choice == 3) {
        System.out.println("Multipilcation");
        System.out.println("Enter first number: ");
        mult1 = calc.nextDouble();
        System.out.println("Enter second number: ");
        mult2 = calc.nextDouble();
        multAnswer = mult1 + mult2;
        System.out.println("Your answer is " + multAnswer);
    } else if (choice == 4) {
        System.out.println("Divsion");
        System.out.println("Enter first number: ");
        div1 = calc.nextDouble();
        System.out.println("Enter second number: ");
        div2 = calc.nextDouble();
        divAnswer = div1 + div2;
        System.out.println("Your answer is " + divAnswer);
    } else {
        System.out.println("Re-run to use again.");
    }

答案 4 :(得分:0)

在这种情况下,最好使用开关/外壳:

    import java.util.Scanner;

public class no_gui {

public static void main(String [] args){

Scanner calc = new Scanner(System.in);
//double choice = calc.nextDouble();
int choice = 0;
boolean getOut=false;
double add1, add2, addAnswer, sub1, sub2, subAnswer, mult1, mult2, multAnswer, div1,     div2, divAnswer;
while(!getOut)
{

    System.out.println("What math operation do you want to do?");
    System.out.println("1. Addition, 2. Subtraction, 3. Multipilcation, 4. Division, else: stop");
    System.out.println("Enter number to corresponding math operation:");
    choice = 0;
    choice = calc.nextInt();

    switch(choice)
    {
    case 1: 
        System.out.println("Addition");
        System.out.println("Enter first number: ");
        add1 = calc.nextDouble();
        System.out.println("Enter second number: ");
        add2 = calc.nextDouble();
        addAnswer = add1 + add2;
        System.out.println("Your answer is " + addAnswer);break;
    case 2:
        System.out.println("Subtraction");
        System.out.println("Enter first number: ");
        sub1 = calc.nextDouble();
        System.out.println("Enter second number: ");
        sub2 = calc.nextDouble();
        subAnswer = sub1 - sub2;
        System.out.println("Your answer is " + subAnswer);break;
    case 3:
        System.out.println("Multipilcation");
        System.out.println("Enter first number: ");
        mult1 = calc.nextDouble();
        System.out.println("Enter second number: ");
        mult2 = calc.nextDouble();
        multAnswer = mult1 + mult2;
        System.out.println("Your answer is " + multAnswer);break;
    case 4:
        System.out.println("Divsion");
        System.out.println("Enter first number: ");
        div1 = calc.nextDouble();
        System.out.println("Enter second number: ");
        div2 = calc.nextDouble();
        divAnswer = div1 + div2;
        System.out.println("Your answer is " + divAnswer);break;
    default:System.out.println("Good Bye");getOut = true;break;
    }

}
}}