1条回答 默认 最新
- 长生的梦呓 2019-12-26 22:23关注
你使用了sc.next()方法后,想使用 sc.nextLine()赋值给password。但是呢,在使用String password = sc.nextLine()之前需要先调用一次sc.nextLine()。
具体可以看我下面给的示例代码,你运行一次就知道了
public static void main(String[] args) { Scanner s = new Scanner(System.in); String str01 = s.next(); // 上面使用了非nextLine的方法, // 如果想使用nextLine(),需要先调用一次nextLine()再使用nextLine()。 s.nextLine(); // 这行代码如果注释那么下一行输入赋值则无法进行 String line = s.nextLine(); System.out.println(str01); System.out.println(line); }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 2无用