gpt4 book ai didi

java - 为什么代码不读取 nextLine()?

转载 作者:行者123 更新时间:2023-12-02 06:45:25 24 4
gpt4 key购买 nike

else if(input==5){
String artist = "Bob";
System.out.print("Artist: ");
artist = keyboard.nextLine();
Playlist artistList = defaultPlaylist.getSongsByArtist(artist);
artistList.printAllSongs();
}

当我运行它时,它完全跳过 [artist = Keyboard.nextLine();]

键盘是扫描仪对象的实例。

编辑---------------------------------------------- -------------------------------------------------- ----------------------------我所说的“跳过”是指完全忽略。当我运行它时,它不要求用户输入。

最佳答案

该语句很可能从不使用这些字符的方法传递换行符,例如 nextInt 。结果是nextLine语句不会阻塞输入。尝试在该语句之前使用这些字符

keyboard.nextLine(); // add this
artist = keyboard.nextLine();

如果有多个if语句 block 然后最好添加新的 nextLine if之前的声明语句(以避免多个 newLine 语句)

int input = keyboard.nextInt();
keyboard.nextLine();

关于java - 为什么代码不读取 nextLine()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18689210/

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