JAVA:使用空格而不是回车表示新输入的开始。

huangapple 未分类评论43阅读模式
英文:

JAVA: Signal start of new input with space instead of enter

问题

我有一个任务需要创建一个程序该程序将接受输入并将其打印到控制台非常简单不过有一个问题我必须将信息存储在单独的变量中但输入的样式如下

    输入
    Blah 123 Green

我知道我可以创建一个与单个变量关联的单个扫描器输入将所有内容都存储为一个字符串但是对于Blah123和Green都必须存储在不同的变量中通常情况下如果我可以使用回车键表示新输入我会这样做

    Scanner scan = new Scanner(System.in);
    String first = scan.nextLine();
    int second = Integer.parseInt(scan.nextLine());
    String third = scan.nextLine();

但在这种情况下空格必须起到回车键的作用我该如何处理
英文:

I have an assignment to create a program that will take input and print it to the console. Pretty simple. There is one issue though. I have to store the information in separate variables but the input looks like this.

Input:
Blah 123 Green

I'm aware that I can create a single scanner input tied to a single variable that will store all of that as one String but for the assignment Blah, 123, and Green would all have to be stored in different variables. Normally what I would do if I could use the enter key to signal new input would be

Scanner scan = new Scanner(System.in);
String first = scan.nextLine();
int second = Integer.parseInt(scan.nextLine());
String third = scan.nextLine();

but in this case, the spaces have to act as the enter key instead. how would I go about this?

答案1

得分: 0

你可以使用 next() 来读取单个输入:

String first = scan.next();
int second = scan.nextInt();
String third = scan.next();
英文:

You could use next() to read individual inputs:

String first = scan.next();
int second = scan.nextInt());
String third = scan.next();

huangapple
  • 本文由 发表于 2020年4月10日 07:39:28
  • 转载请务必保留本文链接:https://java.coder-hub.com/61131921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定