gpt4 book ai didi

java - 使用扫描仪读取Java中输入的许多字符串

转载 作者:行者123 更新时间:2023-12-01 11:01:46 28 4
gpt4 key购买 nike

我有一个项目,需要创建一个程序来读取用户输入,然后该程序告诉他们所在的区域,但我似乎不知道如何添加多个字符串。

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();
}

关于java - 使用扫描仪读取Java中输入的许多字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33280805/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com