I see the enhanced for is convenient, and try to use the enhanced for statement to assign a numeric value of the keyboard input to each element of the array.
Use the enhanced for loop to assign user-entered numbers to the array
Package Lianxi;
Import java.util.*;
public class Lianxi46 {
public static void Main (string[] args) {
Scanner input =new Scanner (system.in);
int suzu[]=new int[5]; Defines an array of length 5
for (int I:suzu) {//using Enhanced for loop
System.out.println ("Please enter Number:");
Suzu[i]=input.nextint (); Loops the value of the keyboard input to each element of the array
}
System.out.println (suzu[2]); A third element in the output array
}
}
The result is 0!
The enhanced for can only be used to go out of an array of elements and assign values to variables.
Since the array was previously defined, no value was assigned to the element, and all elements default to 0 so this method is not true.
java-array: Can I use the enhanced for loop implementation to assign a numeric loop of keyboard input to an array?