我正在尝试在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 :(得分:1)
这是您重构的代码。希望它可以帮到你。
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,实例化并不便宜。 更改了传递给nextInt的值。请参阅API。调用random.nextInt(10)给出0-9范围内的数字。这匹配数组的索引[10]。方便,对吧? ( - :
为了清晰起见,重命名了字段,方法和变量。
崩溃了大的多余条件。
仅存储当前挑战的索引,并在需要时使用它来查找挑战或答案。
为丢失或未调用的代码添加了注释。
祝你好运!