好的,我迷上了这个。这是我的代码:

public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    double weight;
    double newWeight;
    int planet;

    System.out.println("1. Mercury");
    System.out.println("2. Venus");
    System.out.println("3. Earth");
    System.out.println("4. Mars");
    System.out.println("5. Jupiter");
    System.out.println("6. Saturn");
    System.out.println("7. Uranus");
    System.out.println("8. Neptune");
    System.out.println("9. Pluto");
    System.out.println("                   ");
    System.out.println("Select a planet from above: ");
    planet = input.nextInt();
    System.out.println("Enter your weight in pounds: ");
    weight = input.nextInt();

    switch(newWeight) {
        case 1: (3.724 / 9.8) * weight = newWeight; break;
        case 2: (8.918 / 9.8) * weight = newWeight; break;
        case 3: (9.8 / 9.8) * weight = newWeight; break;
        case 4: (3.724 / 9.8) * weight = newWeight; break;
        case 5: (24.892 / 9.8) * weight = newWeight; break;
        case 6: (10.584 / 9.8) * weight = newWeight; break;
        case 7: (8.918 / 9.8) * weight = newWeight; break;
        case 8: (11.662 / 9.8) * weight = newWeight; break;
        case 9: (1.622 / 9.8) * weight = newWeight; break;
    }
    System.out.print("Your weight on " + planet + "is: " + newWeight);
}


编辑*****:确定找出了问题所在,weight是int,newWeight是double。但是现在最后一行"System.out.print("your weight on " + planet + "is: " + newWeight);

它说"variable newWeight might not have been initialized".如果不是一件事,那就是另一件事。

最佳答案

您对变量newWeight的赋值是错误的!

您应该解决以下问题:

newWeight = ( ... ) * weight; break;

IE. case 1: newWeight = (3.724 / 9.8) * weight; break;


在Java中,将接收值的变量必须位于运算符equal ( = )的左侧

关于java - 必需:变量;找到:开关中的值(value),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32663514/

10-10 13:53