简体   繁体   English

扫描仪打印空间

[英]print space with scanner

i'm essentiel trying to create a movie guessing game, a bit like hangman, but you need to guess a movie title. 我正在尝试创建电影猜测游戏,有点像子手,但您需要猜测电影标题。 The movie title is encoded with each character being represented by the underscore ('_') character. 电影标题经过编码,每个字符都由下划线(_)字符表示。 As the user inputs a correct letter, the underscore at the position of the corresponding letter changes to that letter. 当用户输入正确的字母时,相应字母位置的下划线将变为该字母。 The user inputs his guesses as single chars using the scanner. 用户使用扫描仪将其猜测输入为单个字符。 My problem is that some movie titles contain a space (' '). 我的问题是某些电影标题包含空格('')。 But I am unable to use the scanner to return a space... My guess is that a scanner uses the whitespace as the default delimiter therefore it doesn't return a space as a character because it uses the whitespace to break the input into tokens? 但是我无法使用扫描仪返回空格...我的猜测是,扫描仪使用空格作为默认定界符,因此它不返回空格作为字符,因为它使用空格将输入分成令牌?

By 'return a space' I mean consider a space as a input, and not as a delimiter. “返回空格”是指将空格视为输入,而不是定界符。

Can anyone give me a solution as to how I could detect a space from the user input? 谁能给我一个关于如何从用户输入中检测空间的解决方案?

This code should read through the movieTitle, then if the movie title contains a space at index i, and the user input is a space, then the codedMovieTitle should update with a space at position i, instead of an underscore. 此代码应通读movieTitle,然后,如果电影标题在索引i处包含空格,并且用户输入为空格,则codedMovieTitle应该在位置i处更新为空格,而不是下划线。 However this doesn't work. 但是,这不起作用。 When I enter a space using the scanner nothing happens... 当我使用扫描仪进入一个空间时,没有任何反应...

    for (int i = 0; i < movieTitle.length(); i++) {       
            if(Character.isSpaceChar(movieTitle.charAt(i)) && 
            Character.isSpace(userInput)){
                    encodedMovieTitle = encodedMovieTitle.substring(0, i)
                    + userInput
                    + encodedMovieTitle.substring(i + 1);
            }
    }

This is the code for my scanner: 这是我的扫描仪的代码:

    Scanner scanner = new Scanner(System.in);
    char userInput;
    while(!gameIsWon && (numberOfGuessesLeft != 0)) {
        while (scanner.hasNextInt()) {
            System.out.println("You must enter a single character, try again.");
            scanner.next();
        }
        userInput = scanner.nextLine().charAt(0);

I am also struggling with enabling my scanner input to be non case sensitive. 我也在努力使扫描仪输入不区分大小写。 Some of the movies have capital letters in them. 有些电影中有大写字母。 If the title is "Snowstorm" and the user inputs the character 's', the resulting encodedMovieTitle will be "_ _ _ _ s _ _ _ _", not "S _ _ _ s _ _ _ _" 如果标题是“ Snowstorm”,并且用户输入了字符“ s”,则得到的encodingMovieTitle将是“ _ _ _ s _ _ _ _ _”,而不是“ S _ _ _ s _ _ _ _ _”

If anyone has a solution that would be great! 如果有人有解决方案,那就太好了! Most solutions I found involved using strings but I use chars as my userInput etc therefore I was wondering if there's a solution involving chars. 我发现大多数解决方案都涉及使用字符串,但是我将chars用作userInput等,因此我想知道是否存在涉及chars的解决方案。

Code for checking is userInput (char) is in movieTitle (string): 用于检查的代码是movieTitle(字符串)中的userInput(字符):

    for (int i = 0; i < movieTitle.length(); i++) {
            if (updatedCodedMovieTitle.charAt(i) != userInput && movieTitle.charAt(i) == userInput) {
                updatedCodedMovieTitle = updatedCodedMovieTitle.substring(0, i)
                                        + userInput
                                        + updatedCodedMovieTitle.substring(i + 1);
                wordHasBeenUpdated = true;
            }
            if(Character.isSpaceChar(movieTitle.charAt(i)) && Character.isSpaceChar(userInput)){
                updatedCodedMovieTitle = updatedCodedMovieTitle.substring(0, i)
                        + " "
                        + updatedCodedMovieTitle.substring(i + 1);
            }
        }

You can take input from the scanner in the following manner: 您可以通过以下方式从扫描仪获取输入:

Scanner scanner = new Scanner(System.in);
String str;
str = scanner.nextline();

This will accept the space as a character not as a delimiter. 这会将空格作为字符而不是分隔符。

Hope this helps:) 希望这可以帮助:)

