Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 嵌套for循环,不拾取数组中的第一个实例_Java_Loops_For Loop - Fatal编程技术网

Java 嵌套for循环,不拾取数组中的第一个实例

Java 嵌套for循环,不拾取数组中的第一个实例,java,loops,for-loop,Java,Loops,For Loop,我正在尝试对一个单词进行编码,但我不确定为什么我的for循环没有拾取第一个实例0。此方法的输入为“this”和3。该方法的输出为klv。所以我的循环一定不能正常工作,因为字母T被跳过了。我的循环有什么问题 String encodeWord(String word, int Shift) { //word = "This" //Shift = 3, is how far the letter is shifted to the right of the orig

我正在尝试对一个单词进行编码,但我不确定为什么我的for循环没有拾取第一个实例0。此方法的输入为“this”和3。该方法的输出为klv。所以我的循环一定不能正常工作,因为字母T被跳过了。我的循环有什么问题

String encodeWord(String word, int Shift) {

        //word = "This"
        //Shift = 3, is how far the letter is shifted to the right of the original 
        char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
        char[] temp = word.toCharArray();
        char[] FA = new char[temp.length];
        String tempWord = "";
        StringBuilder sb = new StringBuilder(64);




    for (int x = 0; x < word.length(); x++) {

            for (int y = 0; y < alphabet.length; y++) {
                if (word.charAt(0) == alphabet[y]) {
                    FA[0] = alphabet[y + shift];
                    System.out.println(FA[0]);
                }
            }
        }

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

            for (int j = 0; j < alphabet.length; j++) {
                if (word.charAt(i) == alphabet[j]) {
                    FA[i] = alphabet[j + shift];
                    sb.append(FA[i]);
                    System.out.println(FA[i]);
                }

            }
        }
        System.out.println(sb);

        return sb.toString();
    }
String-encodeWord(字符串字,int-Shift){
//word=“这个”
//Shift=3,表示字母向右移动的距离
char[]alphabet=“abcdefghijklmnopqrstuvxyz”。toCharArray();
char[]temp=word.toCharArray();
char[]FA=新字符[temp.length];
字符串tempWord=“”;
StringBuilder sb=新的StringBuilder(64);
对于(int x=0;x
字母“T”与字母“T”不同,因此,由于在数组中只找到字母“T”,程序将无法找到字母“T”的匹配项

代码的另一个问题是,如果输入包含字母“x”、“y”或“z”,则会出现索引越界异常,因为数组中在这些字母之后没有3个字母。

public static String encoder(String word,int shift)
public static String encoder(String word, int shift)
{
    static const int max_char = 122; //letter 'z'
    static const int min_char = 97;  //letter 'a'
    char[] c_array = word.toCharArray();
    char[] encoded_string = new char[c_arary.length()];

    for(for i = 0; i < c_array.length(); i++)
    {
        if( ((int)c + shift) > max_char) //makes sure that the ascii isnt a non number
        {
             encoded_string[i] = (min_char + (int)c + shift - max_char );  // this will correct the overflow 
        }

        c = c + shfit;
    }

    return encoded_string;
}
{ 静态常量int max_char=122;//字母“z” 静态常量int min_char=97;//字母“a” char[]c_数组=word.toCharArray(); char[]encoded_string=新字符[c_arary.length()]; for(对于i=0;imax_char)//确保ascii不是非数字 { encoded_string[i]=(min_char+(int)c+shift-max_char);//这将更正溢出 } c=c+shfit; } 返回编码的字符串; }
这是一种更简单的方法。。。您的循环也有一些逻辑错误。。我抓到的第一个是在第一圈。。。如果你的单词中有一个z,你的字母表数组就会溢出


这是使用Ascii表格方式

如果您试图实现,为什么不看看web上的许多示例中的一个呢?--这段代码有太多的问题,我甚至不能在评论中列出它们。顺便说一句,这只涉及较低的情况