Java凯撒移位字符打印失败
Java Caesar Shift Character Print Failure
我正在尝试开发一个非常基本的程序(稍后我将添加)以对用户输入的文本执行凯撒移位。
我有很多工作,但当我试图打印出 "encrypted" 字符串时,它不起作用。我正在使用 Netbeans IDE,它只打印出一个空白值。我已经添加了额外的打印语句来进行错误检查,并且我相信我的 "encryption" - 即更改字符正在正确发生,但是当我将它重铸为一个字符时,出现了一些问题。我的代码如下:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package forpractice;
import java.util.*;
import java.util.Scanner;
class CaesarShift {
// public String encrypt(String[] plainText){
// return null;
//
//
//
// }
public static void main(String[] args){
// Variable declarations needed for the entire program
Scanner myScan = new Scanner(System.in);
System.out.println("Please input a string of characters to encrypt: ");
String plainText = myScan.nextLine();
String convertedText = plainText.toLowerCase();
char[] plainTextArray = convertedText.toCharArray();
ArrayList<Character> encryptedTextArray = new ArrayList<>();
String encryptedString = new String();
int currValue;
char curr;
char curr1;
char newCurr;
int newCurrValue;
// Variable declarations needed for the entire program
// Loop through the array, convert to all lowercase, and encrypt it
for (int i = 0; i < plainTextArray.length; i++){
curr = plainTextArray[i];
System.out.println("Current character: " + plainTextArray[i]);
currValue = (int) curr;
System.out.println("Current character value: " + currValue);
newCurrValue = ((currValue-3) % 26);
System.out.println("Encrypted character value: " + newCurrValue);
newCurr = (char) newCurrValue;
System.out.println("Encrypted character: " + newCurr);
encryptedTextArray.add(newCurr);
} //end for
System.out.println("Here is the algorithm :");
System.out.println("***************");
System.out.println("Your Plaintext was: " + plainText);
System.out.println("Your encrypted text was: ");
for (int i = 0; i < encryptedTextArray.size(); i++){
encryptedString += encryptedTextArray.get(i);
}
System.out.println("***************");
} //end psvm
} //end class
如果您有任何建议或意见,我们将不胜感激。我还没有找到任何关于这个特定问题的例子。谢谢
密切注意 Ascii table 和你的表情
newCurrValue = ((currValue-3) % 26);
您正在从当前值中减去 3,然后再减去 mod 26,这保证您的结果在 0 到 26 的范围内。如果您在 table 中查找您发现既没有 a
到 z
的值,也没有 A
到 Z
的值。实际上,所有这些字符要么是隐形的,要么是空白的。
以下示例演示了小写字符的正确用法:
newCurrValue = ((currValue - 'a') % 26 - 3); // 3 is your shift value
// if the result is negative, simply add 26 (amount of smallercase characters)
if (newCurrValue < 0) {
newCurrValue += 26;
}
newCurrValue += 'a'; // add 'a' again, to be within 'a' - 'z'
此外,尽管你犯了一点小错误,但无论如何你都不会在控制台上看到结果,因为你缺少一条日志语句:
System.out.println("Your encrypted text was: ");
for (int i = 0; i < encryptedTextArray.size(); i++) {
encryptedString += encryptedTextArray.get(i);
}
System.out.println(encryptedString); // output your result
我正在尝试开发一个非常基本的程序(稍后我将添加)以对用户输入的文本执行凯撒移位。
我有很多工作,但当我试图打印出 "encrypted" 字符串时,它不起作用。我正在使用 Netbeans IDE,它只打印出一个空白值。我已经添加了额外的打印语句来进行错误检查,并且我相信我的 "encryption" - 即更改字符正在正确发生,但是当我将它重铸为一个字符时,出现了一些问题。我的代码如下:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package forpractice;
import java.util.*;
import java.util.Scanner;
class CaesarShift {
// public String encrypt(String[] plainText){
// return null;
//
//
//
// }
public static void main(String[] args){
// Variable declarations needed for the entire program
Scanner myScan = new Scanner(System.in);
System.out.println("Please input a string of characters to encrypt: ");
String plainText = myScan.nextLine();
String convertedText = plainText.toLowerCase();
char[] plainTextArray = convertedText.toCharArray();
ArrayList<Character> encryptedTextArray = new ArrayList<>();
String encryptedString = new String();
int currValue;
char curr;
char curr1;
char newCurr;
int newCurrValue;
// Variable declarations needed for the entire program
// Loop through the array, convert to all lowercase, and encrypt it
for (int i = 0; i < plainTextArray.length; i++){
curr = plainTextArray[i];
System.out.println("Current character: " + plainTextArray[i]);
currValue = (int) curr;
System.out.println("Current character value: " + currValue);
newCurrValue = ((currValue-3) % 26);
System.out.println("Encrypted character value: " + newCurrValue);
newCurr = (char) newCurrValue;
System.out.println("Encrypted character: " + newCurr);
encryptedTextArray.add(newCurr);
} //end for
System.out.println("Here is the algorithm :");
System.out.println("***************");
System.out.println("Your Plaintext was: " + plainText);
System.out.println("Your encrypted text was: ");
for (int i = 0; i < encryptedTextArray.size(); i++){
encryptedString += encryptedTextArray.get(i);
}
System.out.println("***************");
} //end psvm
} //end class
如果您有任何建议或意见,我们将不胜感激。我还没有找到任何关于这个特定问题的例子。谢谢
密切注意 Ascii table 和你的表情
newCurrValue = ((currValue-3) % 26);
您正在从当前值中减去 3,然后再减去 mod 26,这保证您的结果在 0 到 26 的范围内。如果您在 table 中查找您发现既没有 a
到 z
的值,也没有 A
到 Z
的值。实际上,所有这些字符要么是隐形的,要么是空白的。
以下示例演示了小写字符的正确用法:
newCurrValue = ((currValue - 'a') % 26 - 3); // 3 is your shift value
// if the result is negative, simply add 26 (amount of smallercase characters)
if (newCurrValue < 0) {
newCurrValue += 26;
}
newCurrValue += 'a'; // add 'a' again, to be within 'a' - 'z'
此外,尽管你犯了一点小错误,但无论如何你都不会在控制台上看到结果,因为你缺少一条日志语句:
System.out.println("Your encrypted text was: ");
for (int i = 0; i < encryptedTextArray.size(); i++) {
encryptedString += encryptedTextArray.get(i);
}
System.out.println(encryptedString); // output your result