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