用扫描器读取Java输入的许多字符串

时间:2015-10-22 12:14:43

标签: java string java.util.scanner

我有一个项目,让我创建一个程序来读取用户输入,程序然后告诉他们他们在哪个区域,但我似乎无法添加多个字符串。

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

}

4 个答案:

答案 0 :(得分:1)

好像你想要一个循环。一个这样的选择,就是当用户进入一个特殊的“区域”时停止(如下面的退出)。

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

答案 1 :(得分:0)

我不完全确定你的意思“但我似乎无法添加多个字符串。”但你似乎打印扫描仪对象“System.out.println(输入+”在区域2“);”而不是答案 System.out.println(回答+“在第2区”); 可能是因为这样你没有看到预期的结果吗?

public static void main (String args[]){
    Scanner input = new Scanner (System.in);
    String answer = input.nextLine();
    if (answer.equals("Mile End")) { // i would like more stations to be added but i dont no how
      System.out.println( answer +" is in Zone 2");
    } else {
      System.out.println("That is not a Station, please try again");
    }
  }

答案 2 :(得分:0)

您可能需要一个else if语句

import java.util.*;

public class hello {

  public static void main (String args[]){
    Scanner input = new Scanner (System.in);
    String answer = input.nextLine();

    if ("Mile End".equals(answer)) { 
      System.out.println( answer+" is in Zone 2");
    } else if("Hobbitland".equals(answer) {
      System.out.println( answer +" is in Zone 42");
    } else
      System.out.println("That is not a Station, please try again");
    }
  }
}

或者你可以使用像:

这样的开关
import java.util.*;

public class hello {

  public static void main (String args[]){
    Scanner input = new Scanner (System.in);
    String answer = input.nextLine();

    switch(answer){
      case "Mile End":
        System.out.println( answer +" is in Zone 2");
        break;
      case "Hobbitland":
        System.out.println( answer +" is in Zone 42");
        break;
      default:
        System.out.println("That is not a Station, please try again");
        break;
    }
  }
}

在不需要如此复杂的控制结构的情况下,还有其他方法可以解决这个问题。只需创建一个地图,将您的电台名称作为键,将其区域​​保存为值。当您获得输入时,只需在地图中查找并检索其区域即可。如果它不在您的地图中,则会打印您的错误消息。

答案 3 :(得分:0)

为什么不创建一个区域,其中区域是关键,而值是该区域下的站点列表?

然后你可以有一个处理地图填充的方法......

private static Map<String, List<String>> createZoneMap() {

    Map<String, List<String>> zoneMap = new HashMap<String, List<String>>();

    // Probably want to populate this map from a file

    return zoneMap;
}

然后你的主要看起来像......

public static void main(String args[]) {

    Map<String, List<String>> zoneMap = createZoneMap();

    Scanner scan = new Scanner(System.in);

    String input;

    while (true) {

        input = scan.nextLine();

        // Some code to exit the application...
        if (input.equalsIgnoreCase("quit")) {            
            System.out.println("Exiting...");
            System.exit(1);
        }

        String zone = findZone(zoneMap, input);

        if (zone != null) {
            System.out.println(input + " is in " + zone);
        } else {
            System.out.println("That is not a Station, please try again");
        }
    }
}

然后当您输入电台名称时,您会查看地图以查找电台所在的区域,如果它不存在则返回null或其他内容

private static String findZone(Map<String, List<String>> zoneMap, String station) {

    // Maybe make this more versatile so that it does not care about case...

    for (Map.Entry<String, List<String>> entry : zoneMap.entrySet()) {
        if (entry.getValue().contains(station)) {
            return entry.getKey();
        }
    }
    return null;
}

希望这对你来说是一个很好的起点。您还可以考虑不再在main方法中执行所有逻辑,而是在main中创建类的实例。