Я пытаюсь сделать угадывание игровой программы в 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()? –