2014-01-17 39 views
4

我对编程比较陌生,最近开始学习Java以进入Android编程。我认为我会创建一个非常简单的计算器来练习,但似乎我的if语句不起作用。Java计算器不执行if语句

import java.util.Scanner; 

public class Calculator { 

    public static void main(String[] args) { 
     //Create new scanner object 
     Scanner numInput = new Scanner(System.in); 

     //Enter first number 
     System.out.println("Please enter the first number: "); 
     int num1 = numInput.nextInt(); 

     //Enter the second number 
     System.out.println("Please enter the second number: "); 
     int num2 = numInput.nextInt(); 

     //Choose the operation to perform (+,-,*,/) 
     System.out.println("What operation would you like to do?"); 
     System.out.println("Type \"+\" to add."); 
     System.out.println("Type \"-\" to subtract."); 
     System.out.println("Type \"*\" to multiply."); 
     System.out.println("Type \"/\" to divide."); 
     String opChoice = numInput.nextLine(); 


     //Add 
     if (opChoice.equals("+")) { 
      int ans = num1 + num2; 
      System.out.println("Adding " + num2 + " to " + num1 + " equals " + ans + "."); 
     } 

     //Subtract 
     else if (opChoice.equals("-")) { 
      int ans = num1 - num2; 
      System.out.println("Subtracting " + num2 + " from " + num1 + " equals " + ans + "."); 
     } 

     //Multiply 
     else if (opChoice.equals("*")) { 
      int ans = num1 + num2; 
      System.out.println("Multiplying " + num2 + " with " + num1 + " equals " + ans + "."); 
     } 

     //Divide 
     else if (opChoice.equals("/")) { 
      int ans = num1 + num2; 
      System.out.println("Dividing " + num1 + " by " + num2 + " equals " + ans + "."); 
     } 

    } 

} 

我正在使用Eclipse IDE,它运行良好,直到它询问要执行哪个操作。它会显示选项,但不会让我输入任何东西(我已经用5乘以2来测试它)。

我搜索了类似的问题,并尝试了他们的建议,但它似乎仍然不起作用。我很感激任何帮助,我认为这可能只是一些简单的错误,所以我很抱歉,如果这看起来像一个愚蠢的问题!

编辑:感谢您的快速反应,伙计们!我很感激。是的,我固定了乘法和除法。 :)

+0

,我还没有碰到但有趣的问题。你每天学习新的东西! – csmckelvey

回答

5

的问题是,nextInt()不消耗(不读)新行字符(你输入时按[Enter]键)。要解决这个问题的方法之一是每个nextInt()后调用nextLine()

//Enter first number 
System.out.println("Please enter the first number: "); 
int num1 = numInput.nextInt(); 
numInput.nextLine(); // Add this 

//Enter the second number 
System.out.println("Please enter the second number: "); 
int num2 = numInput.nextInt(); 
numInput.nextLine(); // Add this 

另一种方法来解决这个问题,将与nextLine()阅读数(返回String),然后将其解析到一个int

int num1 = Integer.parseInt(numInput.nextLine()); 

您将不需要添加额外的nextLine(),因为已换行的nextLine()正在消耗换行符。

另外,正如@sotondolphin建议的,您可能想要检查您的*/操作。

+0

您的解决方案解决了它,谢谢! 5分钟后我可以接受你的答案。 ;) – Qwurticus

-1

下面的代码不会增加,但不是预期的乘法和除法。你能检查一下源代码吗?

//Multiply 
    else if (opChoice.equals("*")) { 
     int ans = num1 + num2; 
     System.out.println("Multiplying " + num2 + " with " + num1 + " equals " + ans + "."); 
    } 

    //Divide 
    else if (opChoice.equals("/")) { 
     int ans = num1 + num2; 
     System.out.println("Dividing " + num1 + " by " + num2 + " equals " + ans + "."); 
    } 

} 
3

问题是,当调用numInput.nextInt();时,会得到输入的数字...但它会离开换行符(\n)。您拨打numInput.nextLine();然后获得一个空字符串。

更换与numInput.next()该呼叫将解决这个问题,因为它有一个稍微不同的行为:

下一个公开的字符串()

查找并从该扫描仪返回下一个完整标记。完整的令牌前后有与分隔符模式匹配的输入。

缺省的分隔符模式是空白,其中包括\n,什么是输入流中输入的操作后(使用*为例)现在\n*\n