Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何对从文本文件中随机选取的单词进行置乱_Java_Arrays_Character - Fatal编程技术网

Java 如何对从文本文件中随机选取的单词进行置乱

Java 如何对从文本文件中随机选取的单词进行置乱,java,arrays,character,Java,Arrays,Character,我正在尝试编写一个程序,从文本文件中选取一个随机单词,对其进行置乱,并允许用户通过一次交换两个索引位置来解读它 我有一个程序,它从文本文件中抓取一个随机单词,然后用上面的索引号打印出来 我不知道如何: 在屏幕上打印出单词之前,将其置乱,然后 如何让用户能够通过一次交换2个索引来循环,直到单词被解读 我是否可以编写一个方法来执行这些操作 这是到目前为止我的代码 import java.io.*; import java.util.*; public class Midterm { // clas

我正在尝试编写一个程序,从文本文件中选取一个随机单词,对其进行置乱,并允许用户通过一次交换两个索引位置来解读它

我有一个程序,它从文本文件中抓取一个随机单词,然后用上面的索引号打印出来

我不知道如何:

  • 在屏幕上打印出单词之前,将其置乱,然后
  • 如何让用户能够通过一次交换2个索引来循环,直到单词被解读 我是否可以编写一个方法来执行这些操作

    这是到目前为止我的代码

    import java.io.*;
    import java.util.*;
    
    public class Midterm { // class header
    
        public static void main(String[] args) { // Method header
    
            int option = 0;
            Scanner input = new Scanner(System.in);
            int scrambled;
            int counter = 0;
            int index1;
            int index2;     
    
            String[] words = readArray("words.txt");
            /*
             * Picks a random word from the array built from words.txt file. Prints
             * index with word beneath it.
             */
            int randWord = (int) (Math.random() * 11);
    
            for (int j = 0; j < words[randWord].length(); j = j + 1) {
                System.out.print(j);
            }
    
            System.out.print("\n");     
            char[] charArray = words[randWord].toCharArray();
            for (char c : charArray) {          
                System.out.print(c);
            }
            /*
             * Prompt the user for input to play game or quit.
             */
            System.out.println("\n");
            System.out.println("Enter 1 to swap a par of letters.");
            System.out.println("Enter 2 to show the solution and quit.");
            System.out.println("Enter 3 to quit.");     
    
            if (input.hasNextInt()) {
                option = input.nextInt();
                counter++;          
            }
            else {
                option = 3;
            }
            System.out.println("");
    
            if (option == 1) {
                System.out.println("Enter the two index locations to swap separated by a space. ");
                index1 = 0;
                index2 = 0;
                if (input.hasNextInt()) {
                    index1 = input.nextInt();
                    }
                else {
                    System.out.println("Please enter only numbers.");
                }
    
                if (input.hasNextInt()) {
                    index2 = input.nextInt();
                    }
                else {
                    System.out.println("Please enter only numbers.");
                }
            } 
            }   
    
    
    
        // end main
    
        public static String[] readArray(String file) {
            // Step 1:
            // Count how many lines are in the file
            // Step 2:
            // Create the array and copy the elements into it
    
            // Step 1:
            int ctr = 0;
            try {
                Scanner s1 = new Scanner(new File(file));
                while (s1.hasNextLine()) {
                    ctr = ctr + 1;
                    s1.nextLine();
                }
                String[] words = new String[ctr];
    
                // Step 2:
                Scanner s2 = new Scanner(new File(file));
                for (int i = 0; i < ctr; i = i + 1) {
                    words[i] = s2.next();
    
                }
                return words;
            } catch (FileNotFoundException e) {
    
            }
            return null;
    
        }
    }
    
    import java.io.*;
    导入java.util.*;
    公共类中期{//类标题
    公共静态void main(字符串[]args){//方法头
    int选项=0;
    扫描仪输入=新扫描仪(System.in);
    int加扰;
    int计数器=0;
    int index1;
    int index2;
    String[]words=readArray(“words.txt”);
    /*
    *从words.txt文件生成的数组中随机选取一个单词
    *索引下有单词。
    */
    int随机词=(int)(Math.random()*11);
    对于(int j=0;j
    您读取的文件不正确。做

    public static String[] readArray(String file) {
        int ctr = 0;
        try {
            Scanner s1 = new Scanner(new File(file));
            while (s1.hasNext()) {
                ctr = ctr + 1;
                s1.next();
            }
            //..rest of code
    

    我对您的代码做了一些相当大的修改,包括添加了一个扰码器方法。该程序几乎是完美的,只是你的文件“words.txt”不能容纳重复字母的单词。例如,黄色、绿色和紫色无法正确解读,但白色、灰色、蓝色、橙色或红色可以正常解读。除此之外,该程序运行良好。它选择一个随机单词,然后在求解时选择另一个单词,将最后一个单词更改为null,这样就不会再次拾取该单词。节目如下:

    import java.io.*;
    import java.util.*;
    
    public class Experiments { // class header
    
        private static String[] words = readArray("/Users/UserName/Desktop/words.txt"); //change to your location of the file
    
        public static void main(String[] args) { // Method header
            int option = 0;
            Scanner input = new Scanner(System.in);
            int counter = 0;
            String scrambledWord;
            int index1;
            int index2;     
    
            Random rand = new Random();
            int randWord = rand.nextInt(words.length);
    
            for (int j = 0; j < words[randWord].length(); j += 1) {
                System.out.print(j);
            }
    
            System.out.print("\n");     
            scrambledWord = scrambler(words[randWord]);
            System.out.println(scrambledWord);     
    
            System.out.println("\n");
            System.out.println("Enter 1 to swap a pair of letters.");
            System.out.println("Enter 2 to show the solution and quit.");
            System.out.println("Enter 3 to quit."); 
            option = input.nextInt();
    
            if (option == 1) {
                while (!scrambledWord.equals(words[randWord])) {
                    index1 = 0;
                    index2 = 0;
                    boolean validOption = false;
                    System.out.println("Enter the two index locations to swap separated by a space.");
                    while (!validOption) {
                        if (input.hasNextInt()) {
                            index1 = input.nextInt();
                            index2 = input.nextInt();  
                            validOption = true;
                        }
                        else {
                            System.out.println("Please enter only numbers.");
                            validOption = false;
                            break;
                        }
                    }
                    String letter1 = scrambledWord.substring(index1, index1+1);
                    String letter2 = scrambledWord.substring(index2, index2+1);
                    System.out.println("replacing " + letter1 + " with " + letter2 + "...");
                    if (index1 < index2) {
                        scrambledWord = scrambledWord.replaceFirst(letter2, letter1);                 
                        scrambledWord = scrambledWord.replaceFirst(letter1, letter2); 
                    } else {
                        scrambledWord = scrambledWord.replaceFirst(letter1, letter2);                 
                        scrambledWord = scrambledWord.replaceFirst(letter2, letter1); 
                    }
                    System.out.println();
                    for (int j = 0; j < words[randWord].length(); j += 1) {
                        System.out.print(j);
                    }
                    System.out.println("\n"+scrambledWord);
                    System.out.println();
                    counter++;
                    if (scrambledWord.equals(words[randWord])){
                        System.out.println("You did it! The word was " + words[randWord]);
                        System.out.println("You got it with " + counter + " replacements!");
                        words[randWord] = null;
                        if (words.length == 0){
                            System.out.println("I'm all out of words. You win!");
                            System.exit(0);
                        } else {
                            main(args);
                        }
                    }
                } 
    
            } else if (option == 2) {
                System.out.println(words[randWord]);
                System.exit(0);
            } else {
                System.exit(0);
            }
            input.close();
       }   
    
        //scrambles the word given to it
        private static String scrambler(String word) { 
            String scrambled = "";
            Random rand = new Random();
            int length;
            int index;
            String letter;
            String firststring;
            String secondstring;
    
            while (word.length()>0) {
                length = word.length();
                index = rand.nextInt(length);
                letter = word.substring(index, index+1);
                firststring = word.substring(0, index);
                secondstring = word.substring(index+1);
                word = firststring + secondstring;
                scrambled += letter;
            }
            return scrambled;
        }
    
        public static String[] readArray(String file) {
            int ctr = 0;
            try {
                Scanner s1 = new Scanner(new File(file));
                while (s1.hasNextLine()) {
                    ctr = ctr + 1;
                    s1.nextLine();
                }
                String[] words = new String[ctr];
    
                // Step 2:
                Scanner s2 = new Scanner(new File(file));
                for (int i = 0; i < ctr; i = i + 1) {
                    words[i] = s2.next();
    
                }
                return words;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            return null;
            }
    }
    

    这些显然不是唯一可以输入的单词,您可以添加任意数量的单词,只要它们不出现两次相同的字母。

    事实上,我遇到了一个小问题。如果用户在“输入两个索引位置…”部分输入无效条目,代码似乎无法正确处理错误。程序似乎进入了一个无限循环,我必须手动停止它。我知道Java中有一个try/catch方法和一个异常处理过程,但我仍在阅读它,我想我明白了。我删除了while(!validoption)行,并为每个索引将if/else循环分成两个单独的循环。index1和index2现在有自己的if/else循环。看来节目结束得很干净。有没有办法让它返回到要求索引位置,而不是退出该计划?我会的。再次感谢!
    orange
    red
    brown
    black
    white
    blue
    tiger
    horse
    bugs
    stack
    overflow
    pathfinder
    extra
    zealous
    wisdom
    under
    above
    death
    life
    second
    first
    frost
    forest