我試圖讓一個程序,讓用戶選擇一個行星,它會顯示有關該星球的特徵。天文學助手Java菜單驅動程序與循環
我想加入循環回菜單的能力,只要他們選擇了「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();
}
}
}
什麼是您無法擺脫的那些錯誤? – asgs