Indeed, Scanner ( documentation ) 确实, Scanner文档

breaks its input into tokens using a delimiter pattern, which by default matches whitespace. 使用分隔符模式将其输入分解为标记, 默认情况下 ,该模式与空格匹配。

And whitespace includes space too. 而且空格也包含空间。

But you can easily modify Scanner 's delimiter pattern using its useDelimiter method , eg: 但是您可以使用其useDelimiter 方法轻松修改Scanner的定界符模式,例如:

Scanner s = new Scanner(System.in);
s.useDelimiter("\n");

\\n is the newline character, which in this case means that Scanner will generate new tokens when user presses <Enter> . \\n是换行符,在这种情况下,这意味着Scanner将在用户按下<Enter>时生成新令牌。 So spaces will be returned as expected. 因此,空格将按预期返回。

Or if you want Scanner to return only 1 character at a time, you can set "" as the delimiter, and then use s.next() to get a character from the input: 或者,如果您希望Scanner一次返回1个字符 ,则可以将""设置为分隔符,然后使用s.next()从输入中获取字符:

Scanner s = new Scanner(System.in);
s.useDelimiter("");
String input = s.next(); // this will be only one character

To match the strings regardless of cases you can use equalsIgnoreCase() as: 要匹配字符串,无论情况如何,都可以将equalsIgnoreCase()用作:

String movieName = "Snowstrom";
if(str.equalsIgnoreCase(movieName.charAt(0))) {
    // Set the value at appropriate location, 0 in this case
}

The statement in the if condition will compare the two strings regardless of their cases. if条件中的语句将比较两个字符串,而不管它们的大小写如何。 The above condition will evaluate to true whether the user enters s or S . 无论用户输入s还是S ,以上条件都将评估为true。

UPDATE UPDATE

You can take input for the character as follows: 您可以按如下方式输入字符:

Scanner scanner = new Scanner(System.in);
scanner.useDelimiter("\n");
char userInput;
userInput = scanner.next().charAt(0);

The following is your code with one modification: 以下是您的代码,进行了一次修改:

for (int i = 0; i < movieTitle.length(); i++) {
    if (updatedCodedMovieTitle.charAt(i) != userInput && (movieTitle.charAt(i) == userInput || movieTitle.charAt(i) == Character.toUpperCase(userInput))) {
        updatedCodedMovieTitle = updatedCodedMovieTitle.substring(0, i)
                                        + userInput
                                        + updatedCodedMovieTitle.substring(i + 1);
        wordHasBeenUpdated = true;
    }
    if(Character.isSpaceChar(movieTitle.charAt(i)) && Character.isSpaceChar(userInput)){
        updatedCodedMovieTitle = updatedCodedMovieTitle.substring(0, i)
                        + " "
                        + updatedCodedMovieTitle.substring(i + 1);
    }
}

The change I made in your code is in the first if condition to compare both the uppercase and lowercase characters. 我在代码中所做的更改是第一个if条件,用于比较大写和小写字符。 I tested this program myself and it worked fine for me and giving me the desired output. 我自己测试了该程序,它对我来说很好用,并给了我所需的输出。

Hope this works out for you also:) 希望这对您也有用:)

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

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