java - Java Caesar Shift 字符打印失败
问题描述
我正在尝试开发一个非常基本的程序(我稍后会添加)来对用户输入的文本执行凯撒移位。
我有很多工作,但是当我试图打印出“加密”字符串时,它不起作用。我正在使用 Netbeans IDE,它只是打印出一个空白值。我在错误检查中添加了额外的打印语句,并且我相信我的“加密” - 即更改字符,正在正确发生,但是当我将它重新转换为字符时,某些事情会失败。我的代码如下:
/*
* 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表和你的表达方式
newCurrValue = ((currValue-3) % 26);
您从当前值中减去 3,然后取 mod 26,这保证您的结果在 0 到 26 的范围内。如果您在表中查找,您会发现既没有a
to的值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
推荐阅读
- google-chrome - 我可以让 Cypress 运行 Chrome 的测试版吗?
- sql - 尝试在带有 varchar 的 WHERE 子句中使用 IN 但遇到整理问题
- java - Java 使用 Saxon (s9api) 转换 XML:如何在资源中添加输入文件?
- javascript - 以编程方式比较/使用不同网站的数据
- macos - SHELL/AppleScript:在“取消”时停止 AppleScript 并退出 Shell
- ios - 变换标签字体大小
- c# - 这个 NHibernate 代码如何影响提交?
- windows - 选择 + 鼠标中键不再粘贴到 GitBash
- c++ - 插入二叉搜索树时出现分段错误
- css - Angular 宿主元素:设置焦点样式的最简单方法是什么