如何使Java用空格注册字符串输入? [英] How do I make Java register a string input with spaces?

查看:57
本文介绍了如何使Java用空格注册字符串输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

public static void main(String[] args) {
  Scanner in = new Scanner(System.in);
  String question;
  question = in.next();

  if (question.equalsIgnoreCase("howdoyoulikeschool?") )
    /* it seems strings do not allow for spaces */
    System.out.println("CLOSED!!");
  else
    System.out.println("Que?");

当我尝试写你觉得学校怎么样?"时答案总是?"但它可以像你喜欢学校吗?"那样很好

When I try to write "how do you like school?" the answer is always "Que?" but it works fine as "howdoyoulikeschool?"

我应该将输入定义为String以外的其他内容吗?

Should I define the input as something other than String?

推荐答案

in.next()将返回以空格分隔的字符串.如果要阅读整行,请使用 in.nextLine().读取字符串后,使用 question = question.replaceAll("\\ s",")删除空格.

in.next() will return space-delimited strings. Use in.nextLine() if you want to read the whole line. After reading the string, use question = question.replaceAll("\\s","") to remove spaces.

这篇关于如何使Java用空格注册字符串输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