简体   繁体   English

如果检查用户输入字符串的语句是否包含用户输入的字母,不包括任何额外字母

[英]If statement that checks if a user entered string contains user entered letters excluding any extra letters

I need to create a program in which a string and 5 letters are entered by the user and the program checks if the string can be created again from those 5 letters, but ignores any letters that the string cannot be created from. 我需要创建一个程序,其中用户输入一个字符串和5个字母,程序检查是否可以从这5个字母再次创建字符串,但忽略了无法从中创建字符串的任何字母。

For example, if I enter the word "hello" and then I enter the 5 letters "h", "e", "l", "o", and "p", I want the program to makes sure that those letters do make up the word "hello", but ignores any extra letters, which in this case is the "p". 例如,如果我输入单词“hello”然后我输入5个字母“h”,“e”,“l”,“o”和“p”,我希望程序确保这些字母做组成单词“hello”,但忽略任何多余的字母,在这种情况下是“p”。 Another example, if I enter the word "apple" and then I enter the 5 letters "a", "p", "l", "o", and "f", I want the program to recognize that those letters do not create the word apple and to ignore the letters "o" and "f". 另一个例子,如果我输入单词“apple”然后我输入5个字母“a”,“p”,“l”,“o”和“f”,我希望程序能够识别那些字母不创建单词apple并忽略字母“o”和“f”。

If the program does verify that the string can be created from those 5 letters, while ignoring any extras, then it will output "You win!", but if not it will output "You lose!". 如果程序确实可以验证字符串是否可以从这5个字母创建,而忽略任何额外的,那么它将输出“你赢了!”,但如果没有,它将输出“你输了!”。 Here is the code I have so far: 这是我到目前为止的代码:

public static void main(String[] args) {

    Scanner keyboard = new Scanner (System.in);
    System.out.print("Please enter a word: ");
    String in = keyboard.nextLine();
    System.out.print("Please enter the first letter: ");
    String letter1 = keyboard.next();
    System.out.print("Please enter the second letter: ");
    String letter2 = keyboard.next();
    System.out.print("Please enter the third letter: ");
    String letter3 = keyboard.next();
    System.out.print("Please enter the fourth letter: ");
    String letter4 = keyboard.next();
    System.out.print("Please enter the fifth letter: ");
    String letter5 = keyboard.next();

    if (in.contains(letter1 + letter2 + letter3 + letter4 + letter5)) {
        System.out.println("You win!");
    } else {
        System.out.println("You lose!");
    }

}

Here is the output I am getting when I run this: 这是我运行时获得的输出:

样本输出

I am new to Java so I do not know if there is some kind of code I could use to do this. 我是Java的新手,所以我不知道是否有某种代码可以用来做这件事。 If somebody does know, please lead me in the right direction. 如果有人知道,请引导我朝正确的方向前进。

The easiest algorithm I can think of is: For each character inputted, remove all occurences from the string. 我能想到的最简单的算法是:对于输入的每个字符,从字符串中删除所有出现的字符。 At the end, check if string is empty. 最后,检查字符串是否为空。 Or in code: 或者在代码中:

char[] cs = new char[] {'h', 'e', 'l', 'o', 'p'};
String s = "hello";
for (char c : cs) s = s.replace(c+"", "");
if (s.isEmpty()) System.out.println("You win!");

Note that this approach is not performing very well. 请注意,这种方法表现不佳。 For a given number of characters n and a string length m this performs as O(n*m) . 对于给定数量的字符n和字符串长度m这表示为O(n*m) It also does a lot of costly String reallocations. 它还会进行大量昂贵的String重新分配。

The statement letter1 + letter2 + letter3 + letter4 + letter5 concatenates the strings. 声明letter1 + letter2 + letter3 + letter4 + letter5连接字符串。 If you stored the results of it in a variable then that variable would be equal to the string helop . 如果将结果存储在变量中,那么该变量将等于字符串helop

I would store letters 1-5 in some kind of Collection and then check each character to see if it is in that collection using charAt . 我会在某种Collection存储1-5个字母,然后使用charAt检查每个字符是否在该集合中。 There are other more efficient ways of solving your problem but I think this is the most clear. 还有其他更有效的方法来解决你的问题,但我认为这是最清楚的。

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

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