[英]How can I navigate through a 2-d array and shuffle the words within that array?
因此,我需要帮助。 我正在使用二维数组,因此我想做的是连续浏览二维数组(从数组的左上方开始,就像您正在阅读的段落一样)。 我试图编写一个使用以下规则返回数组的方法:
1)如果单词以元音(a,e,i,o,u)开头:将单词与同一行上的前一个单词交换
2)如果该单词是该行的第一个单词,则将其替换为该单词上方的单词; 但是,如果单词在第一行,则不应进行交换。
3)如果单词以辅音开头,则交换单词的第一个和最后一个字符。
例如 2维数组:
米饭,鸡蛋,房间
苹果,爪哇,猫头鹰
已转换:
苹果,艾克,系泊
鸡蛋,猫头鹰,avaj
到目前为止,这就是我所要做的: 我已经准备好了测试器类并准备就绪,但是在下面的类中设置方法时遇到了麻烦。 这是我需要指导的关键。
public class WordShuffle
{
// Use this method signature
// The parameter is a 2-dim array of words
// The method will return a 2-dim array of shuffled words
public String[][] shuffleWords(String[][] words)
{
}
}
测试人员类别:
public class WordShuffleTester
{
// Don't change this tester except to change the values in the 2-dim array
public static void main(String[] args)
{
// This is the 2-dim array to test your method
String[][] words = {{ "doom", "candy", "apple"},
{"orange", "energy", "rat"},
{ "mad", "test", "cool"},
{ "red", "blue", "drain"}};
WordShuffle shuffler = new WordShuffle();
String[][] mixedUpWords = shuffler.shuffleWords(words);
// The following will print out each element of the returned array
for (int r=0; r < mixedUpWords.length; r++)
{
for (int c=0; c < mixedUpWords[r].length; c++)
{
System.out.print(mixedUpWords[r][c] + "\t");
}
System.out.println(" ");
}
}
}
如果有人可以进一步帮助我,那将是非常义务!
我很无聊,可以尝试一下,但是将来至少尝试自己解决问题(您的shuffleWords
方法完全是空的)。
public String[][] shuffleWords(String[][] words)
{
String vowels = "aeiou";
String temp = "";
for (int i=0; i < words.length; i++)
{
for (int j=0; j < words[i].length; j++)
{
if(vowels.contains(words[i][j].substring(0,1)) && j > 0){
temp = words[i][j];
words[i][j] = words [i][j-1];
words[i][j-1] = temp;
}
if(j == 0 && i > 0){
temp = words[i][j];
words[i][j] = words [i-1][j];
words[i-1][j] = temp;
}
if(!vowels.contains(words[i][j].substring(0,1))){
String s = words[i][j];
temp = s.substring(1,s.length()-1);
String first = s.substring(0,1);
String last = s.substring(s.length()-1,s.length());
words[i][j] = last + temp + first;
}
}
}
return words;
}
尽管此代码有效,但需要注意的是,当移动单词时(由于第一个或第二个规则),以辅音开头的单词的首字母和最后一个字母可能会被多次交换。 不确定这是否是预期的效果,但是可能需要对执行这些任务的顺序进行一些调整。 这是我使用WordShuffleTester
运行它时的输出
//Input:
doom candy apple
orange energy rat
mad test cool
red blue drain
//Output:
orange apple candy
mad mood tar
red test looc
energy elub nraid
这是我解决您问题的方法:
public class Main {
public static void main(String[] args) {
String[][] words = {{"rice", "egg", "room"},
{"apple", "java", "owl"}};
for (int i = 0; i < words.length; i++) { //number of rows
for (int j = 0; j < words[i].length; j++) { //number of columns
if (j > 0) { //if it's not in the first column
//if it starts w a vowel
if (words[i][j].substring(0, 1).equals("a") || words[i][j].substring(0, 1).equals("e") || words[i][j].substring(0, 1).equals("i") || words[i][j].substring(0, 1).equals("o") || words[i][j].substring(0, 1).equals("u")) {
//if it starts w a vowel, it swaps with the word 1 over to the left
swapWord(words, i, j, i, j - 1);
}
} else if (j == 0) { //if it is in the first column
if (i > 0) {
//swaps with the word directly above it
swapWord(words, i, j, i - 1, j);
}
}
//swaps based on consonance
if (!words[i][j].substring(0, 1).equals("a") && !words[i][j].substring(0, 1).equals("e") && !words[i][j].substring(0, 1).equals("i") && !words[i][j].substring(0, 1).equals("o") && !words[i][j].substring(0, 1).equals("u")) {
words[i][j] = swapLetter(words[i][j]);
}
}
}
//outputs the newly shifted array to the console
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(words[i][j] + " ");
}
System.out.println();
}
}
//swaps a word with another word
public static void swapWord(String[][] a, int i1, int j1, int i2, int j2) {
String temp = a[i1][j1];
a[i1][j1] = a[i2][j2];
a[i2][j2] = temp;
}
//swaps the first letter of a string with the last letter of the string
public static String swapLetter(String s) {
String newS = "";
newS += s.substring(s.length() - 1);
newS += s.substring(1, s.length() - 1);
newS += s.substring(0, 1);
return newS;
}
}
希望这可以帮助 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.