2015-04-22 75 views
-3

我想在java中做一个猜测游戏程序,吐出存储在数组中的随机加扰词。然后它应该检查该值以查看它是否与用户文本相同,但是它始终检查错误的值。我认为这是一个简单的错误,但我无法弄清楚什么。任何帮助深表感谢。创建一个猜谜游戏程序

public class GameExample extends javax.swing.JFrame { 

    /** 
    * Creates new form GameExample 
    */ 
    public int lives = 3; 
    public String word2; 
    public String word1; 
    public String n[] = { "jaav", "ableriva", "todhem", "neergit", "rignst", "loobnea", "loaft", "henceiniret", "loovreading", "yolmropsirphm" }; 
    public String arr[] = { "java", "variable","method", "integer", "string", "boolean", "float", "inheritence", "overloading","polymorphism" }; 
    public GameExample() { 
     initComponents(); 


     //Random random = new Random(); 
     //String val = String.valueOf(n[random.nextInt(n.length)]; 
     Random r = new Random(); 
     int i1 = r.nextInt(10 - 1) + 1; 
     String val = String.valueOf(n[i1]); 
     word1 = val; 
     String val2 = String.valueOf(arr[i1]); 
     word2 = val2; 


     if(i1==1){ 
      val = String.valueOf(n[0]); 
      jButton2.setText(val); 
     } 

     else if(i1==2){ 
      val = String.valueOf(n[1]); 
      jButton2.setText(val); 
     } 

     else if(i1==3){ 
      val = String.valueOf(n[2]); 
      jButton2.setText(val); 
     } 

     else if(i1==4){ 
      val = String.valueOf(n[3]); 
      jButton2.setText(val); 
     } 

     else if(i1==5){ 
      val = String.valueOf(n[4]); 
      jButton2.setText(val); 
     } 

     else if(i1==6){ 
      val = String.valueOf(n[5]); 
      jButton2.setText(val); 
     } 

     else if(i1==7){ 
      val = String.valueOf(n[6]); 
      jButton2.setText(val); 
     } 

     else if(i1==8){ 
      val = String.valueOf(n[7]); 
      jButton2.setText(val); 
     } 

     else if(i1==9){ 
      val = String.valueOf(n[8]); 
      jButton2.setText(val); 
     } 

     else if(i1==10){ 
      val = String.valueOf(n[9]); 
      jButton2.setText(val); 
     } 

    } 



    private void jButton2ComponentShown(java.awt.event.ComponentEvent evt) {           

    }          
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {           
     if(lives > 0) 
     { 
      Random r = new Random(); 
      int i1 = r.nextInt(10 - 1) + 1; 
      if(i1==1){ 
      word1 = String.valueOf(n[0]); 
      jButton2.setText(word1); 
     } 

     else if(i1==2){ 
      word1 = String.valueOf(n[1]); 
      jButton2.setText(word1); 
     } 

     else if(i1==3){ 
      word1 = String.valueOf(n[2]); 
      jButton2.setText(word1); 
     } 

     else if(i1==4){ 
      word1 = String.valueOf(n[3]); 
      jButton2.setText(word1); 
     } 

     else if(i1==5){ 
      word1 = String.valueOf(n[4]); 
      jButton2.setText(word1); 
     } 

     else if(i1==6){ 
      word1 = String.valueOf(n[5]); 
      jButton2.setText(word1); 
     } 

     else if(i1==7){ 
      word1 = String.valueOf(n[6]); 
      jButton2.setText(word1); 
     } 

     else if(i1==8){ 
      word1 = String.valueOf(n[7]); 
      jButton2.setText(word1); 
     } 

     else if(i1==9){ 
      word1 = String.valueOf(n[8]); 
      jButton2.setText(word1); 
     } 

     else if(i1==10){ 
      word1 = String.valueOf(n[9]); 
      jButton2.setText(word1); 
     } 

     } 

    }           

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
     String oneText = jTextPane2.getText(); 
     String twoText = jTextPane2.getText(); 

      //jButton2.setText(val2); 
     if(oneText.equalsIgnoreCase(word2)){ 
      jButton3.setText("Correct"); 
    }else{ 
      jButton3.setText("Incorrect"); 
     } 
     System.out.println(word2); 
     System.out.println(word1); 
    }           
/* 
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {           


    }           
*/ 
    private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {          

    }          

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(GameExample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(GameExample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(GameExample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(GameExample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new GameExample().setVisible(true); 
      } 
     }); 


    } 

    // Variables declaration - do not modify      
    private javax.swing.JButton jButton1; 
    private javax.swing.JButton jButton2; 
    private javax.swing.JButton jButton3; 
    private javax.swing.JButton jButton4; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JScrollPane jScrollPane2; 
    private javax.swing.JScrollPane jScrollPane3; 
    private javax.swing.JTextArea jTextArea1; 
    private javax.swing.JTextPane jTextPane2; 
    // End of variables declaration     

}

