我遇到的一个问题是我无法使用Camel案例。我必须使用Pascal。我在纠正下面的代码时发现,当我将int更改为像我们的教师想要的那样时,我得到一个无法打开double类型的值。
我正在尝试寻找其他东西而不是可以让我获得相同功能的开关。任何帮助表示赞赏。谢谢!
import java.util.Random;
import java.util.Scanner;
public class CalculatorWithMethods {
// scanner object creation
static Scanner input = new Scanner(System.in);
boolean mainloop = true;
public static void main(String[] args) {
//Declaring variables
double res;
//calling the method
double choice = getMenuOption();
switch (choice) {
case 1:
{
//calling the method to get the operands
double operand1 = getOperand("What is the first number?");
double operand2 = getOperand("What is the second number?");
res = add(operand1, operand2);
System.out.println(operand1 + " + " + operand2 + " = " + res);
break;
}
case 2:
{
//calling the method to get the operands
double operand1 = getOperand("What is the first number?");
double operand2 = getOperand("What is the second number?");
res = subtract(operand1, operand2);
System.out.println(operand1 + " - " + operand2 + " = " + res);
break;
}
case 3:
{
//calling the method to get the operands
double operand1 = getOperand("What is the first number?");
double operand2 = getOperand("What is the second number?");
res = multiply(operand1, operand2);
System.out.println(operand1 + " * " + operand2 + " = " + res);
break;
}
case 4:
{
//calling the method to get the operands
double operand1 = getOperand("What is the first number?");
double operand2 = getOperand("What is the second number?");
if (operand2 == 0) {
System.out.println("The Second Number is Double.NAN.");
} else {
//calling the method to get the operands
operand1 = getOperand("What is the first number?");
operand2 = getOperand("What is the second number?");
res = divide(operand1, operand2);
System.out.println(operand1 + " / " + operand2 + " = " + res);
}
break;
}
case 5:
{
double operand1 = getOperand("What is the lower limit ?");
double operand2 = getOperand("What is the upper limit ?");
res = random(operand1, operand2);
System.out.println("The Random Number is :" + res);
break;
}
}
}
//This method will perform the add operation
public static double add(double operand1, double operand2) {
return operand1 + operand2;
}
//This method will perform the subtract operation
public static double subtract(double operand1, double operand2) {
return operand1 - operand2;
}
//This method will perform the multiply operation
public static double multiply(double operand1, double operand2) {
return operand1 * operand2;
}
//This method will perform the division operation
public static double divide(double operand1, double operand2) {
return operand1 / operand2;
}
//This method returns the random number
public static double random(double x, double y) {
Random generator = new Random();
return generator.nextInt((int)(y - x) + 1) + x;
}
//This method will get the operands entered by the user
public static double getOperand(String str) {
System.out.print(str);
double num = input.nextDouble();
return num;
}
//This method will display the menu
public static int getMenuOption() {
int choice;
// user interaction until quit performed.
while (true) {
// displaying menu
System.out.println("\nMenu\n1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Generate Random Number\n6. Quit");
// ask user choice
System.out.println("What would you like to do?");
choice = input.nextInt();
if (choice < 1 || choice > 5) {
System.out.println("** Invalid Choice **");
continue;
} else {
return choice;
}
}
}
}
答案 0 :(得分:0)
之后:
a.foo = 42
这样做:
a.foo=(42)
然后切换intCastedChoice。