Hi everyone, I am doing a program here to ask question about alphabet, and i am only in the middle of it and i face some problem that i really cant solve, need some help here...
ok the problem is, for(int j =0; j<20;j++),
in output, the 1st jlabel.setText(j) is 0, 2nd straight away became 19, means it skipped from 1 to 18, really want to know whats wrong, here is my code
btw if your are copying this codes into program and trying it, in the output, just ignore everything in the 1st page and press "Begin" button to begin, because things have not been set up properly yet, not until i fix this...
import java.util.Random;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
public class KindergartenTest extends JFrame implements ActionListener{
private JTextField answer;
private JLabel ans;
private JTextField result;
private JLabel Question;
private JButton next;
private int[] number = new int[20];
private char[] alphabet = new char[20];
public KindergartenTest()
{
setTitle("Alphabetical Order Test");
setSize(300,250);
Container KindergartenTest = getContentPane();
KindergartenTest.setLayout(new FlowLayout());
setAlphabet(0);
Question = new JLabel("Alphabet Test\n");
ans = new JLabel("Name");
answer = new JTextField(10);
next = new JButton("Begin");
restart = new JButton("Restart");
result = new JTextField(15);
next.addActionListener(this);
restart.addActionListener(this);
answer.addActionListener(this);
KindergartenTest.add(Question);
KindergartenTest.add(ans);
KindergartenTest.add(answer);
KindergartenTest.add(next);
KindergartenTest.add(result);
KindergartenTest.add(restart);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public char getAlphabet(int x){
return alphabet[x];
}
public void setAlphabet(int z){
for(int i=z; i<20; i++){
Random generator = new Random();
number[i] = 65 + generator.nextInt(25);
alphabet[i] = (char)number[i];
}
}
public void compare(int letter, int j){
if ( letter == getAlphabet(j)+1 || letter == getAlphabet(j)+33 )
{
result.setText("You are Correct");
}
else if ( letter != getAlphabet(j)+1 || letter != getAlphabet(j)+33)
{
result.setText("You are incorrect");
}
}
public void actionPerformed(ActionEvent event){
String input1;
char letter;
for (int j = 0; j < 20; j++)
{
Question.setText("Question " + j+1 + ": What comes after " + getAlphabet(j) + "?");
ans.setText("Answer");
next.setText("Next Question! (Question " + j+2 + ")" );
input1 = answer.getText();
letter = input1.charAt(0);
if(event.getSource() == next)
{
compare(letter, j);
}
}
}
public static void main(String[] args){
KindergartenTest showOutput = new KindergartenTest();
showOutput.setVisible(true);
}
}
by the way, if i change the code to
if(event.getSource() == next)
{
if ( letter == getAlphabet(0)+1 || letter == getAlphabet(0)+33 )
{
result.setText("You are Correct");
}
else if ( letter != getAlphabet(0)+1 || letter != getAlphabet(0)+33){
result.setText("You are incorrect");
}
}
i can succesfully compare my input to alphabet[0], but not when i make it into for loop, anyone know whats the problem?
and i guess there is also some problem with my way of putting event.getSource == next, because if i put it to check [0] follows by [1], the comparison for 1st click, [0], will sure be incorrect and it only works well in 2nd click,[1].
please help me, and thank you in advance for everyone that are trying to help me.