java loop, if else [closed]

末鹿安然 提交于 2019-12-08 12:56:46

问题


I know this is something very simple, however, I've only been programming for a couple months so my brain just fog's out sometimes, and I need help with a while loop with nested else if statements. The problem my loop is continuous because the user is never given the chance to enter in their choice (which -1 stops the loop). How can I change the loop so that it operates by asking user to enter a choice (1-4, or -1 to quit)

Please Any help is appreciated. I know it's something simple, I've searched thorugh previous forum discussions but I can't seem to make it work.

//create new scanner object 
    Scanner userInput = new Scanner (System.in);
    int end = 0;
    //find out what user wants to do with address book
    while (end != -1)
    {
        System.out.println("  ");
        System.out.println("  ");
        System.out.println("----------------------------");
        System.out.println("What would you like to do with your address book? ...");
        System.out.println("----------------------------");
        System.out.println("Add new         [Enter 1]");
        System.out.println("Delete existing [Enter 2]");
        System.out.println("Edit existing   [Enter 3]");
        System.out.println("Search for      [Enter 4]");
        System.out.println("EXIT            [Enter -1]");
    }

    if (userInput.hasNext() && (userInput.nextInt() == 1)) 
    {
        AddressBookProcessor.addContact();
    }
    else if (userInput.hasNext() && userInput.nextInt() == 2)
    {
        AddressBookProcessor.deleteContact();
    }
    else if (userInput.hasNext() && userInput.nextInt() == 3)
    {
        AddressBookProcessor.editContact();
    }
    else if (userInput.hasNext() && userInput.nextInt() == 4)
    {
        AddressBookProcessor.findContact();
    }
    else if (userInput.nextInt() != 1 || userInput.nextInt() != 2 
                     || userInput.nextInt() != 3 || userInput.nextInt() != -1)
    {
        System.out.println("Please enter a valid input");
        end = -1;
    }




}

回答1:


Move your if/else's inside the while loop.




回答2:


You don't change the end variable inside the while loop, so that results in an infinite loop. You will need to place your input processing logic inside the while loop so that end has a chance to change, so that the while loop can end.




回答3:


Simply putting the if statement inside the loop should work (note I haven't checked anything else on the code this is just a quick response):

//create new scanner object 
    Scanner userInput = new Scanner (System.in);
    int end = 0;
    //find out what user wants to do with address book
    while (end != -1)
    {
        System.out.println("  ");
        System.out.println("  ");
        System.out.println("----------------------------");
        System.out.println("What would you like to do with your address book? ...");
        System.out.println("----------------------------");
        System.out.println("Add new         [Enter 1]");
        System.out.println("Delete existing [Enter 2]");
        System.out.println("Edit existing   [Enter 3]");
        System.out.println("Search for      [Enter 4]");
        System.out.println("EXIT            [Enter -1]");
    if (userInput.hasNext() && (userInput.nextInt() == 1)) 
    {
        AddressBookProcessor.addContact();
    }
    else if (userInput.hasNext() && userInput.nextInt() == 2)
    {
        AddressBookProcessor.deleteContact();
    }
    else if (userInput.hasNext() && userInput.nextInt() == 3)
    {
        AddressBookProcessor.editContact();
    }
    else if (userInput.hasNext() && userInput.nextInt() == 4)
    {
        AddressBookProcessor.findContact();
    }
    else if (userInput.nextInt() != 1 || userInput.nextInt() != 2 
                     || userInput.nextInt() != 3 || userInput.nextInt() != -1)
    {
        System.out.println("Please enter a valid input");
        end = -1;
    }
    }

}


来源:https://stackoverflow.com/questions/15489511/java-loop-if-else

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!