1. 程式人生 > >Java怎麼實現從控制檯輸入

Java怎麼實現從控制檯輸入

用整形陣列讀取字串:

Scanner s = new Scanner(System.in);
    int arr[]= new int[10];
    for(int i=0;i<arr.length;i++){
    arr[i]=s.nextInt();
    System.out.print("您輸入的是:"+arr[i]+ " ");
    }
    s.close();

從控制檯輸入字串:

Scanner s = new Scanner(System.in);
        String m; 
        m=s.next();
        System.out
.println(m);

用hasNext遍歷輸入:

Scanner s = new Scanner(System.in);
        while(s.hasNext())
        System.out.println(s.next());

用hasNextInt遍歷:

Scanner s = new Scanner(System.in);
        while(s.hasNextInt()){
            System.out.print(s.next());
        }

用字元陣列讀取輸入的字串:

Scanner s = new Scanner(System.in
); String m =s.nextLine(); String arr[]=m.split("\\s"); for(String n:arr) System.out.print(n+" ");