我有一个项目,涉及到我的创建一个程序,该程序读取用户输入,然后该程序告诉他们它们位于哪个区域,但是我似乎无法添加多个字符串。
import java.util.*;
public class hello {
public static void main (String args[]){
Scanner input = new Scanner (System.in);
String answer = input.nextLine();
// I would like more stations to be added but I don't no how
if ("Mile End".equals(answer)) {
System.out.println( input +" is in Zone 2");
} else {
System.out.println("That is not a Station, please try again");
}
}
}
最佳答案
好像您要循环。一种这样的选择是在用户输入特殊的“区域”(如下面的退出)时停止。
String answer = input.nextLine();
while (!answer.equalsIgnoreCase("quit")) {
// I would like more stations to be added but I don't no how
if ("Mile End".equals(answer)) {
System.out.println( input +" is in Zone 2");
} else {
System.out.println("That is not a Station, please try again. "
+ "Quit to stop.");
}
answer = input.nextLine();
}