Java 如何在二维数组中导航并在该数组中洗牌单词?
所以我需要帮助。我使用的是二维数组,所以我要做的是在二维数组中导航一行(从数组的左上角开始,就像你在读一个段落一样)。 我正在尝试编写一个方法,该方法返回具有以下规则的数组: 1) 如果单词以元音开头(a、e、i、o、u):将单词与同一行上的前一个单词交换 2) 如果单词是行中的第一个单词,则将其与正上方的单词交换;但是,如果单词位于第一行,则不应发生交换 3) 如果单词以辅音开头,则交换单词的第一个和最后一个字符 例如2-dim阵列: 米饭、鸡蛋、房间 苹果、爪哇、猫头鹰 已转换: 苹果、艾克、摩尔 蛋,猫头鹰,阿瓦伊 这就是我到目前为止所做的: 我已经设置好了tester类,但在下面的类中设置方法时遇到了问题。这是我需要指导的关键 测试仪等级:Java 如何在二维数组中导航并在该数组中洗牌单词?,java,arrays,Java,Arrays,所以我需要帮助。我使用的是二维数组,所以我要做的是在二维数组中导航一行(从数组的左上角开始,就像你在读一个段落一样)。 我正在尝试编写一个方法,该方法返回具有以下规则的数组: 1) 如果单词以元音开头(a、e、i、o、u):将单词与同一行上的前一个单词交换 2) 如果单词是行中的第一个单词,则将其与正上方的单词交换;但是,如果单词位于第一行,则不应发生交换 3) 如果单词以辅音开头,则交换单词的第一个和最后一个字符 例如2-dim阵列: 米饭、鸡蛋、房间 苹果、爪哇、猫头鹰 已转换: 苹果、艾克
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(" ");
}
}
}
公共类WordShuffleTester
{
//除了更改2-dim阵列中的值外,不要更改此测试仪
公共静态void main(字符串[]args)
{
//这是用来测试您的方法的2维阵列
字符串[][]单词={{{“厄运”,“糖果”,“苹果”},
{“橙色”、“能量”、“老鼠”},
{“疯狂”,“测试”,“酷”},
{“红色”、“蓝色”、“排水管”};
WordShuffle shuffler=新的WordShuffle();
字符串[][]mixedpwords=shuffler.shuffleWords(单词);
//下面将打印出返回数组的每个元素
for(int r=0;r
如果有人能在这方面进一步帮助我,我将不胜感激 我已经厌倦了尝试一下,但以后至少自己尝试解决这个问题(你的
shuffleWords
方法完全是空的)
闻起来像是家庭作业。。。
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(" ");
}
}
}
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;
}
//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