java中的多字扫描输入? [英] Multi-word scanner input in java?

查看:103
本文介绍了java中的多字扫描输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图使用依赖于用户输入的if-else语句。它在用户的输入只有一个单词时起作用,但是,多个单词输入无法识别并触发else语句。我该如何解决这个问题?

So I'm trying to use if-else statement dependant upon the user's input. It works when the user's input is only one word, however, multiple word inputs go unrecognized and triggers the else statement. How can i resolve this?


import java.util.Scanner;

public class MyFirstJavaClass {

public class MyFirstJavaClass {


推荐答案

替换:

answer = myScanner.next();

使用:

answer = myScanner.nextLine();

next只读取下一个值,直到它到达空格或换行符。在进行比较之前,您想要在整行中阅读

next will only read in the next value until it reaches a space or newline. You want to read in the full line before making the comparison

这篇关于java中的多字扫描输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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