Java的基本字符串帮助(这里是新手)

Java的基本字符串帮助(这里是新手),java,string,Java,String,我有一个程序 提示用户输入单词、短语或句子 然后我应该“加密”你输入的内容13次,每次都打印。最后打印的内容应与用户输入相匹配 我只能加密字母字符。其他一切都是一样的 通过查找每个字符的ASCII值然后将其增加2来“加密”。如果字母大小写发生变化,则改为以a开头表示小写,或以a开头表示大写 我的代码现在只给我1个加密,在2处停止。它也只适用于第一个字母。我们班还没有学会数组,但是如果我们愿意,我们可以试试 import java.util.Scanner; public class Encryp

我有一个程序

  • 提示用户输入单词、短语或句子
  • 然后我应该“加密”你输入的内容13次,每次都打印。最后打印的内容应与用户输入相匹配
  • 我只能加密字母字符。其他一切都是一样的
  • 通过查找每个字符的ASCII值然后将其增加2来“加密”。如果字母大小写发生变化,则改为以a开头表示小写,或以a开头表示大写
  • 我的代码现在只给我1个加密,在2处停止。它也只适用于第一个字母。我们班还没有学会数组,但是如果我们愿意,我们可以试试

    import java.util.Scanner;
    public class Encrypt{
    
        Scanner keyboard = new Scanner(System.in);
        String message = new String();
        String g = new String();
        char y;
        public void input(){
            System.out.printf("Welcome to Encrypt.java. Please enter a word,phrase, or sentence. \n");
            System.out.println();
            System.out.print("->    ");
            message = keyboard.nextLine();
        }
        public void code(){
            int x = message.length()-1;
            boolean enter = true;
    
            for(int i = 0; i <= x; i++){
                int j = message.charAt(i);
                if((j >= 32 && j <=64) ||
                   (j >= 91 && j <=96) ||
                   (j >= 123 && j <= 127)){ 
                }
    
                else if((j >= 65 && j <= 90)){
                    j = j + 2;
                    if(j>90){
                        j = (j-90)+64;
                    }
                }
                else if(j>=97 && j <= 122){
                    j = j + 2;
                    if(j>122){
                        j = (j-122) + 96;
                    }
                }
    
                if(enter == true){
                    System.out.println();
                    System.out.print("  ");
                    enter = false;
                }
                y = (char)(j);
                g = g + y;
                message = g;
    
                x = message.length()-1;             
            }
            System.out.print(g);
            System.out.println();
        }
    
        public void print(){
            for(int i = 1; i <= 13; i ++){
                System.out.println("Encryption " + i + ":");
                this.code();
            }
    
        }
        public static void main(String [] args){
            Encrypt e = new Encrypt();
            e.input();
            e.print();
    
        }
    }
    
    import java.util.Scanner;
    公共类加密{
    扫描仪键盘=新扫描仪(System.in);
    字符串消息=新字符串();
    字符串g=新字符串();
    chary;
    公共无效输入(){
    System.out.printf(“欢迎使用Encrypt.java。请输入单词、短语或句子。\n”);
    System.out.println();
    系统输出打印(“->”);
    message=keyboard.nextLine();
    }
    公共无效代码(){
    int x=message.length()-1;
    布尔输入=true;
    对于(int i=0;i=32&&j=91&&j=123&&j=65&&j=90){
    j=(j-90)+64;
    }
    }
    否则如果(j>=97和&j 122){
    j=(j-122)+96;
    }
    }
    如果(输入==true){
    System.out.println();
    系统输出打印(“”);
    输入=假;
    }
    y=(char)(j);
    g=g+y;
    消息=g;
    x=message.length()-1;
    }
    系统输出打印(g);
    System.out.println();
    }
    公开作废印刷品(){
    对于(inti=1;i这是您的问题

    y = (char) (j);
    g = g + y;
    message = g;
    
    在第一次运行中,
    g
    只有一个字符,您正在生成
    message=g;
    。这使得
    message
    只有一个字符
    g
    。我运行它删除了
    message=g
    ,它工作正常。我不知道它是否是您想要的输出,但至少它已经通过了
    Encryption 1

    注意:您应该真正了解如何使用调试器。这就是我发现问题的原因。

    两件事:

    • 每次迭代后重置变量g
    • 信息的正确放置


    我很困惑如何从
    charAt
    方法中得到一个整数,即
    int j=message.charAt(I)
    @Samo:将
    char
    赋值给
    int
    是合法的。字符的ASCII码将被赋值。参见@Samo,java中的
    char
    是一个无符号16位整数。从API的角度看,它与整数类型有点不同,因为类似于
    System.out.print('x'))
    特别对待它们。@peeskillet-我不是OP。很遗憾我不能。当你说它停止时,它是正常退出还是出现异常?
    print()
    没有重复
    input()
    命令。这不是你想要的:获得重复的用户输入吗?如果不是,我一定误解了你的问题此
    If
    具有
    否则If
    组件。第一个条件只是排除(不适用于)一些范围。@PM77-1请发布一个当前输出和所需输出的示例。如果我们有这些信息,就更容易看出问题出在哪里。很难从代码和问题中看出问题所在。因此,如何使消息等于字符串g?您是否运行了它并查看了输出?您明白我关于可能无法获得消息的意思了吗你想要的输出。我假设你想在加密邮件的行中得到与原始邮件相同大小的内容,而不是13倍大小的内容,对吗?
     public void code() {
        int x = message.length() - 1;
        boolean enter = true;
        g = "";
        for (int i = 0; i <= x; i++) {
            int j = message.charAt(i);
                if ((j >= 32 && j <= 64) || (j >= 91 && j <= 96)
            || (j >= 123 && j <= 127)) {
                }
    
            else if ((j >= 65 && j <= 90)) {
                j = j + 2;
                if (j > 90) {
                    j = (j - 90) + 64;
                    }
            } else if (j >= 97 && j <= 122) {
                j = j + 2;
                if (j > 122) {
                    j = (j - 122) + 96;
                }
            }
            if(enter == true){
                System.out.println();
                System.out.print("  ");
                enter = false;
            }
            y = (char) (j);
            g = g + y;
        }
        message = g;
    }
    
    Welcome to Encrypt.java. Please enter a word,phrase, or sentence. 
    
    ->    abba
    Encrypt.code() message >> abba
    Encrypt.code() message >> cddc
    Encrypt.code() message >> effe
    Encrypt.code() message >> ghhg
    Encrypt.code() message >> ijji
    Encrypt.code() message >> kllk
    Encrypt.code() message >> mnnm
    Encrypt.code() message >> oppo
    Encrypt.code() message >> qrrq
    Encrypt.code() message >> stts
    Encrypt.code() message >> uvvu
    Encrypt.code() message >> wxxw
    Encrypt.code() message >> yzzy