java循环,如果不是
我知道这很简单,但是,我只编程了几个月,所以我的大脑有时会迷糊,我需要帮助使用嵌套的else if语句进行while循环。“我的循环”的问题是连续的,因为用户从来没有机会进入他们的选择(即-1停止循环)。如何更改循环,使其通过要求用户输入选项(1-4或-1退出)来运行 请帮忙,谢谢。我知道这很简单,我已经搜索了以前的论坛讨论,但我似乎无法让它工作java循环,如果不是,java,loops,while-loop,if-statement,Java,Loops,While Loop,If Statement,我知道这很简单,但是,我只编程了几个月,所以我的大脑有时会迷糊,我需要帮助使用嵌套的else if语句进行while循环。“我的循环”的问题是连续的,因为用户从来没有机会进入他们的选择(即-1停止循环)。如何更改循环,使其通过要求用户输入选项(1-4或-1退出)来运行 请帮忙,谢谢。我知道这很简单,我已经搜索了以前的论坛讨论,但我似乎无法让它工作 //create new scanner object Scanner userInput = new Scanner (System.in
//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;
}
}
在
while
循环中,您不会更改end
变量,从而导致无限循环。您需要将输入处理逻辑放在while循环中,以便end
有机会更改,从而while
循环可以结束。您不需要更改while
循环中的end
变量,从而导致无限循环。您需要将输入处理逻辑放在while循环内,以便end
有机会更改,while
循环可以结束。将if/else移动到while循环内。将if/else移动到while循环内。只需将if语句放在循环内即可(注意,我没有检查代码中的任何其他内容,这只是一个快速响应):
只需将if语句放入循环中就可以了(注意,我没有检查代码中的任何其他内容,这只是一个快速响应):
谢谢,我的大脑累了,今晚所有的东西都必须放进去,我已经连续4天不停地看代码了!谢谢,我的大脑累了,今晚所有的东西都必须放进去,我已经连续4天不停地看代码了!谢谢;-)我现在感觉很累,不管怎样,我也累了。。谢谢;-)我现在感觉很累,不管怎样,我累了。。
//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;
}
}
}