For-each Usage Error (cannot change array element values)

Source: Internet
Author: User

Code routines:

/**
* Data Encryption transmission
*/
Import Java.util.Scanner;

public class Secretpass {
public static void Main (string[] args) {
Scanner input = new Scanner (system.in);
int num = 0;
while (true) {
System.out.println ("Please enter four-bit positive integer:");
num = Input.nextint ();
if (num > 999 && num < 10000) {
SYSTEM.OUT.PRINTLN ("The input data is valid.) ");
Break
} else {
SYSTEM.OUT.PRINTLN ("The input data is invalid, please re-enter it.") ");
}
}
SYSTEM.OUT.PRINTLN ("The data before encryption is:" + num);
int[] Arrs = new Int[4];
ARRS[0] = num% 10; Take a digit
ARRS[1] = num/10% 10; Take 10 digits
ARRS[2] = num/100% 10; Take hundreds
ARRS[3] = num/1000; Take thousands
You cannot change the elements of a arrs array using the For-each statement
For-each can only be used to traverse the print array element

for (int n:arrs) {
n = (n + 5)% 10;
System.out.print (n+ "");
}

/*
for (int i = 0; i < arrs.length; i++) {
Arrs[i] = (Arrs[i] + 5)% 10;
}
*/
/*
for (int i = 0; i < arrs.length; i++) {
System.out.print (Arrs[i] + "");
}
*/
int tmp1 = arrs[0];
int tmp2 = arrs[1];
int tmp3 = arrs[2];
int tmp4 = arrs[3];
Arrs[0] = Tmp4;
ARRS[1] = Tmp3;
ARRS[2] = TMP2;
ARRS[3] = TMP1;
System.out.print ("Encrypted data is:");
for (int i:arrs) {
System.out.print (i);
}
}
}
The output results are:

Please enter a four-bit positive integer:
1234
The input data is valid.
The data before encryption is: 1234
9 8 7 6 Encrypted data is: 1234

Modified routines:

/**
* Data Encryption transmission
*/
Import Java.util.Scanner;

public class Secretpass {
public static void Main (string[] args) {
Scanner input = new Scanner (system.in);
int num = 0;
while (true) {
System.out.println ("Please enter four-bit positive integer:");
num = Input.nextint ();
if (num > 999 && num < 10000) {
SYSTEM.OUT.PRINTLN ("The input data is valid.) ");
Break
} else {
SYSTEM.OUT.PRINTLN ("The input data is invalid, please re-enter it.") ");
}
}
SYSTEM.OUT.PRINTLN ("The data before encryption is:" + num);
int[] Arrs = new Int[4];
ARRS[0] = num% 10; Take a digit
ARRS[1] = num/10% 10; Take 10 digits
ARRS[2] = num/100% 10; Take hundreds
ARRS[3] = num/1000; Take thousands
You cannot change the elements of a arrs array using the For-each statement
For-each can only be used to traverse the print array element
/*
for (int n:arrs) {
n = (n + 5)% 10;
System.out.print (n+ "");
}
*/

for (int i = 0; i < arrs.length; i++) {
Arrs[i] = (Arrs[i] + 5)% 10;
}

/*
for (int i = 0; i < arrs.length; i++) {
System.out.print (Arrs[i] + "");
}
*/
int tmp1 = arrs[0];
int tmp2 = arrs[1];
int tmp3 = arrs[2];
int tmp4 = arrs[3];
Arrs[0] = Tmp4;
ARRS[1] = Tmp3;
ARRS[2] = TMP2;
ARRS[3] = TMP1;
System.out.print ("Encrypted data is:");
for (int i:arrs) {
System.out.print (i);
}
}
}

The output results are:

Please enter a four-bit positive integer:
1234
The input data is valid.
The data before encryption is: 1234
The encrypted data is: 6789

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.