【问题标题】:Astronomy Helper Java Menu Driven Program with a Loop带有循环的天文助手 Java 菜单驱动程序
【发布时间】:2013-11-14 18:54:40
【问题描述】:

我正在尝试制作一个程序,让用户选择一颗行星,它会显示该行星的特征。

只要他们确实选择“5”结束程序并验证选择为 1-5,我想将其循环回菜单的功能。

但是我有一些错误我不知道如何摆脱。

import java.util.Scanner;

public class AstronomyHelper
{

   public static void main(String[] args)
   {

   //Declare a variable to hold the user's menu selection
   int menuSelection;

   //Declare a variable to hold the different planets
   string Mercury, Venus, Earth, Mars;


   //Create a scanner object for the keyboard input
   Scanner keyboard = new Scanner(System.in);


      do 
      {
         //Display the menu and get the user's selection
         displayMenu(menuSelection);


         //Display the information for the user's selection
         switch(menuSelection)
         {

            case 1: 
                  System.out.println("\t\t\t\t\t\t\t\t\t\t MERCURY");
                  System.out.println("_________________________________________________________");

                  System.out.println("Average Distance from the sun:  57.9 million kilomenters");
                  System.out.println("Mass:  3.31 x 10^23 kg");
                  System.out.println("Surface Temperature:  -173 to 430 degrees Celsius");

                  break;


           case 2: 
                  System.out.println("\t\t\t\t\t\t\t\t\t\t VENUS");
                  System.out.println("_________________________________________________________");

                  System.out.println("Average Distance from the sun:  108.2 million kilometers");
                  System.out.println("Mass:  4.87 x 10^24 kg");
                  System.out.println("Surface Temperature:  472 degrees Celsius");

                  break;

           case 3: 
                  System.out.println("\t\t\t\t\t\t\t\t\t\t EARTH");
                  System.out.println("_________________________________________________________");

                  System.out.println("Average Distance from the sun:  149.6 million kilometers");
                  System.out.println("Mass:  5.967 x 10^24 kg");
                  System.out.println("Surface Temperature:  -50 to 50 degrees Celsius");

                  break;

           case 4: 
                  System.out.println("\t\t\t\t\t\t\t\t\t\t MARS");
                  System.out.println("_________________________________________________________");

                  System.out.println("Average Distance from the sun:  227.9 million kiometers");
                  System.out.println("Mass:  0.6424 x 10^24 kg");
                  System.out.println("Surface Temperature:  -140 to 20 degrees Celsius");

                  break;

         }

       while(menuSelection !=5)
       }       



         //Call the displayMenu method to display the menu options and get the user's selection
         public displayMenu()
         {
         System.out.println("Please select a planet to view details about it's: ");
         System.out.println("average distance from the sun, mass, and surface temperature.");
         System.out.println("-------------------------------------------------------------");
         System.out.println("1. Mercury");
         System.out.println("2. Venus");
         System.out.println("3. Earth");
         System.out.println("4. Mars");
         System.out.println("5. EXIT the program");
         System.out.println("Enter your selection:  ");

         menuSelection = keyboard.nextInt();


            //Validate the menu selection
            while (menuSelection < 1 || menuSelection > 5)
            {
               System.out.print("This is an invalid selection.");
               System.out.print("Enter a selection from 1-5: ");

               menuSelection = keyboard.nextInt();
            }
          }
}

【问题讨论】:

  • 哪些错误是您无法摆脱的?

标签: java menu do-while


【解决方案1】:
  public displayMenu() // there must be a return type and int parameter

如果你在 displayMenu() 方法中访问 Scanner 对象,你必须在 main() 之外声明它们。

而且你没有正确关闭 do-while 循环。

【讨论】:

    【解决方案2】:

    声明的键盘和菜单选择。正确关闭您的 do-while 并将 return-statement 添加到您的 displayMenu() 方法。创建了一个 printPlanet(..) 方法,这样您就不需要复制/粘贴所有相同的数据。工作示例:

    import java.util.Scanner;
    
    public class AstronomyHelper {
    
    public static void main(String[] args) {
        int menuSelection = 0;
        do {
            // Display the menu and get the user's selection
            menuSelection = displayMenu();
            // Display the information for the user's selection
            switch (menuSelection) {
            case 1:
                printPlanet("MERCURY", "57.9", "3.31 x 10^23", "-173 to 430");
                break;
    
            case 2:
                printPlanet("VENUS", "108.2", "4.87 x 10^24", "472");
                break;
    
            case 3:
                printPlanet("EARTH", "149.6", "5.967 x 10^24", "-50 to 50");
                break;
    
            case 4:
                printPlanet("MARS", "227.9", "0.6424 x 10^24", "-140 to 20");
                break;
            }
        }
        while (menuSelection != 5);
    } // Call the displayMenu method to display the menu options and get the
    
    // user's selection
    public static int displayMenu() {
        System.out.println("Please select a planet to view details about it's: ");
        System.out.println("average distance from the sun, mass, and surface temperature.");
        System.out.println("_________________________________________________________");
        System.out.println("1. Mercury");
        System.out.println("2. Venus");
        System.out.println("3. Earth");
        System.out.println("4. Mars");
        System.out.println("5. EXIT the program");
        System.out.println("Enter your selection:  ");
    
        Scanner keyboard = new Scanner(System.in);
        int menuSelection = keyboard.nextInt();
    
        // Validate the menu selection
        while (menuSelection < 1 || menuSelection > 5) {
            System.out.print("This is an invalid selection.");
            System.out.print("Enter a selection from 1-5: ");
    
            menuSelection = keyboard.nextInt();
        }
        return menuSelection;
    }
    public static void printPlanet(String planet, String distanceToSun, String mass, String degrees) {
        System.out.println("\t\t\t " + planet);
        System.out.println("_________________________________________________________");
        System.out.println("Average Distance from the sun: " + distanceToSun + " million kiometers");
        System.out.println("Mass: " + mass + " kg");
        System.out.println("Surface Temperature: " + degrees + " degrees Celsius");
        System.out.println("_________________________________________________________");
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-03
      • 2015-07-20
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2020-03-04
      相关资源
      最近更新 更多