You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java嵌套switch语句输出错误求助(行星体重计算程序)

行星体重计算程序嵌套switch语句错误分析与修复

我正在完成CS101课程的作业,要求使用嵌套switch语句结合charAt方法判断用户选择的行星,计算并输出对应行星上的体重。但嵌套switch语句未按预期工作,出现以下错误:

  • 选择Sun时,同时输出Sun和Uranus的体重结果;
  • 选择Moon时,同时输出Moon和Uranus的体重结果;
  • 选择Saturn时,同时输出Saturn、Mars和Uranus的体重结果;
  • 选择Mercury时,同时输出Mercury和Uranus的体重结果;
  • 选择Mars时,同时输出Mars和Mercury的体重结果;
  • Uranus、Venus、Jupiter和Neptune的输出正常。

原程序代码:

import java.io.*;
import java.util.*;

public class Planets 
{

    public static void main(String[] args) throws IOException
    {
        Scanner in;
        in = new Scanner(System.in);
        boolean runAgain = true;
        System.out.println("Starting program...\n\n");
        
        double weight = getWeight();
        
        while(runAgain)
        {
            String planet = getPlanet();
            calculateWeight(weight, planet);
            
            System.out.println("\nWould you like to see your weight on a different planet? (Yes or No)");
            String input = in.nextLine();
            if(input.charAt(0)=='N' || input.charAt(0)=='n')
            {
                runAgain = false;
            }
        }
            

    } //end main
    
    public static double getWeight() throws IOException
    { //Prompts user to enter their weight
        Scanner in;
        in = new Scanner(System.in);
        System.out.println("Greetings! This program calculates how much you would weigh on other planets.\n\nPlease enter your weight (lbs.): ");
        double weight = in.nextDouble();
        return weight;
    }
    
    public static String getPlanet() throws IOException
    { //Prompts user to enter their planet
        Scanner in;
        in = new Scanner(System.in);
        System.out.println("\nChoose your planet:\nSun\nMoon\nSaturn\nMercury\nMars\nUranus\nVenus\nJupiter\nNeptune");
        String planet = in.nextLine();
        return planet;
    }
    
    public static void calculateWeight(double weight, String planet) throws IOException
    { //Outputs the weight of the given planet ; takes two parameters weight and planet
        switch(planet.charAt(0))
        {
            case 's':
            case 'S':
                switch(planet.charAt(1))
                {
                case 'u':
                case 'U':
                    System.out.println("Your weight on the Earth is " + weight + " lbs.\nYour weight on the Sun is " + 27.94*weight);
                    break;
                case 'a':
                case 'A':
                    System.out.println("Your weight on the Earth is " + weight + " lbs.\nYour weight on Saturn is " + .91*weight);
                    break;
                }
            case 'm':
            case 'M':
            switch(planet.charAt(1))
            {
                case 'o':
                case 'O':
                    System.out.println("Your weight on the Earth is " + weight + " lbs.\nYour weight on the Moon is " + .17*weight);
                    break;
                case 'e':
                case 'E':
                    System.out.println("Your weight on the Earth is " + weight + " lbs.\nYour weight on Mercury is " + .37*weight);
                    break;
                case 'a':
                case 'A':
                    System.out.println("Your weight on the Earth is " + weight + " lbs.\nYour weight on Mars is " + .38*weight);
                    break;
            } 
            case 'u':
            case 'U':
                System.out.println("Your weight on Earth is " + weight + " lbs.\nYour weight on Uranus is " + .88*weight);
                break;
            case 'v':
            case 'V':
                System.out.println("Your weight on Earth is " + weight + " lbs.\nYour weight on Venus is " + .90*weight);
                break;
            case 'j':
            case 'J':
                System.out.println("Your weight on Earth is " + weight + " lbs.\nYour weight on Jupiter is " + 2.64*weight);
                break;
            case 'n':
            case 'N':
                System.out.println("Your weight on Earth is " + weight + " lbs.\nYour weight on Neptune is " + 1.12*weight);
                break;
        } 
    } 
    
} //end class

错误原因分析

问题出在switch语句的贯穿(fall-through)特性:

  • 外层switch中,case 's'/'S'执行完内部嵌套switch后,没有添加break语句,导致程序会继续执行后续的case 'm'/'M'代码块;
  • 同样,case 'm'/'M'执行完内部switch后也没有break,会继续执行case 'u'/'U'的代码块;
  • 这就解释了为什么选择Sun、Moon等行星时,会额外输出后续case对应的结果(比如Uranus)。

修复方案

在case 's'/'S'的嵌套switch结束后添加break,在case 'm'/'M'的嵌套switch结束后也添加break,阻止程序贯穿到下一个case。

修正后的代码

import java.io.*;
import java.util.*;

public class Planets 
{

    public static void main(String[] args) throws IOException
    {
        Scanner in;
        in = new Scanner(System.in);
        boolean runAgain = true;
        System.out.println("Starting program...\n\n");
        
        double weight = getWeight();
        
        while(runAgain)
        {
            String planet = getPlanet();
            calculateWeight(weight, planet);
            
            System.out.println("\nWould you like to see your weight on a different planet? (Yes or No)");
            String input = in.nextLine();
            if(input.charAt(0)=='N' || input.charAt(0)=='n')
            {
                runAgain = false;
            }
        }
            

    } //end main
    
    public static double getWeight() throws IOException
    { //Prompts user to enter their weight
        Scanner in;
        in = new Scanner(System.in);
        System.out.println("Greetings! This program calculates how much you would weigh on other planets.\n\nPlease enter your weight (lbs.): ");
        double weight = in.nextDouble();
        return weight;
    }
    
    public static String getPlanet() throws IOException
    { //Prompts user to enter their planet
        Scanner in;
        in = new Scanner(System.in);
        System.out.println("\nChoose your planet:\nSun\nMoon\nSaturn\nMercury\nMars\nUranus\nVenus\nJupiter\nNeptune");
        String planet = in.nextLine();
        return planet;
    }
    
    public static void calculateWeight(double weight, String planet) throws IOException
    { //Outputs the weight of the given planet ; takes two parameters weight and planet
        switch(planet.charAt(0))
        {
            case 's':
            case 'S':
                switch(planet.charAt(1))
                {
                case 'u':
                case 'U':
                    System.out.println("Your weight on the Earth is " + weight + " lbs.\nYour weight on the Sun is " + 27.94*weight);
                    break;
                case 'a':
                case 'A':
                    System.out.println("Your weight on the Earth is " + weight + " lbs.\nYour weight on Saturn is " + .91*weight);
                    break;
                }
                // 添加break阻止贯穿到case 'm'
                break;
            case 'm':
            case 'M':
                switch(planet.charAt(1))
                {
                    case 'o':
                    case 'O':
                        System.out.println("Your weight on the Earth is " + weight + " lbs.\nYour weight on the Moon is " + .17*weight);
                        break;
                    case 'e':
                    case 'E':
                        System.out.println("Your weight on the Earth is " + weight + " lbs.\nYour weight on Mercury is " + .37*weight);
                        break;
                    case 'a':
                    case 'A':
                        System.out.println("Your weight on the Earth is " + weight + " lbs.\nYour weight on Mars is " + .38*weight);
                        break;
                } 
                // 添加break阻止贯穿到case 'u'
                break;
            case 'u':
            case 'U':
                System.out.println("Your weight on Earth is " + weight + " lbs.\nYour weight on Uranus is " + .88*weight);
                break;
            case 'v':
            case 'V':
                System.out.println("Your weight on Earth is " + weight + " lbs.\nYour weight on Venus is " + .90*weight);
                break;
            case 'j':
            case 'J':
                System.out.println("Your weight on Earth is " + weight + " lbs.\nYour weight on Jupiter is " + 2.64*weight);
                break;
            case 'n':
            case 'N':
                System.out.println("Your weight on Earth is " + weight + " lbs.\nYour weight on Neptune is " + 1.12*weight);
                break;
        } 
    } 
    
} //end class

内容的提问来源于stack exchange,提问作者b-rad90

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 13:15:34