Java 既然我能';你不用开关吗?

Java 既然我能';你不用开关吗?,java,Java,我遇到的一个问题是,我不能使用Camel case。我得用帕斯卡。我在更正下面的代码时发现,当我像我们的讲师所希望的那样将int改为double时,我得到了一个无法打开double类型值的值 我正在尝试寻找其他的东西来使用,而不是一个开关,可以让我得到同样的功能。感谢您的帮助。谢谢 import java.util.Random; import java.util.Scanner; public class CalculatorWithMethods { // scanner object

我遇到的一个问题是,我不能使用Camel case。我得用帕斯卡。我在更正下面的代码时发现,当我像我们的讲师所希望的那样将int改为double时,我得到了一个无法打开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;
        }
    }
}
import java.util.Random;
导入java.util.Scanner;
公共类计算器WithMethods{
//扫描仪对象创建
静态扫描仪输入=新扫描仪(System.in);
布尔mainloop=true;
公共静态void main(字符串[]args){
//声明变量
双res;
//调用方法
double-choice=getMenuOption();
开关(选择){
案例1:
{
//调用该方法以获取操作数
double operan1=getOperand(“第一个数字是什么?”);
double operans2=getOperand(“第二个数字是什么?”);
res=加法(操作数1,操作数2);
System.out.println(操作数1+“+”+操作数2+“=”+res);
打破
}
案例2:
{
//调用该方法以获取操作数
double operan1=getOperand(“第一个数字是什么?”);
double operans2=getOperand(“第二个数字是什么?”);
res=减法(操作数1,操作数2);
System.out.println(操作数1+“-”+操作数2+“=”+分辨率);
打破
}
案例3:
{
//调用该方法以获取操作数
double operan1=getOperand(“第一个数字是什么?”);
double operans2=getOperand(“第二个数字是什么?”);
res=乘法(操作数1,操作数2);
System.out.println(操作数1+“*”+操作数2+“=”+res);
打破
}
案例4:
{
//调用该方法以获取操作数
double operan1=getOperand(“第一个数字是什么?”);
double operans2=getOperand(“第二个数字是什么?”);
if(操作数2==0){
System.out.println(“第二个数字是Double.NAN”);
}否则{
//调用该方法以获取操作数
操作数1=GetOperator(“第一个数字是什么?”);
操作数2=GetOperator(“第二个数字是什么?”);
res=除法(操作数1,操作数2);
System.out.println(操作数1+“/”+操作数2+“=”+分辨率);
}
打破
}
案例5:
{
双操作数1=GetOperator(“下限是多少?”);
double operans2=getOperand(“上限是多少?”);
res=随机(操作数1、操作数2);
System.out.println(“随机数为:“+res”);
打破
}
}
}
//此方法将执行添加操作
公共静态双加(双操作数1,双操作数2){
返回操作数1+2;
}
//此方法将执行减法运算
公共静态双减法(双操作数1,双操作数2){
返回操作数1-操作数2;
}
//此方法将执行乘法运算
公共静态双乘法(双操作数1,双操作数2){
返回操作数1*2;
}
//此方法将执行除法运算
公共静态双除法(双操作数1,双操作数2){
返回操作数1/2;
}
//此方法返回随机数
公共静态双随机(双x,双y){
随机生成器=新随机();
返回生成器.nextInt((int)(y-x)+1)+x;
}
//此方法将获取用户输入的操作数
公共静态双GetOperator(字符串str){
系统输出打印(str);
double num=input.nextDouble();
返回num;
}
//此方法将显示菜单
公共静态int getMenuOption(){
智力选择;
//执行退出之前的用户交互。
while(true){
//显示菜单
System.out.println(“\n菜单\n1.加\n2.减\n3.乘\n4.除\n5.生成随机数\n6.退出”);
//询问用户选择
System.out.println(“您想做什么?”);
choice=input.nextInt();
如果(选项<1 | |选项>5){
System.out.println(“**无效选择**”);
持续
}否则{
回报选择;
}
}
}
}

在此之后:

//calling the method
double choice = getMenuOption();
这样做:

long intCastedChoice = (long)choice;

然后切换intCastedChoice。

您可能应该将其标记为Java。camel case或Pascal与它有什么关系?您在哪里将输入更改为双精度?如果输入最初是一个整数,为什么不能打开它?不清楚你在问什么。