作业就像一个凯撒密码,涉及两个字符[]

时间:2019-04-17 23:43:37

标签: java arrays

有两个char []。 ENGLISHARR将字母从A-Z存储在数组中,然后是a-z和SAURIANARR,将字母进行加扰,同时仍然保持大写,然后是Undercase,这两个数组都排除M和m以及特殊字符(例如?)。要么 !因此它们将按原样使用。将有一个String变量,该变量将以英语存储一个句子,并将该句子转换为一种称为Saurian的组合语言,该语言在名为Star Fox Adventures或Saurian to English的视频游戏中使用。

我在翻译String并将其转换为其他语言时遇到麻烦。

我尝试使用两个for循环,一个循环继续将要翻译的单词的长度,另一个循环使用单词的字母并在翻译后存储该字母。在for循环之后,我将获得一个if语句,该语句将构建一个字符串,该字符串将存储已在数组中找到的已翻译字母(Letter)。

我尝试使用Arrays.asList()。indexOf();看看是否可以存储找到字母的数组的索引值。然后使用存储的索引将字符存储存储在相反的数组中。

我的老师还说这是本学期最难的作业。这是我的第一个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);
                }

如果我添加一会儿while(index <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);
                    }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

  • 无需将$ git reset HEAD^转换为数组,只需使用word方法即可 访问角色。
  • 您无法创建基本类型的列表,因此charAt()将创建Arrays.asList(ENGLISHARR)的列表。代替char[],使用char[]
  • 应该为Character[]
  • 您应该使用if (indexOfYellow != -1)来获得字符indexOfYellow;
  • 不需要内循环
  • 如果未找到任何字符(例如空格),只需将其附加到字符串即可。

    SAURIANARR[indexOfYellow]