繁体   English   中英

扫描仪打印空间

[英]print space with scanner

我正在尝试创建电影猜测游戏,有点像子手,但您需要猜测电影标题。 电影标题经过编码,每个字符都由下划线(_)字符表示。 当用户输入正确的字母时,相应字母位置的下划线将变为该字母。 用户使用扫描仪将其猜测输入为单个字符。 我的问题是某些电影标题包含空格('')。 但是我无法使用扫描仪返回空格...我的猜测是,扫描仪使用空格作为默认定界符,因此它不返回空格作为字符,因为它使用空格将输入分成令牌?

“返回空格”是指将空格视为输入,而不是定界符。

谁能给我一个关于如何从用户输入中检测空间的解决方案?

此代码应通读movieTitle,然后,如果电影标题在索引i处包含空格,并且用户输入为空格,则codedMovieTitle应该在位置i处更新为空格,而不是下划线。 但是,这不起作用。 当我使用扫描仪进入一个空间时,没有任何反应...

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

这是我的扫描仪的代码:

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

我也在努力使扫描仪输入不区分大小写。 有些电影中有大写字母。 如果标题是“ Snowstorm”,并且用户输入了字符“ s”,则得到的encodingMovieTitle将是“ _ _ _ s _ _ _ _ _”,而不是“ S _ _ _ s _ _ _ _ _”

如果有人有解决方案,那就太好了! 我发现大多数解决方案都涉及使用字符串,但是我将chars用作userInput等,因此我想知道是否存在涉及chars的解决方案。

用于检查的代码是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);
            }
        }

您可以通过以下方式从扫描仪获取输入:

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

这会将空格作为字符而不是分隔符。

希望这可以帮助:)

确实, Scanner文档

使用分隔符模式将其输入分解为标记, 默认情况下 ,该模式与空格匹配。

而且空格也包含空间。

但是您可以使用其useDelimiter 方法轻松修改Scanner的定界符模式,例如:

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

\\n是换行符,在这种情况下,这意味着Scanner将在用户按下<Enter>时生成新令牌。 因此,空格将按预期返回。

或者,如果您希望Scanner一次返回1个字符 ,则可以将""设置为分隔符,然后使用s.next()从输入中获取字符:

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

要匹配字符串,无论情况如何,都可以将equalsIgnoreCase()用作:

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

if条件中的语句将比较两个字符串,而不管它们的大小写如何。 无论用户输入s还是S ,以上条件都将评估为true。

UPDATE

您可以按如下方式输入字符:

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

以下是您的代码,进行了一次修改:

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

我在代码中所做的更改是第一个if条件,用于比较大写和小写字符。 我自己测试了该程序,它对我来说很好用,并给了我所需的输出。

希望这对您也有用:)

暂无
暂无

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

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