用java构建计算器(带开关)

时间:2013-10-24 22:56:21

标签: java

我正在使用一个开关计算两个数字,但是当我尝试做任何类型的运算符时,我只是继续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) {

从该地区的某个地方开始,只是很难搞清楚。也许其他人可以就我的错误提供意见

提前感谢您提供任何帮助

5 个答案:

答案 0 :(得分:6)

   int answer = 0 ;
   switch (answer) {

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

启用operation而不是

答案 1 :(得分:3)

您可能想要

switch (operation) {

而不是

switch (answer) {

答案 2 :(得分:0)

以下是发生的事情:

您要将0分配给answer。然后:

  • 如果answer'+',请做第一件事
  • 如果answer'-',请执行其他操作
  • 如果answer'*',请做第三件事
  • ...
显然你可以看到这是荒谬的;可能触发的唯一情况是,如果您有'\0'(空字符)作为其中一个案例。

您似乎想要启用operation,而不是answer

答案 3 :(得分: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;
   }

答案 4 :(得分:-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");
        }
    }
}