2

I am a high school student and a beginner in programming who is currently making a "Who wants to be a Millionaire? in java. I am using swing for my game. Right now I am only able to answer to my first question. The problem I am having is that I don't know how to move to the next Question. So if you guys could look at my program and tell me how do I move to the next question? I know that this question is too for advanced programmers but I really don't have that much experience in programming. Also one other question: How do I create the help in my program (50:50, telephone and the other one)?

This is my code -

import java.awt.Color;
import java.util.Scanner;
import javax.swing.ImageIcon;

import javax.swing.JFrame;

public class MillionareForm extends javax.swing.JFrame {
      //          0   1     2    3   4    5   6    7      8      9     10   11   12   13   14
String [] Ans1 = {"6","55","288","7","42","2","2","4025","3600","8760","12","89","79","16","40"};
String [] Ans2 = {"10","99","269","9","66","1","4","3025","4200","12674","15","90","10","15","39"};
String [] Ans3 = {"9","123","319","8","32","34","6","2025","2800","6782","9","45","1","14","42"};
String [] Ans4 = {"4","45","289","6","28","11","8","2455","2891","36500","18","87","2","13","43"};
String [] Ques = new String [15];
int turn=0, turn1=0;
boolean sw=false;
public MillionareForm() {
    initComponents();
}


@SuppressWarnings("unchecked")
int count =0;


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
   for(int c=0; c<15; c++){
        turn++;
        if(turn==1){

                if(choose.getText().equals("b")){
                    Answer2.setBackground(Color.green);
                    points.setLocation(650, 495);


                }
                else if(choose.getText().equals("a")){
                    Answer1.setBackground(Color.red);
                    points.setLocation(650, 520);
                    Answer2.setBackground(Color.green);
                }
                else if(choose.getText().equals("c")){
                    Answer3.setBackground(Color.red);
                    points.setLocation(650, 520);
                    Answer2.setBackground(Color.green);

                }
                else if(choose.getText().equals("d")){
                    Answer4.setBackground(Color.red);
                    points.setLocation(650, 520);
                    Answer2.setBackground(Color.green);
                }
        }
    } 
}                                        

private void formWindowActivated(java.awt.event.WindowEvent evt) {                                     



}                                    

private void formMouseMoved(java.awt.event.MouseEvent evt) {                                
    this.getContentPane().setBackground(Color.black);
    this.setBounds(0,0,900,700);
}                               

private void formComponentShown(java.awt.event.ComponentEvent evt) {                                    
    this.requestFocusInWindow();

    Ques[0]="What is 5 + 5 = ?";
    Ques[1]="What is 18 + 37 = ?";
    Ques[2]="What is 17 * 17 = ?";
    Ques[3]="What is 1024 / 128 = ?";
    Ques[4]="What is the square root of 1024?";
    Ques[5]="What is 1*2*3*4*5*6*7*8*9*0+1?";
    Ques[6]="2x-(3*2-2)=0. What is the value of x?";
    Ques[7]="What is 45 X 45?";
    Ques[8]="One hour equals how many seconds?";
    Ques[9]="How many hours are there in a year?";
    Ques[10]="How many zeros in a quadrillion?";
    Ques[11]="45/50. What is the percentage value?";
    Ques[12]="cos 90 = ?";
    Ques[13]="What is value of D in hexadecimal?";
    Ques[14]="If 2=6, 3=12, 4=20. What does 6 equal?";
    choose.setText("");

    System.out.println(points.getX());
    System.out.println(points.getY());
    for(int c=0; c<15; c++){
        turn1++;
        if(turn1==1){
            Question.setText(Ques[0]);
            Answer1.setText("A) "+Ans1[0]);
            Answer2.setText("B) "+Ans2[0]);
            Answer3.setText("C) "+Ans3[0]);
            Answer4.setText("D) "+Ans4[0]);

        }
        if(turn1==2){
            Question.setText(Ques[1]);
            Answer1.setText("A) "+Ans1[1]);
            Answer2.setText("B) "+Ans2[1]);
            Answer3.setText("C) "+Ans3[1]);
            Answer4.setText("D) "+Ans4[1]);
        }
        if(turn1==3){
            Question.setText(Ques[2]);
            Answer1.setText("A) "+Ans1[2]);
            Answer2.setText("B) "+Ans2[2]);
            Answer3.setText("C) "+Ans3[2]);
            Answer4.setText("D) "+Ans4[2]);
        }
        if(turn1==4){
            Question.setText(Ques[3]);
            Answer1.setText("A) "+Ans1[3]);
            Answer2.setText("B) "+Ans2[3]);
            Answer3.setText("C) "+Ans3[3]);
            Answer4.setText("D) "+Ans4[3]);
        }
        if(turn1==5){
            Question.setText(Ques[4]);
            Answer1.setText("A) "+Ans1[4]);
            Answer2.setText("B) "+Ans2[4]);
            Answer3.setText("C) "+Ans3[4]);
            Answer4.setText("D) "+Ans4[4]);
        }
        if(turn1==6){
            Question.setText(Ques[5]);
            Answer1.setText("A) "+Ans1[5]);
            Answer2.setText("B) "+Ans2[5]);
            Answer3.setText("C) "+Ans3[5]);
            Answer4.setText("D) "+Ans4[5]);
        }
        if(turn1==7){
            Question.setText(Ques[6]);
            Answer1.setText("A) "+Ans1[6]);
            Answer2.setText("B) "+Ans2[6]);
            Answer3.setText("C) "+Ans3[6]);
            Answer4.setText("D) "+Ans4[6]);
        }
        if(turn1==8){
            Question.setText(Ques[7]);
            Answer1.setText("A) "+Ans1[7]);
            Answer2.setText("B) "+Ans2[7]);
            Answer3.setText("C) "+Ans3[7]);
            Answer4.setText("D) "+Ans4[7]);
        }
        if(turn1==9){
            Question.setText(Ques[8]);
            Answer1.setText("A) "+Ans1[8]);
            Answer2.setText("B) "+Ans2[8]);
            Answer3.setText("C) "+Ans3[8]);
            Answer4.setText("D) "+Ans4[8]);
        }
        if(turn1==10){
            Question.setText(Ques[9]);
            Answer1.setText("A) "+Ans1[9]);
            Answer2.setText("B) "+Ans2[9]);
            Answer3.setText("C) "+Ans3[9]);
            Answer4.setText("D) "+Ans4[9]);
        }
        if(turn1==11){
            Question.setText(Ques[9]);
            Answer1.setText("A) "+Ans1[9]);
            Answer2.setText("B) "+Ans2[9]);
            Answer3.setText("C) "+Ans3[9]);
            Answer4.setText("D) "+Ans4[9]);
        }
        if(turn1==12){
            Question.setText(Ques[10]);
            Answer1.setText("A) "+Ans1[10]);
            Answer2.setText("B) "+Ans2[10]);
            Answer3.setText("C) "+Ans3[10]);
            Answer4.setText("D) "+Ans4[10]);
        }
        if(turn1==13){
            Question.setText(Ques[11]);
            Answer1.setText("A) "+Ans1[11]);
            Answer2.setText("B) "+Ans2[11]);
            Answer3.setText("C) "+Ans3[11]);
            Answer4.setText("D) "+Ans4[11]);
        }
        if(turn1==14){
            Question.setText(Ques[12]);
            Answer1.setText("A) "+Ans1[12]);
            Answer2.setText("B) "+Ans2[12]);
            Answer3.setText("C) "+Ans3[12]);
            Answer4.setText("D) "+Ans4[12]);
        }
        if(turn1==15){
            Question.setText(Ques[13]);
            Answer1.setText("A) "+Ans1[13]);
            Answer2.setText("B) "+Ans2[13]);
            Answer3.setText("C) "+Ans3[13]);
            Answer4.setText("D) "+Ans4[13]);
        }
        if(turn1==15){
            Question.setText(Ques[14]);
            Answer1.setText("A) "+Ans1[14]);
            Answer2.setText("B) "+Ans2[14]);
            Answer3.setText("C) "+Ans3[14]);
            Answer4.setText("D) "+Ans4[14]);
        }
    }

}                                   

