java-array: Can I use the enhanced for loop implementation to assign a numeric loop of keyboard input to an array?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.