Java加密项目(Java.lang.StringIndexOutOfBoundsException) import java.util.Scanner; 公共类Oppish_代码2{ 公共静态void main(字符串[]args){ 字符串s=“bcdfghjklmnpqrstvwxz”; 字符串ss=“aeiouy”; System.out.println(“输入要加密的行:”); 扫描仪sc=新的扫描仪(System.in); 字符串未编码=sc.next(); System.out.println(“加密行:”); 对于(int i=0;i

Java加密项目(Java.lang.StringIndexOutOfBoundsException) import java.util.Scanner; 公共类Oppish_代码2{ 公共静态void main(字符串[]args){ 字符串s=“bcdfghjklmnpqrstvwxz”; 字符串ss=“aeiouy”; System.out.println(“输入要加密的行:”); 扫描仪sc=新的扫描仪(System.in); 字符串未编码=sc.next(); System.out.println(“加密行:”); 对于(int i=0;i,java,exception,encryption,project,indexoutofboundsexception,Java,Exception,Encryption,Project,Indexoutofboundsexception,我一直在这个项目上工作的乐趣和关闭,但最近我觉得好像我非常接近让它工作,但一个错误被抛出,我不知道如何修复它。代码的目的是在任何辅音的末尾添加“op”,在元音的末尾添加“op”。这是由童年的一种用代码说话的方式激发出来的,因此只有你和一个朋友知道对方在说什么。任何帮助都将不胜感激,谢谢 检查未编码字符串中的字符是元音还是辅音,不需要循环太多次 我使用String.indexOf方法简化了您的程序。您只需检查未编码字符串中的字母是否存在于元音字符串或辅音字符串中 这是: import java.u

我一直在这个项目上工作的乐趣和关闭,但最近我觉得好像我非常接近让它工作,但一个错误被抛出,我不知道如何修复它。代码的目的是在任何辅音的末尾添加“op”,在元音的末尾添加“op”。这是由童年的一种用代码说话的方式激发出来的,因此只有你和一个朋友知道对方在说什么。任何帮助都将不胜感激,谢谢

检查未编码字符串中的字符是元音还是辅音,不需要循环太多次

我使用
String.indexOf
方法简化了您的程序。您只需检查未编码字符串中的字母是否存在于
元音
字符串或
辅音
字符串中

这是:

import java.util.Scanner;
public class Oppish_Coder2 {

    public static void main(String[] args) {
        String s = "bcdfghjklmnpqrstvwxz";
        String ss = "aeiouy";

        System.out.println("Enter line to encrypt: ");

        Scanner sc = new Scanner (System.in);
        String uncoded = sc.next();

        System.out.println("Encrypted line: ");

            for (int i = 0; i < uncoded.length(); i++){

                for (int ii = 0; ii < s.length(); ii++){

                    if (uncoded.charAt(i) == s.charAt(ii)){
                        System.out.print(uncoded.charAt(i) + "op ");
                    }
                    else {
                        for (int iii = 0; iii < ss.length(); iii++){
                            if (uncoded.charAt(i) == ss.charAt(ii)){
                                System.out.print(uncoded.charAt(i) + " ");
                            }
                        }
                    }   
                }
            }
        sc.close();
    }
}
import java.util.Scanner;
公共类Oppish_代码2{
公共静态void main(字符串[]args){
字符串辅音=“bcdfghjklmnpqrstvwxz”;
字符串元音=“aeiouy”;
System.out.println(“输入要加密的行:”);
扫描仪sc=新的扫描仪(System.in);
字符串未编码=sc.next();
System.out.println(“加密行:”);
对于(int i=0;i-1){
系统输出打印(c+“op”);
}else if(元音索引of(c)>-1){
系统输出打印(c+“”);
}
}
sc.close();
}
}

希望这有帮助

我不认为你的代码,如张贴的,是任何接近工作。请花些时间考虑你要做的事情的逻辑,并相应地修正你的循环。我建议您阅读一本教程,其中介绍了
break
continue
语句。在第三个for循环中,您将比较
uncoded.charAt(I)==ss.charAt(ii)
,而不是
uncoded.charAt(I)==ss.charAt(iii)
。此外,该循环不应嵌套。忠告;使用不同的字母作为循环变量。
import java.util.Scanner;

public class Oppish_Coder2 {

    public static void main(String[] args) {
        String consonants = "bcdfghjklmnpqrstvwxz";
        String vowels = "aeiouy";

        System.out.println("Enter line to encrypt: ");

        Scanner sc = new Scanner(System.in);
        String uncoded = sc.next();

        System.out.println("Encrypted line: ");

        for (int i = 0; i < uncoded.length(); i++) {
            char c = uncoded.charAt(i);
            if (consonants.indexOf(c)>-1) {
                System.out.print(c + "op ");
            } else if (vowels.indexOf(c)>-1) {
                System.out.print(c + " ");
            }
        }
        sc.close();
    }
}