家庭作业就像一个涉及两个char []的凯撒密码

问题描述 投票:0回答:1

有两个char []。 ENGLISHARR将字母存储在数组中,从A-Z后跟a-z和SAURIANARR,字母表被扰乱,同时仍保持大写,然后是Undercase,两个数组都排除M和m以及特殊字符,例如?要么 !所以他们会按原样使用。将存在一个String变量,用于存储英语句子并将句子翻译成称为Saurian的组成语言,该语言在名为Star Fox Adventures或Saurian to English的视频游戏中使用。

我无法翻译String并将其转换为其他语言。

我已经尝试过使用两个for循环,一个继续用于翻译单词的长度,另一个用于翻译单词并在翻译后存储该单词。在for循环之后,我将有一个if语句,它构建一个String,它将存储已在数组中找到的已翻译的Letter。

我尝试过使用Arrays.asList()。indexOf();查看是否存储了找到该字母的数组索引的值。然后使用存储的索引在相反的数组中打印char存储。

我的老师也说这是本学期最艰难的任务。这是我的第一个java课程,我一直在尝试在课堂外学习。这也是for循环无法正常工作的原因。我还在努力学习Java,所以如果我犯了初学者的错误,我很抱歉。

public class as5
{

//English alphabet
    public static final char[] ENGLISHARR = {'A','B','C','D','E','F','G','H','I','J','K','L','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','n','o','p','q','r','s','t','u','v','w','x','y','z'};

//Saurian alphabet
public static final char[] SAURIANARR = {'U','R','S','T','O','V','W','X','A','Z','B','C','D','E','F','G','H','J','K','I','L','N','P','O','Q','u','r','s','t','o','v','w','x','a','z','b','c','d','e','f','g','h','j','k','i','l','n','p','o','q'};


public static final int ARRLENGTH = ENGLISHARR.length;

public static void main(String[] args)
{
    //String that will be converted into a char[]
    String word = "Hello World";

    // String that will be used to store the word after it has been translated and will be built using the for loops
    String saurianToEnglish = "";

    //Character Array that turns the given string into a char array
    char[] str = word.toCharArray();


    // For loop that loops as long as the input is Ex. "Hello World" is 11 characters long
    for (int i = 0; i < word.length(); i++)
    {
        //For loop that should loop through the ENGLISHARR and build the word
        for (int j = 0; j < ARRLENGTH; j++)
        {
            // indexOfYellow should store the index number for which the letter in the string was located in the array.
            int indexOfYellow = Arrays.asList(ENGLISHARR).indexOf(str[i]);
            System.out.println(indexOfYellow);
            int index = str[i];

            //Should Check if the character at index i is present in ENGLISHARR then it will save it to saurianToEnglish
            if (indexOfYellow == -1)
            {
                saurianToEnglish += SAURIANARR[index];

                 //This is just here to see if the if statement passed
                System.out.println("saurianToEnglish PASS   " + saurianToEnglish);
            }
            else
            {
                //This is just here to see if the if statement failed
                System.out.println("saurianToEnglish FAIL   " + indexOfYellow);
            }
        }
    }
}

}

例如,如果输入是“Hello World”

ENGLISHARR [1] ='H'

哪个需要翻译成蜥蜴

SAURIANARR [1] ='X'

所以总输入“Hello World”应该翻译成“Xocce Gebvt”

// Add while loop here 

if (indexOfYellow == -1)

                    saurianToEnglish += SAURIANARR[index];

                     //This is just here to see if the if statement passed
                    System.out.println("saurianToEnglish PASS   " + saurianToEnglish);
                }
                else
                {
                    //This is just here to see if the if statement failed
                    System.out.println("saurianToEnglish FAIL   " + indexOfYellow);
                }

如果我添加一段时间(索引<51),索引为int index = str [i];在if语句之前循环程序将运行一段时间所以我知道这里有问题。

我的老师也给了我一个如何构建for循环的提示。

