我是个CS学位的大一新生,学习Java。当我试图解释我的问题时,你会不会介意,因为我对Java/IDE还不太熟悉,而且还不太熟悉它是如何一起工作的?
首先,我的教授给了我们一个简单的任务,在询问用户时加密/解密四位数的值。(用于,例如。“输入要加密的四位整数”- 9835)
然后,代码将使用一些公式(不完全正确的加密),但它将把四位数转换成一个新的四位数数字。因此,对于这个例子,9835加密将是0265。
因此,当我的课程使用Netbeans作为首选IDE时,代码运行良好,9835的答案是0265。
但是今天,当我决定尝试IntelliJ Idea Community并复制完全相同的代码时,答案应该是0265,但是输出只显示265。不知怎么的,它没有显示0作为第一个数字。
请注意,我已经考虑过将加密的变量从整数转换为字符串-所以将显示0,但是我的教授说可以不进行任何转换(当使用Netbeans时)。
当在IntelliJ中运行代码时,我能做什么才能将确切的输出显示为Netbeans?(答覆为0265)
非常感谢,任何帮助/建议都是非常感谢的。
下面是我的代码供你参考。(这可能会让人困惑,但确实有效。)
代码语言:javascript运行复制 import java.util.Scanner;
class Encryption_Decryption
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
//Variables to store the inputs
int encrypted_value_input , decrypted_value_input;
//Variables for storing the individual digits.
int digit1_E , digit2_E , digit3_E , digit4_E; //Where digit1_E is the extreme left position and _E stands for Encrypted.
//Variables for storing the digits with the formula applied.
int a1_e , a2_e , a3_e , a4_e;
//Variables for storing the encrypted and decrypted values.
int encrypted_value , decrypted_value;
//Welcome Message
System.out.println("This is an Encryption and Decryption Application.");
System.out.println();
//Start of Encryption
System.out.print("Enter 4 digits to be Encrypted: ");
encrypted_value_input = input.nextInt();
digit1_E = (encrypted_value_input / 1000); //takes the first digit
digit2_E = ((encrypted_value_input / 100) % 10);
digit3_E = ((encrypted_value_input / 10) % 10);
digit4_E = (encrypted_value_input % 10);
//Encryption Formula
a1_e = (digit1_E + 7) % 10;
a2_e = (digit2_E + 7) % 10;
a3_e = (digit3_E + 7) % 10;
a4_e = (digit4_E + 7) % 10;
//Swapping the digits. For eg. 1234 is abcd. abcd needs to be swapped to become cdab.
int temp1 = a1_e;
a1_e = a3_e;
a3_e = temp1;
temp1 = a2_e;
a2_e = a4_e;
a4_e = temp1;
encrypted_value = a1_e * 1000 + a2_e * 100 + a3_e * 10 + a4_e;
System.out.println("The encrypted value is " + encrypted_value);
}
}