gpt4 book ai didi

java - Scanner.next() 和 Scanner.nextLine()

转载 作者:行者123 更新时间:2023-12-02 06:29:34 26 4
gpt4 key购买 nike

我有以下代码:

Scanner in = new Scanner (System.in);
String[] data = new String[5];

System.out.println("Please, enter the name of the customer ordering:");
data[0] = in.next();
System.out.print("Please, enter the assembly details: ");
data[1] = in.nextLine();
System.out.print("Please, enter the assembly id:");
data[2] = in.next();
System.out.println("Please, enter the date the assembly was ordered (MM-DD-YYYY):");
data[3] = in.next();

我试图让 nextLine() 读取多个单词,但在测试它时,它只是跳转到下一个扫描数据[2]。我需要帮助。我不知道该怎么办。

最佳答案

这应该有效:

    Scanner in = new Scanner (System.in);
String[] data = new String[5];

System.out.println("Please, enter the name of the customer ordering:");
data[0] = in.nextLine();
System.out.println("Please, enter the assembly details: ");
data[1] = in.nextLine();
System.out.println("Please, enter the assembly id:");
data[2] = in.nextLine();
System.out.println("Please, enter the date the assembly was ordered (MM-DD-YYYY):");
data[3] = in.nextLine();
in.close();

您应该使用 nextLine() 而不是 next() 从控制台读取每一行

关于java - Scanner.next() 和 Scanner.nextLine(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20189486/

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