gpt4 book ai didi

java - java中无法接受字符输入

转载 作者:行者123 更新时间:2023-12-01 11:19:54 26 4
gpt4 key购买 nike

我是java新手,我想接受如下输入:

0 2 a

我能够接受整数输入,但无法接受字符输入。

我正在使用以下代码

Scanner s = new Scanner(System.in);
int o = s.nextInt();
if(o==0)
{
int l=s.nextInt();
char c=s.next().charAt(0);
}

最佳答案

May be Thats because the Scanner#nextInt method does not read the last newline character of your input, and thus that newline is consumed in the next call to Scanner#nextLine.

在 nextInt 之后触发一个新行。

    Scanner s = new Scanner(System.in);
int o = s.nextInt();
s.nextLine();
if(o==0)
{
int l=s.nextInt();
s.nextLine();
char c=s.next().charAt(0);
}

关于java - java中无法接受字符输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31370843/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com