如何使用while循环

狐狸

我在为这个项目使用while循环时遇到麻烦。我需要在每次操作后显示这些选项。当用户按下除1,2,3,4,5以外的任何按钮时,程序终止。如果没有while循环,我想不出任何其他方法来执行此操作,这似乎是正确的方法。

导入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);
                }
    }

}
随机射击6

嗨,您可以尝试使用此代码,

do{
  // Put the user input information here
}while(1<=input<=5);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章