我正在尝试使用 Scanner(System.in) 进行输入,但它不允许我这样做。帮助!

发布于 2024-09-30 04:42:50 字数 252 浏览 0 评论 0原文

因此,我尝试使用 Scanner(System.in) 从用户那里获取输入,但是当我尝试在控制台中输入某些内容时,它不会让我这样做。

有人可以帮忙吗?

Function.show();

Scanner input = new Scanner(System.in);

if (input.equals("a"))
{
    Function.outputFile(1, list);
}

input.close();

So I'm trying to take an input from a user using Scanner(System.in) but when i try to type something into the console it won't let me.

Can anyone help?

Function.show();

Scanner input = new Scanner(System.in);

if (input.equals("a"))
{
    Function.outputFile(1, list);
}

input.close();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把时间冻结 2024-10-07 04:42:51

您忘记在扫描仪上调用next。您的 if 行应该改为 if (input.next().equals("a"))

You're forgetting to call next on the Scanner. Your if line should be if (input.next().equals("a")), instead.

少女的英雄梦 2024-10-07 04:42:51

我建议在开关中使用 input.next.charAt(0) ...

Function.show(); 

Scanner input = new Scanner(System.in); 

switch (input.next().charAt(0)) {
    case 'a': { 
        Function.outputFile(1, list); 
        break;
    }
    case 'b': {
        etc
    }

如果将其分开,(即字符字母),您可以使用 switch (letter.toUpperCase()) [理论上...我从来没有尝试过]然后你就不必担心case

I would recommend using input.next.charAt(0) in a switch...

Function.show(); 

Scanner input = new Scanner(System.in); 

switch (input.next().charAt(0)) {
    case 'a': { 
        Function.outputFile(1, list); 
        break;
    }
    case 'b': {
        etc
    }

If you separate it, (I.E. char letter) you can use switch (letter.toUpperCase()) [in theory... I've never tried it] and then you don't have to worry about case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文