java循环,if else

时间:2013-03-19 00:34:13

标签: java loops while-loop if-statement

我知道这是非常简单的事情,但是,我只编程了几个月,所以我的大脑有时会出现雾,我需要一个带有嵌套else if语句的while循环的帮助。问题我的循环是连续的,因为用户永远不会有机会输入他们的选择(-1停止循环)。如何通过要求用户输入选项(1-4或-1退出)来更改循环以使其运行

请感谢任何帮助。我知道这很简单,我已经在之前的论坛讨论中进行了搜索,但我似乎无法使其发挥作用。

//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;
    }




}

3 个答案:

答案 0 :(得分:3)

将你的if / else移动到while循环中。

答案 1 :(得分:1)

您不会更改end循环内的while变量,因此会导致无限循环。您需要将输入处理逻辑放在while循环中,以便end有机会更改,以便while循环可以结束。

答案 2 :(得分:1)

简单地将if语句放在循环中应该有效(注意我没有检查代码上的任何其他内容,这只是一个快速响应):

//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;
    }
    }

}