简体   繁体   English

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

[英]Reading many strings input in Java with scanner

I have a project that involves me to create a program that reads the user input and the program then tells them what zone they are in, but I cant seem to how to add multiple strings. 我有一个项目,涉及到我的创建一个程序,该程序读取用户输入,然后该程序告诉他们它们位于哪个区域,但是我似乎无法添加多个字符串。

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

}

It seems like you want a loop. 好像您要循环。 One such option, would be to stop when the user enters a special "zone" (like quit below). 一种这样的选择是在用户输入特殊的“区域”(如下面的退出 )时停止

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

am not entirely sure what you mean by "but I cant seem to how to add multiple strings." 不能完全确定您的意思是“但是我似乎无法添加多个字符串”。 but you seem to print the Scanner object " System.out.println( input +" is in Zone 2");" 但您似乎要打印扫描程序对象“ System.out.println(input +”在区域2中“);” instead of the answer System.out.println( answer +" is in Zone 2"); 而不是答案System.out.println(答案+“位于区域2”); Could it be because of this you are not seeing the expected result ? 可能是因为这个原因,您没有看到预期的结果吗?

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

You may need an else if statement 您可能需要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");
    }
  }
}

Alternatively you could use a switch like: 另外,您可以使用类似以下的开关:

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

There are still other means to solve this problem without the need of such a complex control structure. 还有其他方法可以解决此问题,而无需这种复杂的控制结构。 Just create a Map that holds your station names as key and their zone as value. 只需创建一个地图,将您的电台名称作为关键字,并将其区域作为值即可。 When you get an input you just look it up in your map and retrieve its zone. 收到输入后,您只需在地图中查找并检索其区域即可。 If it's not in your map you print your error message. 如果不在地图中,则打印错误消息。

Why not create a map where the zone is the key and the value is a list of stations that come under that zone? 为什么不创建一个以区域为键且值是该区域下的站点列表的地图?

You can then have a method that handles the population of the map... 然后,您可以使用一种方法来处理地图的人口...

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

Then your main can look something like... 然后您的主体可以看起来像...

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

Then when you type in the station name you look through the map to find the zone which the station comes under, if its not present then return null or something 然后,当您键入站点名称时,您会在地图上浏览以找到站点所属的区域,如果不存在该站点,则返回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;
}

Hope that's a good starting point for you. 希望这对您来说是一个很好的起点。 You could also consider moving away from performing all of your logic in the main method and instead create an instance of your class in the main. 您也可以考虑放弃在main方法中执行所有逻辑,而在main方法中创建类的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM