2014-10-18 58 views

回答

0

你可以使用像它

package userinput; 

import java.util.Scanner; 

public class USERINPUT { 

    public static void main(String[] args) { 

     Scanner input = new Scanner(System.in); 

     //allow user input; 
     System.out.println("How many numbers do you want to enter?"); 
     int num = input.nextInt(); 

     int array[] = new int[num]; 

     System.out.println("Enter the " + num + " numbers now."); 

     for (int i = 0 ; i < array.length; i++) { 
      array[i] = input.nextInt(); 
     } 

     //you notice that now the elements have been stored in the array .. array[] 

     System.out.println("These are the numbers you have entered."); 
     printArray(array); 

    } 

    //print the array 
    public static void printArray(int arr[]){ 

     int n = arr.length; 

     for (int i = 0; i < n; i++) { 
      System.out.print(arr[i] + " "); 
     } 
    } 

} 
+0

感谢您接受 – Rasel 2014-10-21 07:11:57

0

如何简单地使用System.console().readLine("Array (use , as separator)? ").split(",")简单的方法是什么?然后将其转换为int[]数组(有大量的util库可以这样做)。