Java 为什么我的第一个nextLine()会被跳过?
坐在学校的一个项目上,我真的很困惑。 我的下一行;在我的两个for循环中都被跳过。我似乎不明白为什么,我得到这个答案可能是基本的,但我真的无法理解这一点Java 为什么我的第一个nextLine()会被跳过?,java,loops,for-loop,Java,Loops,For Loop,坐在学校的一个项目上,我真的很困惑。 我的下一行;在我的两个for循环中都被跳过。我似乎不明白为什么,我得到这个答案可能是基本的,但我真的无法理解这一点 for (int i = 0; i < antal; i++) { System.out.println("Name of the customer?"); String name = sc.nextLine(); Kunde.setName(name); Sys
for (int i = 0; i < antal; i++) {
System.out.println("Name of the customer?");
String name = sc.nextLine();
Kunde.setName(name);
System.out.println("Adress of the customer?");
String adresse = sc.nextLine();
Kunde.setAdress(adresse);
System.out.println("Mail of the customer?");
String mail = sc.nextLine();
Kunde.setMail(mail);
System.out.println("Phone number of the customer?");
int tlf = sc.nextInt();
Kunde.setTlf(tlf);
System.out.println("Postal code of the customer?");
int postalcode = sc.nextInt();
Kunde.setPostalCode(postalcode);
int x = ran.nextInt(100000);
Kunde.setOrderNumber(x);
}
for (int i = 0; i < antal; i++) {
System.out.println("Any specific orderdetails revolving order " + Kunde.getName(i)
+ " If not, press enter");
String details = sc.nextLine();
order.add(details);
}
Scanner.nextInt方法不使用输入的最后一个换行符。 这可能就是跳过第二个循环的第一条下一行的原因。
也许您在第一个cas之前也有类似的cas?您所说的被跳过是什么意思?你是说在第二个for循环中,当你打印任何特定的orderdetails时。。。并调用Kunde.getNamei该值未设置?您可能需要编辑您的问题以包含Kunde的代码,因为设置一个值似乎很奇怪,但随后得到的是索引i。sc.nextLine代码看起来不错,所以我猜这是关于如何从Kunde检索值的问题。客户的姓名和地址会同时打印出来,而nextLine方法不会提示任何内容。这听起来不对——扫描仪的设置一定有点怪怪的。你能编辑你的文章,把代码也包括进去吗?它是否像您期望的那样用于客户的邮件?块你的扫描仪从哪里读取数据?它是来自标准输入、文件还是其他什么?好像是从一个空行开始的。如果在String name=sc.nextLine之后立即在行中添加System.out.printlnName is:+name,会发生什么情况?同时打印客户的名称和地址。所以下一条线;把它当作一个空字符串。我不认为这真的回答了这个问题-虽然这可能会导致循环迭代之间发生奇怪的事情,但OP询问的是第一次调用nextLine方法的情况,所以这在当时不是问题。