我在为这个项目使用while循环时遇到了麻烦。我需要在每次操作后显示选项。当使用按下任何按钮但1,2,3,4,5时程序终止。我想不出任何其他方法来做这个没有while循环它似乎是正确的方式。
import java.util。*;
public class CollectionsApplication
{
public static void main(String[] args)
{
//Map for employees to be stored and retrieved
Map<String, Employee> myMap = new TreeMap<>();
createMap(myMap);
}
//method for entering NEW employees
private static void createMap(Map<String, Employee> map)
{
// Scanner Object for user to input info
Scanner scanner = new Scanner(System.in);
//List options - loop these options after each operation
System.out.println("Select an option from the choices below \n"
+ "1 - Add an employee \n"
+ "2 - Change an employees salary \n"
+ "3 - Delete an employee \n"
+ "4 - List an employee \n"
+ "5 - List all employees \n"
+ "Enter any other number to exit.");
int inputOption = scanner.nextInt();
//Check to see if input equals and integer
//Actions for each number
if(inputOption == 1)
{
//add employee
//create new employee
Employee x = new Employee(null,null,null,null);
x.getEmpId(null);
x.getLastName(null);
x.getFirstName(null);
x.getAnnualSalary(null);
//Prompt user for each
System.out.println("Enter employee Id number:");
String inputId = scanner.nextLine();
scanner.nextLine();
System.out.println("Enter employees last name:");
String inputLastName = scanner.nextLine();
System.out.println("Enter employees first name:");
String inputFirstName = scanner.nextLine();
System.out.println("Enter employees salary:");
String inputSalary = scanner.nextLine();
Double inSalary = Double.parseDouble(inputSalary);
//set values
x.setEmpId(inputId);
x.setLastName(inputLastName);
x.setFirstName(inputFirstName);
x.setAnnualSalary(inSalary);
map.put(inputId , x );
}
else if (inputOption == 2)
{
//get employee id(key).
//set new salary
}
else if (inputOption == 3)
{
//get employee id
//remove employee
}
else if (inputOption == 4)
{
//get employee id
//list employee properties
}
else if (inputOption == 5)
{
//List all employees
}
else
{
//change input to integer and check to see if it equals integer entered
System.exit(0);
}
}
}
答案 0 :(得分:0)
您好,您可以尝试使用此代码,
do{
// Put the user input information here
}while(1<=input<=5);