+0

不,太多的代码。不会阅读,P租约只提供相关的代码给你的具体问题。 – amit

+0

你不需要if,那是什么变量 – maraca

+0

initComponents()的实现在哪里? –

回答

0

为了让你的代码工作(其他未决更改旁),你应该通过一个单一的线

jButton2.setText(n[i1]); 

全部更换

// if i1 is equal to one you don't want to set the label with the 
// string from i1-1 
if(i1==1){ 
    val = String.valueOf(n[0]); 
    jButton2.setText(val); 
} 

在你jButton4ActionPerformed你还需要设置变量值word2。如果你删除所有其他不必要的线,它可能看起来像。

Random r = new Random(); 
int i1 = r.nextInt(10 - 1) + 1; 
word1 = n[i1]; 
word2 = arr[i1]; 
jButton2.setText(word1); 
0

这是您重构的代码。希望它可以帮助你。

import javax.swing.*; 
import java.util.Random; 

public class GameExample extends javax.swing.JFrame { 

    // Variables declaration - do not modify 
    private javax.swing.JButton jButton1; 
    private javax.swing.JButton jButton2; 
    private javax.swing.JButton jButton3; 
    private javax.swing.JButton jButton4; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JScrollPane jScrollPane2; 
    private javax.swing.JScrollPane jScrollPane3; 
    private javax.swing.JTextArea jTextArea1; 
    private javax.swing.JTextPane jTextPane2; 
    // End of variables declaration 

    private static final String CHALLENGES[] = {"jaav", "ableriva", "todhem", "neergit", "rignst", "loobnea", "loaft", "henceiniret", "loovreading", "yolmropsirphm"}; 
    private static final String ANSWERS[] = {"java", "variable", "method", "integer", "string", "boolean", "float", "inheritence", "overloading", "polymorphism"}; 

    private final Random random = new Random(); 

    private int lives = 3; 
    private int currentChallenge; 

    public GameExample() { 
     initComponents(); 
     nextChallenge(); 
    } 

    private void nextChallenge() { 
     currentChallenge = random.nextInt(CHALLENGES.length); 
     jButton2.setText(CHALLENGES[currentChallenge]); 
    } 

    private void initComponents() { 
     // ? 
    } 

    // never called? 
    private void jButton2ComponentShown(java.awt.event.ComponentEvent evt) { 
     // ? 
    } 

    // never called? 
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { 
     if (lives > 0) { 
      nextChallenge(); 
     } 
    } 

    // never called? 
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
     String givenAnswer = jTextPane2.getText(); 
     if (givenAnswer.equalsIgnoreCase(ANSWERS[currentChallenge])) { 
      jButton3.setText("Correct"); 
     } else { 
      jButton3.setText("Incorrect"); 
     } 
    } 

    // never called? 
    private void jButton4MouseClicked(java.awt.event.MouseEvent evt) { 
     // ? 
    } 

    public static void main(String args[]) throws Exception { 
     validateChallenges(); 
     setNimbusLookAndFeel(); 
     startGame(); 
    } 

    private static void validateChallenges() { 
     if (CHALLENGES.length != ANSWERS.length) { 
      throw new RuntimeException("There must be the same number of CHALLENGES and ANSWERS"); 
     } 
    } 

    private static void setNimbusLookAndFeel() throws Exception { 
     final String NIMBUS_LOOK_AND_FEEL_NAME = "Nimbus"; 
     for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { 
      if (NIMBUS_LOOK_AND_FEEL_NAME.equals(info.getName())) { 
       UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } 

    private static void startGame() { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new GameExample().setVisible(true); 
      } 
     }); 
    } 
} 

一些评论:

感动了所有字段声明顶端,这是Java标准。

根据需要将字段设置为私有的,静态的和/或最终的。

删除了日志框架。有了这样一个简单的应用程序,您可能最好通过抛出异常退出。

尽可能提取的方法。

固定使用Random: 在班级中重复使用Random,实例化起来并不便宜。 更改传递给nextInt的值。请参阅API。调用random.nextInt(10)给出0-9范围内的数字。这与数组的索引整齐匹配[10]。方便,是吧? ( - :为清楚起见

更名字段,方法和变量

晕倒大冗余条件语句

只有存储当前挑战的指标,并用它来查找挑战或回答需要的时候。 。

丢失添加的注释或从来没有所谓的代码。

祝你好运!