在开发过程中,我们经常会遇到各种问题,有时候即使是看似简单的问题也会让我们束手无策。比如,你可能会遇到一个让你困扰的问题,就是如何解决 nextline 问题并使其正常工作。不要担心,php小编新一在这里为大家提供一些解决该问题的方法和技巧,希望能帮助到大家解决这个难题。下面我们就一起来看看吧!
do { System.out.println("write in format \"firstname lastname\""); //just want to scan a sentence with spaces String[] tempArr = sc.nextLine().split(" "); System.out.println("Hello " + tempArr[0]); System.out.println("Do you want to do it again Y/N"); }while(sc.next().equals("Y"));
我不知道如何防止下一行扫描仪在第一次迭代后扫描空白
我希望循环运行,直到输入“y”以外的任何内容
我不会为此使用 do...while 循环。您可以通过创建一个无限循环来轻松地做到这一点,如果答案不是“y”,该循环就会停止。我假设您想循环用户输入 y 并停止其他任何内容。如果相反,只需翻转 if 语句中的布尔值即可。尝试这样的事情:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { System.out.println("write in format \"firstname lastname\""); String[] tempArr = sc.nextLine().split(" "); System.out.println("Hello " + tempArr[0]); System.out.println("Do you want to do it again Y/N"); String answer = sc.nextLine(); if (!answer.equalsIgnoreCase("Y")) { break; } } } }
以上就是我不知道如何解决 nextLine 问题并使其正常工作的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号