0

创建一个将执行以下操作的 java 程序:

a) 从键盘读取三个输入,

• 两个输入数字,每个数字都是一个数字 (0…9)

• 一个字符代表五种运算之一:+(加法)、-(减法)、*(乘法)、/(除法)和 ^(求幂)

b) 以简单的英文输出操作描述以及数值结果

import java.util.Scanner;

public class EnglishCalc {

       public static void main(String[] args)
       {
            Scanner input = new Scanner(System.in);

            System.out.println("Enter first number");
            int number1 = input.nextInt();          

            System.out.println("Enter second number");
            int number2 = input.nextInt();

            System.out.println("Enter operation: +,-,*,/,^");
            String operation = input.next();

            int output = 0;

           if(number1 < 0 || number1 > 9 || number2 < 0 || number2 > 9) {
                System.out.println("Number should be between 0 and 10");
           } 
              else if (operation.equals("+"))
           {
               output = number1 + number2;
               System.out.println("Sum of "+number1+" and "+number2+" is: " +output);
           }
               else if (operation.equals("-"))
           {
               output = number1 - number2;
               System.out.println("Subtraction of "+number2+" from "+number1+" is: " +output);
           }
               else if (operation.equals("*"))
           {   
              output = number1 * number2;
              System.out.println("Product of "+number1+" and "+number2+" is: " +output);
           }
               else if (operation.equals("/"))
           {
               if(number2 == 0) {
                   System.out.println("You cannot divide by 0");
                } else {
                output = number1/number2;
                System.out.println("Division of "+number1+" by "+number2+" is: " +output);
                }
           }
               else if(operation.equals('^'))
           {
                output = Math.pow((double)number1 , (double)number2);
                System.out.println("Value of "+num1+" raised to power of "+num2+" is: " +output);
           } else {
               System.out.println("Invalid input");
           }
    }
}

战俘。我试过铸造不会工作。如果我不投,它不会接受 int。必须是双倍的。

4

1 回答 1

3

利用

s1.equals(s2)

比较字符串,而不是使用:

s1 == s2

发生这种情况是因为==用于比较对象引用(如果它们是同一个对象),因此它不会比较该对象的“包含”,在本例中为字符串。

编辑

要打印“单词”中的每个数字,您可以使用数组:

String[] numbers = {"zero", "one", "two", ... };

然后将它们用作:

System.out.println(numbers[2] + " plus " + numbers[5] + ...);
于 2013-10-26T18:49:33.847 回答