for()--->迭代给定的单词

for() ---> which iterates through the ENGLISHARR to check if the letter is held in the array.

    if(found)
    {
    }
    else
    {
    }

这是带有实际运行的while循环的代码。

import java.util.Arrays;

public class as5
{
    //Character array to check if another array contains one of the following
    public static final char[] ENGLISHARR = {'A','B','C','D','E','F','G','H','I','J','K','L','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    public static final char[] SAURIANARR = {'U','R','S','T','O','V','W','X','A','Z','B','C','D','E','F','G','H','J','K','I','L','N','P','O','Q','u','r','s','t','o','v','w','x','a','z','b','c','d','e','f','g','h','j','k','i','l','n','p','o','q'};
    public static final int ARRLENGTH = ENGLISHARR.length;

    public static void main(String[] args)
    {
        //String that will be converted into a char[]
        String word = "Hello World";

        // String that will hold the index of the
        String saurianToEnglish = "";

        //Character Array that takes turns the given string into a char array
        char[] str = word.toCharArray();


        // For loop that loops as long as the input is Ex. "Hello World" is 11 characters long
        for (int i = 0; i < word.length(); i++)
        {
            //For loop that should loop through the ENGLISHARR and build the word
            for (int j = 0; j < ARRLENGTH; j++)
            {

                int indexOfYellow = Arrays.asList(ENGLISHARR).indexOf(str[i]);
                System.out.println(indexOfYellow);
                int index = str[i];

                //Should Check if the character at index i is present in ENGLISHARR then it will save it to indexOfYellow
                while(index < 51)

                    if (indexOfYellow == -1)
                    {
                        saurianToEnglish += SAURIANARR[str[i]];

                        System.out.println("saurianToEnglish PASS   " + saurianToEnglish);
                    }
                    else
                    {
                        //saurianToEnglish += SAURIANARR[indexOfYellow];
                        System.out.println("saurianToEnglish FAIL   " + indexOfYellow);
                    }
            }
        }
    }
}
java arrays
1个回答
0
投票
  • 无需将word转换为数组,只需使用charAt()方法来访问字符。
  • 您无法创建基本类型列表,因此Arrays.asList(ENGLISHARR)将创建一个char[]列表。而不是char[],使用Character[]
  • 它应该是if (indexOfYellow != -1)
  • 你应该使用indexOfYellow得到角色SAURIANARR[indexOfYellow];
  • 不需要内循环
  • 如果找不到任何字符,例如空格,只需将其附加到字符串即可。 // English alphabet public static final Character[] ENGLISHARR = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; // Saurian alphabet public static final Character[] SAURIANARR = { 'U', 'R', 'S', 'T', 'O', 'V', 'W', 'X', 'A', 'Z', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'I', 'L', 'N', 'P', 'O', 'Q', 'u', 'r', 's', 't', 'o', 'v', 'w', 'x', 'a', 'z', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'i', 'l', 'n', 'p', 'o', 'q' }; public static void main(String... args) { String word = "Hello World"; // String that will be used to store the word after it has been // translated and will be built using the for loops String saurianToEnglish = ""; // For loop that loops as long as the input is Ex. "Hello World" is 11 // characters long for (int i = 0; i < word.length(); i++) { // indexOfYellow should store the index number for which the letter in the string was located in the array. int indexOfYellow = Arrays.asList(ENGLISHARR).indexOf(word.charAt(i)); System.out.println(indexOfYellow); // Should Check if the character at index i is present in ENGLISHARR then it will save it to saurianToEnglish if (indexOfYellow != -1) { saurianToEnglish += SAURIANARR[indexOfYellow]; // This is just here to see if the if statement passed System.out.println("saurianToEnglish PASS " + saurianToEnglish); } else { saurianToEnglish += word.charAt(i); // This is just here to see if the if statement failed System.out.println("saurianToEnglish FAIL " + indexOfYellow); } } }
© www.soinside.com 2019 - 2024. All rights reserved.