Java 程序不返回字符串

Java 程序不返回字符串,java,Java,对于一门在线java课程,我正在编写Caesar密码。在这里,您输入一个字符串和一个移位号,然后返回移位字符串,其中所有字符都按移位号在字母表中“向下”移位。对于我的程序,我还有一个分组练习,其中我必须将移位的字符串分组为特定数字的组(例如:按3分组的“SGHSJDGDKGHSA”是“SGH SJD GDK GHSA”)。如果字符串中的字符数不能被分组数整除,则程序会在字符串末尾添加小写的x(例如:“SGHSJDGDKGHSA”按4分组为“SGHS JDGDKGHS Axxx”) 我的程序一直工

对于一门在线java课程,我正在编写Caesar密码。在这里,您输入一个字符串和一个移位号,然后返回移位字符串,其中所有字符都按移位号在字母表中“向下”移位。对于我的程序,我还有一个分组练习,其中我必须将移位的字符串分组为特定数字的组(例如:按3分组的“SGHSJDGDKGHSA”是“SGH SJD GDK GHSA”)。如果字符串中的字符数不能被分组数整除,则程序会在字符串末尾添加小写的x(例如:“SGHSJDGDKGHSA”按4分组为“SGHS JDGDKGHS 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);
    }
}
import java.util.Scanner;
公开类密码{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入短语”);
字符串userText=input.nextLine();
字符串normText=normalizeText(userText);
System.out.println(“输入一个班次”);
字符串strUserShift=input.nextLine();
int userShift=Integer.parseInt(strUserShift);
字符串shiftText=shiftAlphabet(userShift,normText);
System.out.println(“移位文本:“+shiftText”);
System.out.println(“输入分组号”);
字符串strUserGroupNum=input.nextLine();
int userGroupNum=Integer.parseInt(strUserGroupNum);
字符串encryptedText=groupify(shiftText,userGroupNum);
系统输出打印(加密文本);
}
私有静态字符串normalizeText(字符串借口){
StringBuilder newText=新StringBuilder();
for(int charNum=0;charNum
StringBuilder sbShiftText = new StringBuilder();
StringBuilder sbShiftText = new StringBuilder(shiftText);
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;
    }