创建一个将执行以下操作的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。必须加倍。
答案 0 :(得分:3)
使用
s1.equals(s2)
比较字符串,而不是使用:
s1 == s2
这是因为==
用于比较对象引用(如果它们是同一个对象),因此它不会比较该对象的“包含”,在本例中为String。
修改强>
要打印'words'中的每个数字,您可以使用数组:
String[] numbers = {"zero", "one", "two", ... };
然后将它们用作:
System.out.println(numbers[2] + " plus " + numbers[5] + ...);