2017-05-21 33 views
0

基本上我必须添加一个文本文件到我的程序,并能够从文本文件中随机取字符串,以便执行其他功能。能够玩hang子手游戏。任何人的帮助将是非常受欢迎的。在下面找到如何增加它在我的代码..添加文本文件,并从中随机取

public class Main extends javax.swing.JFrame { 

    public ImageIcon imgs[]; 
    public JButton btns[]; 
    public String msgs[]; 
    public int ran; 
    public int err; 
    public String res[]; 

    public Main() { 
     initComponents(); 
     imgs = new ImageIcon[6]; 
     btns = new JButton[27]; 
     msgs = new String[20]; 

     //Images of the young man hanged 
     imgs[0] = new ImageIcon(getClass().getResource("/Hangnam/im1.jpg")); 
     imgs[1] = new ImageIcon(getClass().getResource("/Hangnam/im2.jpg")); 
     imgs[2] = new ImageIcon(getClass().getResource("/Hangnam/im3.jpg")); 
     imgs[3] = new ImageIcon(getClass().getResource("/Hangnam/im4.jpg")); 
     imgs[4] = new ImageIcon(getClass().getResource("/Hangnam/im5.jpg")); 
     imgs[5] = new ImageIcon(getClass().getResource("/Hangnam/im6.jpg")); 

     //Buttons for letters 
     btns[1] = jButton2; 
     btns[2] = jButton3; 
     btns[3] = jButton4; 
     btns[4] = jButton5; 
     btns[5] = jButton6; 
     btns[6] = jButton7; 
     btns[7] = jButton8; 
     btns[8] = jButton9; 
     btns[9] = jButton10; 
     btns[10] = jButton11; 
     btns[11] = jButton12; 
     btns[12] = jButton13; 
     btns[13] = jButton14; 
     btns[14] = jButton15; 
     btns[15] = jButton16; 
     btns[16] = jButton17; 
     btns[17] = jButton18; 
     btns[18] = jButton19; 
     btns[19] = jButton20; 
     btns[20] = jButton21; 
     btns[21] = jButton22; 
     btns[22] = jButton23; 
     btns[23] = jButton24; 
     btns[24] = jButton25; 
     btns[25] = jButton26; 
     btns[26] = jButton27; 

     //words to guess 
     msgs[0] = "understanding".toUpperCase(); 
     msgs[1] = "proliferation".toUpperCase(); 
     msgs[2] = "University".toUpperCase(); 
     msgs[3] = "Academic".toUpperCase(); 
     msgs[4] = "Atlas".toUpperCase(); 
     msgs[5] = "Tigres".toUpperCase(); 
     msgs[6] = "baby".toUpperCase(); 
     msgs[7] = "pumpy".toUpperCase(); 
     msgs[8] = "jump".toUpperCase(); 
     msgs[9] = "fun".toUpperCase(); 
     msgs[10] = "love".toUpperCase(); 
     msgs[11] = "something".toUpperCase(); 
     msgs[12] = "sometime".toUpperCase(); 
     msgs[13] = "mauritian".toUpperCase(); 
     msgs[14] = "lovers".toUpperCase(); 
     msgs[15] = "timetravel".toUpperCase(); 
     msgs[16] = "political".toUpperCase(); 
     msgs[17] = "psychologist".toUpperCase(); 
     msgs[18] = "rodrigues".toUpperCase(); 
     msgs[19] = "jackfruit".toUpperCase(); 


     //An event is assigned to each letter to check that it exists in the word to guess 
     for (int i = 1; i < 27; i++) { 
      btns[i].addActionListener(new ActionListener() { 
       public void actionPerformed(ActionEvent e) { 
        checkLetter(e); 
       } 
      }); 
     } 
     start(); 
    } 

