为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕沐林林 2023-07-19 15:56:21
如何检查扫描仪输入是否为空并将变量分配给空白字符串?覆盖初始字符串?尝试了以下代码,但它不起作用。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();
查看完整描述

1 回答

?
翻过高山走不出你

TA贡献1875条经验 获得超3个赞

您只需要重新分配变量,而不需要重新声明它

if (username.isEmpty()) {
   username = "Dude"; //generate replacement username
   }


查看完整回答
反对 回复 2023-07-19
  • 1 回答
  • 0 关注
  • 81 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信