【问题标题】:Guess who you are thinking of program (Theory of) [closed]猜猜你在想谁的程序(理论)[关闭]
【发布时间】:2015-08-09 21:30:39
【问题描述】:

在 Java 中,我正在创建一个程序,让用户想到他们认识的人。

然后,我的程序要求他们输入名字的第一个字母和姓氏的最后一个字母,不能有空格。

我希望我的程序然后查看一个全名数组,找到第一个字母与用户输入的第一个字母匹配的那个,以及相应的姓氏的最后一个字母。

到目前为止,这是我的程序:

import java.util.* ;
public class Guesser
{
    public static void main(String[] args)
    {
        Scanner UserInput = new Scanner(System.in);
        String [] names = {"firstname lastname " + "etc"}; //example name array
        System.out.print( "Hello! I am a robot. I might be smart, but I don't know. Please play a game with me to help me see if I am smart." +  "\n"  + "What I want you to do is think of someone you know." +  "\n"  + "Enter the first letter of their first name, and the last letter of their last name. Please no spaces. Then, press enter. " );
        String TheirGuess = UserInput.nextLine(); //get their input, assign a string to it
        System.out.println("You entered: " + TheirGuess);
        char FirstChar = TheirGuess.charAt(0);  // get the the first char
        char SecondChar = TheirGuess.charAt(1);  // get the second char
        System.out.println("I will now think of someone whose first name starts    with " + FirstChar + " and last name ends with " + SecondChar );


        UserInput.close();


    }
}

如何在字符串数组中搜索第一个字符为 FirstChar 且最后一个字符为 SecondChar 的名称?

【问题讨论】:

  • 我知道这个程序中还没有数组。
  • 从用户读取数据作为字符串并使用charAtsubstring

标签: java arrays


【解决方案1】:

这可以在 1 行代码中完成。

// Assuming you have populated a Set (actually any Collection) of names
Set<String> names;

List<String> matchedNames = names.stream()
    .filter(s -> s.matches(userInput.replaceAll("^.", "$0.*")))
    .collect(Collectors.toList());

如果你只是想打印匹配,那就更简单了:

names.stream()
    .filter(s -> s.matches(userInput.replaceAll("^.", "$0.*")))
    .forEach(System.out::println);

此代码识别您可以有多个匹配项。

虽然这看起来像是用勺子喂食,但对你来说,这个答案的价值在于弄清楚它是如何工作的。

【讨论】:

    【解决方案2】:

    执行此操作的有效方法是使用两个TreeSet 对象。一个包含名称,另一个包含姓氏。然后您可以使用subSet() 方法获取条目。所以,例如:

    TreeSet<String> names = new TreeSet<>();
    names.add("Antonio");
    names.add("Bernard");
    names.add("Peter");
    names.add("Zack");
    
    Set<String> bNames = names.subSet("B", "C");
    

    请注意,此实现区分大小写。但只需稍作调整,您就可以修复它 - 我将把它留给您。

    【讨论】:

      【解决方案3】:

      有一段时间没有写Java,但它应该是这样的:

      String names[] = new String[] { "AAA BBB", "CCC DDD", "EEE FFF" };
      Scanner input = new Scanner(System.in);
      String userInput = input.nextLine().toLowerCase();
      String result = "None";
      for (String name : names) {
          String[] nameSplitted = name.toLowerCase().split(" ");
          if (nameSplitted[0].charAt(0) == userInput.charAt(0) &&
              nameSplitted[1].charAt(0) == userInput.charAt(1)
          ) {
              result = name;
              break;
          }
      }
      System.out.println("Result is: " + result);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多