如何打乱数组中不同字符串的字符?

How to shuffle the characters of different strings in an array?

我正在尝试读取 CSV 文件,然后随机排列数组中字符串的字符。 CSV 文件包含从英语到德语的词典。这是它的样子:

吃、喝

玩耍,玩耍

睡觉,睡觉

所需的输出应如下所示:

吃森森

玩lpsie

睡觉 fenlahsc

到目前为止,这是我的代码(我一直收到错误“无法从静态上下文中引用非静态方法 shuffle(String)”):

public class Shuffle { 
    public static void main(String[] args) {
        String path = "/Users/SaberKlai/Documents/vokabeln.csv";
        String line = "";

        try {
            BufferedReader br = new BufferedReader(new FileReader(path));

            Shuffle s = new Shuffle();

            while ((line = br.readLine()) != null) {
               

                String[] values = line.split(",");               
                System.out.println(values[0] + " " + shuffle(values[1]));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void shuffle(String input){
        List<Character> characters = new ArrayList<Character>();
        for(char c:input.toCharArray()){
            characters.add(c);
        }
        StringBuilder output = new StringBuilder(input.length());
        while(characters.size()!=0){
            int randPicker = (int)(Math.random()*characters.size());
            output.append(characters.remove(randPicker));
        }
        System.out.println(output.toString());
    }
}

               

您可以使用'this'关键字直接引用当前对象。 而不是

System.out.println(values[0] + " " + shuffle(values[1]));

您可以使用:

System.out.println(values[0] + " " + this.shuffle(values[1]));

此外,只需删除 :

Shuffle s = new Shuffle();

在旁注中,您可能想阅读更多有关 OOPS 概念的信息!

问题是您正试图从静态方法 (main()) 调用实例方法 (shuffle())。您需要将 shuffle() 设为静态方法。

除此之外,你不能做System.out.println(values[0] + " " + shuffle(values[1])),因为shuffle()方法returns没有String,实际上它returns什么也没有。因此,您还需要将这段代码更改如下:

public class Shuffle { 
    public static void main(String[] args) {
        String path = "/Users/SaberKlai/Documents/vokabeln.csv";
        String line = "";

        try {
            BufferedReader br = new BufferedReader(new FileReader(path));

            while ((line = br.readLine()) != null) {
                String[] values = line.split(",");
                System.out.print(values[0] + " ");
                shuffle(values[1]);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void shuffle(String input){
        List<Character> characters = new ArrayList<Character>();
        for(char c:input.toCharArray()){
            characters.add(c);
        }
        StringBuilder output = new StringBuilder(input.length());
        while(characters.size()!=0){
            int randPicker = (int)(Math.random()*characters.size());
            output.append(characters.remove(randPicker));
        }
        System.out.println(output.toString());
    }
}

您的随机播放方法是非静态的。只需将其设为静态并设为 return String 而不是 void

public static String shuffle(String input) {
...
return output.toString();
}

或使用对您对象的引用 Shuffle s

System.out.println(values[0] + " " + s.shuffle(values[1]));