2017-05-14 23 views
1

ランダムな単語をテキストファイルから選択してスクランブルし、2つのインデックス位置をスワップして解読できるようにするプログラムを作成しようとしています。時間。テキストファイルからランダムに選択された単語をスクランブルする方法

私は、テキストファイルからランダムな単語を取得し、その上にあるインデックス番号を使って出力するプログラムを持っています。

私は考え出すトラブルを抱えていますどのように:

  1. それが画面上に出力し、そして前にスクランブル単語をゲットで2つのインデックスをスワッピングをループできるようにするには、ユーザーを取得するにはどのよう
  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; 

    } 
} 

答えて

1

私はスクランブラーメソッドを追加するなど、あなたのコードにかなり大きな変更を加えました。プログラムはほぼ完璧です。あなたのファイル "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; 
     } 
} 

そしてここでは、ファイルwords.txt内の単語のリストは、(私はかなりのリピート手紙を持っていなかった私の頭に浮かん何でも書い)です:

orange 
red 
brown 
black 
white 
blue 
tiger 
horse 
bugs 
stack 
overflow 
pathfinder 
extra 
zealous 
wisdom 
under 
above 
death 
life 
second 
first 
frost 
forest 

これらの明らかに唯一の言葉ではありませんが、同じ文字の2回の出現がない限り、あなたが望む数だけ追加することができます。

+1

ありがとうございました! – Ordee

+0

実際、私が遭遇した小さな問題がありました。ユーザーが「2つのインデックスロケーションを入力...」の部分に無効なエントリを入力した場合、コードはエラーを適切に処理していないようです。プログラムは無限ループになるようで、手動で停止する必要があります。私は、Javaでtry/catchメソッドと例外処理プロシージャがあることを知っていますが、まだ読んでいます。 – Ordee

+0

私はそれを得たと思う。 while(!validoption)行を削除し、if/elseループを各インデックスの2つの別々のループに分けました。 index1とindex2にはそれぞれ独自のif/elseループがあります。プログラムをきれいに終了させるようです。プログラムを終了するのではなく、インデックスの場所を尋ねることに戻る方法はありますか? – Ordee

1

ファイルを間違って読み込んでいます。 Do

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 
関連する問題