Java 重复void方法
我想重复getCommand()方法,直到用户键入quit,但它总是失败Java 重复void方法,java,if-statement,methods,repeat,Java,If Statement,Methods,Repeat,我想重复getCommand()方法,直到用户键入quit,但它总是失败 我试图将getCommand()放在另一个方法的末尾&if语句不会重复 我试着做一个while循环,但是如果有人不小心输入错误,退出程序将永远不会结束 在用户希望退出之前,我如何才能有效地返回此方法?我将使用以下两种方法之一 public static void main(String[] args) { CollegeTester ct = new CollegeTester(); ct.getC
在用户希望退出之前,我如何才能有效地返回此方法?我将使用以下两种方法之一
public static void main(String[] args)
{
CollegeTester ct = new CollegeTester();
ct.getCommand();//goes to command
}
//Ask user for a command
public void getCommand()
{
String command = "";
System.out.println("Enter a command: ");
command = input.nextLine();
if(command.equals("add"))
addCommand();//If command is add go to addCommand
if(command.equals("course"))
courseCommand();//If command is course go to courseCommand
if(command.equals("find"))
findCommand();
if(command.equals("remove"))
removeCommand();
if(command.equals("highest"))
highestCommand();
if(command.equals("login"))
loginCommand();
if(command.equals("quit"))
System.exit(1);
}
编辑:已更正系统。退出(1)。谢谢Mikkel。我会用两种方法之一
public static void main(String[] args)
{
CollegeTester ct = new CollegeTester();
ct.getCommand();//goes to command
}
//Ask user for a command
public void getCommand()
{
String command = "";
System.out.println("Enter a command: ");
command = input.nextLine();
if(command.equals("add"))
addCommand();//If command is add go to addCommand
if(command.equals("course"))
courseCommand();//If command is course go to courseCommand
if(command.equals("find"))
findCommand();
if(command.equals("remove"))
removeCommand();
if(command.equals("highest"))
highestCommand();
if(command.equals("login"))
loginCommand();
if(command.equals("quit"))
System.exit(1);
}
编辑:已更正系统。退出(1)。谢谢Mikkel。我会用两种方法之一
public static void main(String[] args)
{
CollegeTester ct = new CollegeTester();
ct.getCommand();//goes to command
}
//Ask user for a command
public void getCommand()
{
String command = "";
System.out.println("Enter a command: ");
command = input.nextLine();
if(command.equals("add"))
addCommand();//If command is add go to addCommand
if(command.equals("course"))
courseCommand();//If command is course go to courseCommand
if(command.equals("find"))
findCommand();
if(command.equals("remove"))
removeCommand();
if(command.equals("highest"))
highestCommand();
if(command.equals("login"))
loginCommand();
if(command.equals("quit"))
System.exit(1);
}
编辑:已更正系统。退出(1)。谢谢Mikkel。我会用两种方法之一
public static void main(String[] args)
{
CollegeTester ct = new CollegeTester();
ct.getCommand();//goes to command
}
//Ask user for a command
public void getCommand()
{
String command = "";
System.out.println("Enter a command: ");
command = input.nextLine();
if(command.equals("add"))
addCommand();//If command is add go to addCommand
if(command.equals("course"))
courseCommand();//If command is course go to courseCommand
if(command.equals("find"))
findCommand();
if(command.equals("remove"))
removeCommand();
if(command.equals("highest"))
highestCommand();
if(command.equals("login"))
loginCommand();
if(command.equals("quit"))
System.exit(1);
}
编辑:已更正系统。退出(1)。谢谢Mikkel。或者您可以将其放在while循环中:
public static void main(String[] args)
{
CollegeTester ct = new CollegeTester();
ct.getCommand();//goes to command
}
//Ask user for a command
public void getCommand()
{
boolean exit = false;
while(!exit){
String command = "";
System.out.println("Enter a command: ");
command = input.nextLine();
if(command.equals("add")){
addCommand();//If command is add go to addCommand
}else if(command.equals("course")){
courseCommand();//If command is course go to courseCommand
}else if(command.equals("find")){
findCommand();
}else if(command.equals("remove")){
removeCommand();
}else if(command.equals("highest")){
highestCommand();
}else if(command.equals("login")){
loginCommand();
}else if(command.equals("quit")){
exit = true;
}else {
System.out.println("Not valid command, try again.");
}
}
}
//Ask user for a command
public void getCommand()
{
while(true){
String command = "";
System.out.println("Enter a command: ");
command = input.nextLine();
if(command.equals("add"))
addCommand();//If command is add go to addCommand
if(command.equals("course"))
courseCommand();//If command is course go to courseCommand
if(command.equals("find"))
findCommand();
if(command.equals("remove"))
removeCommand();
if(command.equals("highest"))
highestCommand();
if(command.equals("login"))
loginCommand();
if(command.equals("quit"))
System.exit(0); // or could use 'break' here (thanks Mikkel)
}
}
另外,请注意,您实际上是说应用程序应该由于错误而关闭。当打算关闭时,您应该通过0
,而不是1
事实上,如果您使用像上面这样的while循环,我可能只是通过
返回false来逃避循环代码>之类的,让程序自然结束 或者您可以将其放置在while循环中:
public static void main(String[] args)
{
CollegeTester ct = new CollegeTester();
ct.getCommand();//goes to command
}
//Ask user for a command
public void getCommand()
{
boolean exit = false;
while(!exit){
String command = "";
System.out.println("Enter a command: ");
command = input.nextLine();
if(command.equals("add")){
addCommand();//If command is add go to addCommand
}else if(command.equals("course")){
courseCommand();//If command is course go to courseCommand
}else if(command.equals("find")){
findCommand();
}else if(command.equals("remove")){
removeCommand();
}else if(command.equals("highest")){
highestCommand();
}else if(command.equals("login")){
loginCommand();
}else if(command.equals("quit")){
exit = true;
}else {
System.out.println("Not valid command, try again.");
}
}
}
//Ask user for a command
public void getCommand()
{
while(true){
String command = "";
System.out.println("Enter a command: ");
command = input.nextLine();
if(command.equals("add"))
addCommand();//If command is add go to addCommand
if(command.equals("course"))
courseCommand();//If command is course go to courseCommand
if(command.equals("find"))
findCommand();
if(command.equals("remove"))
removeCommand();
if(command.equals("highest"))
highestCommand();
if(command.equals("login"))
loginCommand();
if(command.equals("quit"))
System.exit(0); // or could use 'break' here (thanks Mikkel)
}
}
另外,请注意,您实际上是说应用程序应该由于错误而关闭。当打算关闭时,您应该通过0
,而不是1
事实上,如果您使用像上面这样的while循环,我可能只是通过返回false来逃避循环代码>之类的,让程序自然结束 或者您可以将其放置在while循环中:
public static void main(String[] args)
{
CollegeTester ct = new CollegeTester();
ct.getCommand();//goes to command
}
//Ask user for a command
public void getCommand()
{
boolean exit = false;
while(!exit){
String command = "";
System.out.println("Enter a command: ");
command = input.nextLine();
if(command.equals("add")){
addCommand();//If command is add go to addCommand
}else if(command.equals("course")){
courseCommand();//If command is course go to courseCommand
}else if(command.equals("find")){
findCommand();
}else if(command.equals("remove")){
removeCommand();
}else if(command.equals("highest")){
highestCommand();
}else if(command.equals("login")){
loginCommand();
}else if(command.equals("quit")){
exit = true;
}else {
System.out.println("Not valid command, try again.");
}
}
}
//Ask user for a command
public void getCommand()
{
while(true){
String command = "";
System.out.println("Enter a command: ");
command = input.nextLine();
if(command.equals("add"))
addCommand();//If command is add go to addCommand
if(command.equals("course"))
courseCommand();//If command is course go to courseCommand
if(command.equals("find"))
findCommand();
if(command.equals("remove"))
removeCommand();
if(command.equals("highest"))
highestCommand();
if(command.equals("login"))
loginCommand();
if(command.equals("quit"))
System.exit(0); // or could use 'break' here (thanks Mikkel)
}
}
另外,请注意,您实际上是说应用程序应该由于错误而关闭。当打算关闭时,您应该通过0
,而不是1
事实上,如果您使用像上面这样的while循环,我可能只是通过返回false来逃避循环代码>之类的,让程序自然结束 或者您可以将其放置在while循环中:
public static void main(String[] args)
{
CollegeTester ct = new CollegeTester();
ct.getCommand();//goes to command
}
//Ask user for a command
public void getCommand()
{
boolean exit = false;
while(!exit){
String command = "";
System.out.println("Enter a command: ");
command = input.nextLine();
if(command.equals("add")){
addCommand();//If command is add go to addCommand
}else if(command.equals("course")){
courseCommand();//If command is course go to courseCommand
}else if(command.equals("find")){
findCommand();
}else if(command.equals("remove")){
removeCommand();
}else if(command.equals("highest")){
highestCommand();
}else if(command.equals("login")){
loginCommand();
}else if(command.equals("quit")){
exit = true;
}else {
System.out.println("Not valid command, try again.");
}
}
}
//Ask user for a command
public void getCommand()
{
while(true){
String command = "";
System.out.println("Enter a command: ");
command = input.nextLine();
if(command.equals("add"))
addCommand();//If command is add go to addCommand
if(command.equals("course"))
courseCommand();//If command is course go to courseCommand
if(command.equals("find"))
findCommand();
if(command.equals("remove"))
removeCommand();
if(command.equals("highest"))
highestCommand();
if(command.equals("login"))
loginCommand();
if(command.equals("quit"))
System.exit(0); // or could use 'break' here (thanks Mikkel)
}
}
另外,请注意,您实际上是说应用程序应该由于错误而关闭。当打算关闭时,您应该通过0
,而不是1
事实上,如果您使用像上面这样的while循环,我可能只是通过返回false来逃避循环代码>之类的,让程序自然结束 正确使用do/while。您可以用它显示您的实现吗?do while命令!=退出如果我在进入无限循环后按backspace键,请不要使用==
比较字符串值。使用equals()
。在(!(command.equals(“quit”))时执行{stuff}也会发生同样的情况;在java中,如果按backspace,字符将保持不变,因此无法退出程序。请正确使用do/while。您可以使用它显示您的实现吗?do while命令!=退出?如果我在它进入无限循环后按backspace,请不要使用=
比较字符串值。使用equals()
。在(!(command.equals(“quit”))时执行{stuff}也会发生同样的情况;在java中,如果按backspace,字符将保持不变,因此无法退出程序。请正确使用do/while。您可以使用它显示您的实现吗?do while命令!=退出?如果我在它进入无限循环后按backspace,请不要使用=
比较字符串值。使用equals()
。在(!(command.equals(“quit”))时执行{stuff}也会发生同样的情况;在java中,如果按backspace,字符将保持不变,因此无法退出程序。请正确使用do/while。您可以使用它显示您的实现吗?do while命令!=退出?如果我在它进入无限循环后按backspace,请不要使用=
比较字符串值。使用equals()
。在(!(command.equals(“quit”))时执行{stuff}也会发生同样的事情;在java中,如果按backspace,字符将保持不变,因此您永远无法退出程序。或者,您可以只中断而退出while(true)
循环。另外系统.exit()
应该用0来调用,以指示干净的退出。或者,您可以只中断while(true)
循环。另外System.exit()应该用0来调用,以指示干净的退出。或者,您可以只中断while(true)
循环。也可以System.exit()
应使用0调用,以指示干净的退出。或者,您可以只中断而不执行while(true)
循环。此外System.exit()
应使用0调用,以指示干净的退出。