简体   繁体   English

如何浏览二维数组并随机排列该数组中的单词?

[英]How can I navigate through a 2-d array and shuffle the words within that array?

So I need help with this. 因此,我需要帮助。 I'm working with two-dimensional arrays and so what I'm trying to do is to navigate through the 2-d arrays in a row (starting at the top left of the array like you are reading a paragraph). 我正在使用二维数组,因此我想做的是连续浏览二维数组(从数组的左上方开始,就像您正在阅读的段落一样)。 I trying to write a method that returns the array with the following rules: 我试图编写一个使用以下规则返回数组的方法:

1) If the word begins with a vowel (a, e, i, o, u) : swap the word with the previous word on the same row 1)如果单词以元音(a,e,i,o,u)开头:将单词与同一行上的前一个单词交换

2) If the word is the first word on the row, then swap it with the word just above it; 2)如果该单词是该行的第一个单词,则将其替换为该单词上方的单词; however, if the word is on the first row, then no swapping should occur. 但是,如果单词在第一行,则不应进行交换。

3) If the word begins with a consonant, then swap the first and last characters of the word. 3)如果单词以辅音开头,则交换单词的第一个和最后一个字符。

Ex. 例如 2-dim array: 2维数组:

rice , egg, room 米饭,鸡蛋,房间

apple, java, owl 苹果,爪哇,猫头鹰

Converted: 已转换:

apple, eicr, moor 苹果,艾克,系泊

egg, owl, avaj 鸡蛋,猫头鹰,avaj

This is what I have so far: I've got the tester class all set up and ready but I having trouble setting up the methods in the class below. 到目前为止,这就是我所要做的: 我已经准备好了测试器类并准备就绪,但是在下面的类中设置方法时遇到了麻烦。 This is essential what I need guidance for. 这是我需要指导的关键。

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) 
    {


    }

}

Tester Class: 测试人员类别:

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(" ");
        }



    }

}

If anyone could help me further with this it would be much obliged! 如果有人可以进一步帮助我,那将是非常义务!

I was bored enough to give this a shot, but in the future try to at least attempt to solve the problem yourself (your shuffleWords method was completely empty). 我很无聊,可以尝试一下,但是将来至少尝试自己解决问题(您的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;
}

While this code works, it is important to note that when words are moved (due to the 1st or 2nd rule) words that begin with consonants may have the first and last letter swapped multiple times. 尽管此代码有效,但需要注意的是,当移动单词时(由于第一个或第二个规则),以辅音开头的单词的首字母和最后一个字母可能会被多次交换。 Not sure if this is the intended effect, but some adjustments may need to be made to the order that these tasks are performed. 不确定这是否是预期的效果,但是可能需要对执行这些任务的顺序进行一些调整。 Here is the output when I ran it using your WordShuffleTester 这是我使用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

Here's how I solved your problem: 这是我解决您问题的方法:

  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;
}

} }

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM