gpt4 book ai didi

java - Java 中的扫描器 nextLine 问题

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:45:21 24 4
gpt4 key购买 nike

<分区>

只有一个问题:为什么我必须键入 answer = in.nextLine(); 两次?如果这一行是单一的,则程序不会按预期运行。如果没有第二行,程序不会要求您输入字符串。

public class Main {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String answer = "Yes";

while (answer.equals("Yes")) {
System.out.println("Enter name and rating:");
String name = in.nextLine();
int rating = 0;

if (in.hasNextInt()) {
rating = in.nextInt();
} else {
System.out.println("Error. Exit.");
return;
}

System.out.println("Name: " + name);
System.out.println("Rating: " + rating);
ECTS ects = new ECTS();
rating = ects.checkRating(rating);
System.out.println("Enter \"Yes\" to continue: ");
answer = in.nextLine();
answer = in.nextLine();
}

System.out.println("Bye!");
in.close();
}
}

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