对于在线Java课程,我正在编写Caesar Cipher。在这里,您输入一个字符串和一个移位编号,答案随移位字符串一起返回,其中所有字符都按移位编号将字母“向下”移位。对于我的程序,我还进行了分组练习,在该练习中,我必须将移位后的字符串分组为一定数量的组(例如,“ SGHSJDGDKGHSA”按3分组是“ SGH SJD GDK GHSA”)。如果字符串中的字符数不能被分组号整除,则程序会在字符串的末尾添加小写字母x(例如:“ SGHSJDGDKGHSA”,按4分组为“ SGHS JDGD KGHS Axxx”)。
我的程序一直工作到分组功能(我的代码中的groupify方法)。不返回带有组的字符串。有关如何解决此问题的任何建议?
import java.util.Scanner;
public class Crypto {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a phrase.");
String userText = input.nextLine();
String normText = normalizeText(userText);
System.out.println("Enter a shift.");
String strUserShift = input.nextLine();
int userShift = Integer.parseInt(strUserShift);
String shiftText = shiftAlphabet(userShift, normText);
System.out.println("Shifted Text: " + shiftText);
System.out.println("Enter a grouping number.");
String strUserGroupNum = input.nextLine();
int userGroupNum = Integer.parseInt(strUserGroupNum);
String encryptedText = groupify(shiftText, userGroupNum);
System.out.print(encryptedText);
}
private static String normalizeText(String preText) {
StringBuilder newText = new StringBuilder();
for(int charNum = 0; charNum < preText.length(); charNum++) {
char charText = preText.charAt(charNum);
if (charText == '.' || charText == ',' || charText == ':' || charText == ';' || charText == '\'' || charText == '\\' || charText == '"' || charText == '!' || charText == '?' || charText == '(' || charText == ')') {
newText.append("");
} else {
String stringText = "" + charText;
stringText = stringText.toUpperCase();
newText.append(stringText);
}
}
return(newText.toString());
}
private static String shiftAlphabet(int shift, String normText) {
StringBuilder shiftedText = new StringBuilder();
String alphabet = "abcdefghijklmnopqrstuvwxyz".toUpperCase();
for (int index = 0; index < normText.length(); index++) {
char currentChar = normText.charAt(index);
int currentCharNum = alphabet.indexOf(currentChar);
int newCharNum = currentCharNum + shift;
while (newCharNum >= 26 || newCharNum < 0) {
if (newCharNum >= 26) {
newCharNum -= 26;
} else {
newCharNum += 26;
}
}
char newChar = alphabet.charAt(newCharNum);
shiftedText.append(newChar);
}
return(shiftedText.toString());
}
private static String groupify(String shiftText, int groupNum) {
StringBuilder sbShiftText = new StringBuilder();
StringBuilder sbGroupText = new StringBuilder();
String finalText = "";
while (sbShiftText.length() % groupNum != 0) {
sbShiftText.append("x");
}
for (int i = 1; i <= sbShiftText.length(); i++) {
if (groupNum == 1) {
finalText = shiftText;
} else {
// everything w group number > 1
if (groupNum % i == 0) {
sbGroupText.append(" ");
} else {
String tempStr = sbShiftText.charAt(i) + "";
sbGroupText.append(tempStr);
}
finalText = sbGroupText.toString();
}
}
return(finalText);
}
}
答案 0 :(得分:0)
sbShiftText
已定义但从未初始化,因此sbShiftText.length()
无效。
应该是这样的:
StringBuilder sbShiftText = new StringBuilder(shiftText);
答案 1 :(得分:0)
下面是对工作https://repl.it/@omurbekjk/DesertedDistinctLight稍作修改的解决方案:
import java.util.Scanner;
public class Main {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a phrase.");
String userText = input.nextLine();
String normText = normalizeText(userText);
System.out.println("Enter a shift.");
String strUserShift = input.nextLine();
int userShift = Integer.parseInt(strUserShift);
String shiftText = shiftAlphabet(userShift, normText);
System.out.println("Shifted Text: " + shiftText);
System.out.println("Enter a grouping number.");
String strUserGroupNum = input.nextLine();
int userGroupNum = Integer.parseInt(strUserGroupNum);
String encryptedText = groupify(shiftText, userGroupNum);
System.out.print(encryptedText);
}
private static String normalizeText(String preText) {
StringBuilder newText = new StringBuilder();
for(int charNum = 0; charNum < preText.length(); charNum++) {
char charText = preText.charAt(charNum);
if (charText == '.' || charText == ',' || charText == ':' || charText == ';' || charText == '\'' || charText == '\\' || charText == '"' || charText == '!' || charText == '?' || charText == '(' || charText == ')') {
newText.append("");
} else {
String stringText = "" + charText;
stringText = stringText.toUpperCase();
newText.append(stringText);
}
}
return(newText.toString());
}
private static String shiftAlphabet(int shift, String normText) {
StringBuilder shiftedText = new StringBuilder();
String alphabet = "abcdefghijklmnopqrstuvwxyz".toUpperCase();
for (int index = 0; index < normText.length(); index++) {
char currentChar = normText.charAt(index);
int currentCharNum = alphabet.indexOf(currentChar);
int newCharNum = currentCharNum + shift;
while (newCharNum >= 26 || newCharNum < 0) {
if (newCharNum >= 26) {
newCharNum -= 26;
} else {
newCharNum += 26;
}
}
char newChar = alphabet.charAt(newCharNum);
shiftedText.append(newChar);
}
return(shiftedText.toString());
}
private static String groupify(String shiftText, int groupNum) {
StringBuilder sbShiftText = new StringBuilder(shiftText);
StringBuilder sbGroupText = new StringBuilder();
String finalText = "";
String temp = "";
int remainder = sbShiftText.length() % groupNum;
while (remainder>0) {
sbShiftText.append("x");
remainder--;
}
int grouping = 0;
System.out.println(sbShiftText);
System.out.println(groupNum);
for (int i = 0; i < sbShiftText.length(); i++) {
temp += sbShiftText.charAt(i);
grouping++;
if (groupNum == grouping) {
finalText += temp + " ";
temp="";
grouping=0;
}
}
return(finalText);
}
}
答案 2 :(得分:0)
首先尝试调试groupify()方法。如果不知道如何调试它,那就学习它。
groupify()方法中的问题是您声明此问题
StringBuilder sbShiftText = new StringBuilder();
sbShiftText为空,因此sbShiftText.length()返回0。因此,while循环和for循环根本没有运行。
首先用shiftText之类初始化sbShiftText
StringBuilder sbShiftText = new StringBuilder(shiftText);
答案 3 :(得分:0)
private static String groupify(String shiftText, int groupNum) {
StringBuilder sbShiftText = new StringBuilder();
StringBuilder sbGroupText = new StringBuilder();
String finalText = "";
*sbShiftText.append(shiftText);*
while (sbShiftText.length() % groupNum != 0) {
sbShiftText.append("x");
}
for (int i = 0; i <sbShiftText.length(); i++) {
if (groupNum == 1) {
finalText = shiftText;
}
else {
if (*i%groupNum* == 0&&i!=0) {
sbGroupText.append(" ");
}
String tempStr = sbShiftText.charAt(i) + "";
sbGroupText.append(tempStr);
finalText = sbGroupText.toString();
}
}
return finalText;
}