How to get input to array simply in Java? -
How to get input to array simply in Java? -
like c++ can utilize this.
a[5]; copy(istream_iterator<int>(cin),istream_iterator<int>(),a);
is there simple way array input in java?
you can utilize simple way it
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 want enter?"); int num = input.nextint(); int array[] = new int[num]; system.out.println("enter " + num + " numbers now."); (int = 0 ; < array.length; i++ ) { array[i] = input.nextint(); } //you notice elements have been stored in array .. array[] system.out.println("these numbers have entered."); printarray(array); } //print array public static void printarray(int arr[]){ int n = arr.length; (int = 0; < n; i++) { system.out.print(arr[i] + " "); } } }
java arrays input
Comments
Post a Comment