【发布时间】:2019-04-17 23:43:37
【问题描述】:
有两个char[]。 ENGLISHARR 将字母表存储在数组中,从 A-Z 后跟 a-z 和 SAURIANARR 字母表被打乱,同时仍然保持大写字母后跟小写字母,这两个数组都排除了 M 和 m 以及特殊字符,例如 ?或者 !所以它们将按原样使用。将有一个字符串变量,它将用英语存储一个句子,并将该句子翻译成一种称为 Saurian 的合成语言,该语言在名为 Star Fox Adventures 或 Saurian 的视频游戏中使用到英语。
我在翻译字符串并将其转换为其他语言时遇到问题。
我尝试使用两个 for 循环,一个持续到要翻译的单词的长度,另一个遍历单词的字母并在翻译后存储字母。在 for 循环之后,我将有一个 if 语句,它构建一个字符串,如果在数组中找到它,它将存储已翻译的字母。
我尝试过使用 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'
这需要翻译成saurian
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);
}
如果我在索引为 int index = str[i] 的情况下添加一段时间 while(index
我的老师还给了我一个关于如何构建 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);
}
}
}
}
}
【问题讨论】: