2

我正在使用开关来计算两个数字,但是当我尝试执行任何类型的运算符时,我都会不断得到0.

//imports
import java.util.Scanner;
public class Calculator {
      public static void main (String[] args) {
//get first number
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter the first number: > ");
        int num1 = scan.nextInt();
// get second number
        System.out.println("Please enter the second number: > ");
        int num2 = scan.nextInt();
// what the user wants to do
        System.out.println("Please enter the method you would like to use "
                + "(+ , - , * , / , ^ ) > " );
        String method = scan.next();
        char operation = method.charAt(0);
        String output = "";
//switch for first number
        switch(num1) {
                case 0: output+= "zero";
                        break;
                case 1: output+= " one";
                        break;
                case 2: output+= " two";
                        break;
                case 3: output+= " three";
                        break;
                case 4: output+= " four";
                        break;
                case 5: output+= " five";
                        break;
                case 6: output+= " six";
                        break;
                case 7: output+= " seven";
                        break;
                case 8: output+= " eight";
                        break;
                case 9: output+= " nine";
                        break;
                case 10: output+= " ten";
                        break;
}
// operation the user wants to use
      switch (operation){
          case '+' : output += " plus";
                     break;
          case '-' : output += " minus";
                     break;
          case '*' : output += " times";
                     break;
          case '/' : output += " divided by";
                     break;
          case '^' : output += " to the power of";
                     break;
}
//switch for second number
       switch (num2) {
               case 0: output += " zero";
                       break;
               case 1: output += " one";
                       break;
               case 2: output += " two";
                       break;
               case 3: output += " three";
                       break;
               case 4: output += " four";
                       break;
               case 5: output += " five";
                       break;
               case 6: output += " six";
                       break;
               case 7: output += " seven";
                       break;
               case 8: output += " eight";
                       break;
               case 9: output += " nine";
                       break;
               case 10: output += " ten";
                       break;
// calculate the answer
        }
          int answer = 0 ;
       switch (answer) {
           case '+' : answer = (answer + (num1 + num2));
               break;
           case '-' : answer = (answer + (num1 - num2));
               break;
           case '*' : answer = (answer + (num1 * num2));
               break;
           case '/' : answer = (answer + (num1 / num2));
               break;
           case '^' : answer = (answer + (int) (Math.pow(num1,num2) ));
               break;
       }
       System.out.println(output + " equals " + answer );
    }
}

我很确定我的问题出在程序的底部,具有实际功能

 int answer = 0 ;
       switch (answer) {

从那个区域的某个地方开始,只是很难弄清楚。也许其他人可以就我做错了什么提供意见

提前感谢您的帮助

4

5 回答 5

6
   int answer = 0 ;
   switch (answer) {

没有任何意义,您正在打开0(硬编码)并且您没有相关案例,

operation改为开启

于 2013-10-24T22:58:12.930 回答
3

你可能想做

switch (operation) {

代替

switch (answer) {
于 2013-10-24T23:00:07.050 回答
0

这是正在发生的事情:

您分配0answer. 然后:

  • 如果answer'+',做第一件事
  • 如果answer是的话'-',做一些其他的事情
  • 如果answer'*',做第三件事
  • ...

显然,您可以看到这是荒谬的;唯一可能触发的情况是您将'\0'(空字符)作为其中一种情况。

看起来你想要打开operation,而不是answer

于 2013-10-24T23:05:53.917 回答
0

你是说:

...
int answer = 0;
if(answer == '+'){
    ...
}
else if(answer ==  '-'){
    ...
}
else if(answer == '*'){
    ...
} ... 

我想这应该可行:

   int answer = 0 ;
   switch (operation) {
       case '+' : answer = num1 + num2;
           break;
       case '-' : answer = num1 - num2;
           break;
       case '*' : answer = num1 * num2;
           break;
       case '/' : answer = num1 / num2;
           break;
       case '^' : answer = (int) Math.pow(num1,num2) ;
           break;
   }
于 2013-10-24T23:06:41.297 回答
-1
public class Calculator {
    public static void main(String args []){

        int x=10,y=5,add,sub,div,mul;

        switch (4) {
        case 1:
            add=x+y;
            System.out.print("add :"+add);
            break;
        case 2:
            sub=x-y;
            System.out.print("sub :"+sub);
            break;
        case 3:
            mul=x*y;
            System.out.print("mul:"+mul);
            break;
        case 4:
            div=x/y;
            System.out.print("div:"+div);           
        default:
            System.out.print("invailid");
        }
    }
}
于 2014-07-13T14:06:59.937 回答