我想在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
}
不,太多的代码。不会阅读,P租约只提供相关的代码给你的具体问题。 – amit
你不需要if,那是什么变量 – maraca
initComponents()的实现在哪里? –