当前位置: 代码迷 >> Java相关 >> 类 Scanner 的输入有关问题
  详细解决方案

类 Scanner 的输入有关问题

热度:6003   发布时间:2013-02-25 21:50:46.0
类 Scanner 的输入问题
import java.util.Scanner;

public class StringLength {
  private Scanner scan = new Scanner(System.in);
   
  public static void main(String[] args) {
  StringLength sl = new StringLength();
  sl.scanMany();
  }

  public void scanMany() {
  System.out.print("Enter update user id: ");
  int id = scan.nextInt();
  System.out.print("Enter update name: ");
  String name = scan.nextLine();
  System.out.print("Enter update password: ");
  String password = scan.nextLine();
  System.out.println(id + "\t\t" + name + "\t\t" + password);
  }
}

运行结果如下:
Enter update user id: 11
Enter update name: Enter update password: 56
11 56

第二个参数 name 系统居然不让输入了是怎么回事 ?

------解决方案--------------------------------------------------------
sorry!是我搞错了.
public void scanMany() { 
System.out.print("Enter update user id: "); 
int id = scan.nextInt(); 

scan = new Scanner(System.in);
System.out.print("Enter update name: "); 
String name = scan.nextLine(); 

System.out.print("Enter update password: "); 
String password = scan.nextLine(); 

System.out.println(id + "\t\t" + name + "\t\t" + password); 

把scan重新赋值就可以啦.
  相关解决方案