多个 sc.nextLine(); 一个接一个地运行,但需要运行 if/else 语句

如何检查扫描仪输入是否为空并将变量分配给空白字符串?覆盖初始字符串?


尝试了以下代码,但它不起作用。


System.out.print("Username: ");


    String username = sc.nextLine();


    int u1nameLength = username.length();


    if (u1nameLength == 0) {


        String username = "Dude"; //generate replacement username


    }


    System.out.print("Phone Number: ");


    String phoneNumber = sc.nextLine();


慕沐林林
浏览 86回答 1
1回答

翻过高山走不出你

您只需要重新分配变量,而不需要重新声明它if (username.isEmpty()) {    username = "Dude"; //generate replacement username    }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java