private void chooseActionPerformed(java.awt.event.ActionEvent evt) {                                       

}                                      

/**
 * @param args the command line arguments
 */
 public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new MillionareForm().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JLabel Answer1;
private javax.swing.JLabel Answer2;
private javax.swing.JLabel Answer3;
private javax.swing.JLabel Answer4;
private javax.swing.JLabel Question;
private javax.swing.JTextField choose;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel points;
private javax.swing.JLabel word;
// End of variables declaration                   
}

Explanation of code - I made 4 arrays for my answers naming answer1, answer2, answer3, answer4. I made one array for my question--(same name for variable). jButton1 is the button that I push when answering a question and choose is my textField where put a, b, c, d and push jButton1. All the other labels are used in the game.

Qwerty6501
  • 65
  • 9
  • 2
    You mean something like [this](http://stackoverflow.com/questions/30925564/why-is-my-jlabel-not-showing-up/30926625#30926625)? Or, if you're really if for a design leap, maybe [this](http://stackoverflow.com/questions/31602113/listener-placement-adhering-to-the-traditional-non-mediator-mvc-pattern/31604919#31604919) – MadProgrammer Jan 19 '16 at 22:15
  • 1
    Use a [CardLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) – Frakcool Jan 19 '16 at 22:17
  • Just a sidenote, and definitely no answer to your question, but still important: You should refactor the code for simplicity. E.g. the index of the button- and question-texts is always equal to `turn1 - 1`. –  Jan 19 '16 at 22:17
  • 1
    One of the hardest conceptual hurdles we must get over is to change from creating a console program, one with linear logic flow to an event-driven program model, one where the coder doesn't have full control over program flow, and so instead he must manipulate program *state*, and as I see it, this is the main problem with your current program. That large for loop will work great for a console program but not here. So yeah, look closely at @MadProgrammer's example as it can help you get over this large hurdle. – Hovercraft Full Of Eels Jan 19 '16 at 22:20
  • 1
    The next main concept to focus on is to try to separate *data* from *code*. Here your GUI components and logic classes will make up the code, while the questions and answers themselves constitute data and shouldn't be hard-coded into your program. – Hovercraft Full Of Eels Jan 19 '16 at 22:21
  • 1
    0) 10 / 1) 55 / 2) 289 / 3) 8 / 4) 32 / 5) 1 / 6) 2 / 7) 2025 / 8) 3600 / 9) normal year: 8760, leap year: 8784 / 10) short scale countries: 15, long scale countries: 24 / 11) 90 / 12) 0 / 13) 13_dec, 15_oct, ... / 14) 42 ... 2(*3) = 6, 3(*4) = 12, 4(*5)= 20, 5(*6) = 30, 6(*7) = 42 .......... I prefer to be paid in cold hard cash! Until then - back to work... :/ – hamena314 Jan 20 '16 at 10:54

0 Answers0