    //Function to start the game parameters or start a new game 
    public void start() { 
     //ERRORES EN 0 
     err = 0; 
     jButton1.setIcon(imgs[0]); 
     jTextPane1.setText(""); 
     //To activate the letters on the board 
     for (int i = 1; i < 27; i++) { 
      btns[i].setEnabled(true); 
     } 
     //To generate a word randomly 
     ran = 0 + (int) (Math.random() * ((msgs.length - 1) + 1)); 
     //SEPARATE THE MESSAGE BY WORDS 
     String pal[] = msgs[ran].split(" "); 
     res = new String[msgs[ran].length() + 1]; 
     int j = 0; 
     // Will be the word that go under the letters as a separation_ 
     for (String pal1 : pal) { 
      for (int i = 0; i < pal1.length(); i++) { 
       jTextPane1.setText(jTextPane1.getText() + "_ "); 
       res[j++] = "_"; 
      } 
      jTextPane1.setText(jTextPane1.getText() + "\n"); 
      res[j++] = " "; 
     } 
    } 

    //When pressing a letter, this will be searched if it belongs to the word, otherwise mark it as error 
    public void checkLetter(ActionEvent e) { 
     JButton bt = (JButton) e.getSource(); 
     char c[]; 
     //Look for the letter in the word after it has been pressed 
     for (int i = 1; i < 27; i++) { 
      if (bt == btns[i]) { 
       //The key is initialized 
       c = Character.toChars(64 + i); 
       //Check if the letter is in the sentence 
       boolean esta = false; 
       for (int j = 0; j < msgs[ran].length(); j++) { 
        if (c[0] == msgs[ran].charAt(j)) { 
         res[j] = c[0] + ""; 
         esta = true; 
        } 
       } 
       //IF THE LETTER IS IN THE MESSAGE IS SHOWN ON THE TEXTPANEL 
       if (esta) { 
        jTextPane1.setText(""); 
        for (String re : res) { 
         if (" ".equals(re)) { 
          jTextPane1.setText(jTextPane1.getText() + "\n"); 
         } else { 
          jTextPane1.setText(jTextPane1.getText() + re + " "); 
         } 
        } 
        //Makes a check of the remaining letters and missing, in case there are no letters will be a winner 
        boolean gano = true; 
        for (String re : res) { 
         if (re.equals("_")) { 
          gano = false; 
          break; 
         } 
        } 
        //To be correct a message is displayed and the game is restarted 
        if (gano) { 
         JOptionPane.showMessageDialog(this, "Congratulation you won xD!!!"); 
         start(); 
         return; 
        } 
        //IF THE LETTER IS NOT IN THE MESSAGE, THE ERROR IS INCREASED AND THE IMAGE IS CHANGED 
       } else { 
        jButton1.setIcon(imgs[++err]); 
        //IF THE 5 try ARE REACHED THEN THE GAME IS MISSED AND THE MESSAGE IS MADE: 
        if (err == 5) { 
         JOptionPane.showMessageDialog(this, "Sorry you lose"+"\n"+"Please Try Again, the answer is: \n" + msgs[ran]); 
         start(); 
         return; 
        } 
       } 
       //This is the line that deactivates the letters after being used 
       bt.setEnabled(false); 
       break; 
      } 
     } 

    } 
} 
+2

什么问题?你不知道如何读取文件,将结果存储在数组中,生成一个随机索引,什么?你不指望我们做完整件事,是吗? –

+1

我同意。这个问题*读取*就像我们为您编写代码一样。这实际上不是家庭作业的重点。 –

+0

其实我的家庭作业在这里完成..这个程序运行完美..仍然我想学习如何在我的程序中添加一个文本文件,程序随机从文本文件而不是数组中获取单词。 –

回答

0

这里有一个简单的程序来从一个文本文件中的任意地方获得的话:

public static void main(String[] args) { 
    String fileName = "/Users/[YOUR_NAME]/Desktop/words.txt"; //change to location of file 
    File wordList = new File(fileName); 
    List<String> words = new ArrayList<>(); 
    Scanner reader = null; 

    try { 
     reader = new Scanner(wordList); 
    } catch (FileNotFoundException e) { 
     System.out.println("file \"" + fileName + "\" not found"); 
     System.exit(0); 
    } 

    while(reader.hasNextLine()) { 
     String word = reader.nextLine(); 
     words.add(word); 
    } 

    int place = rand.nextInt(words.size()); 
    String s = words.get(place); 
    System.out.println("Random word from file: " + s); 
} 

只是将其纳入你的代码。你可能需要做一些小的改动,但这会给你答案。