hi lo calculator

Problem: When I click a number and then click enter (z button) it randomizes again every time a letter or number is clicked. So that way it is a different number everytime I click, so I can't guess the right number. For example if I click on 4 it will say higher, if I click on 5 it will say lower, so if I click 4 again it will say lower.I have tried moving my random statement and this does not help and when I use a while loop the button freezes on the number I click

//GUI Calculator Program

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

publicclass calcextends JFrameimplements ActionListener

{

private JTextField displayText =new JTextField(30);

private JButton[] button =new JButton[17];

private String[] keys ={"1","2","3","4",

"5","6","7","8",

"a","b","c","d",

"e","f","g","h","z"};

private String numStr1 ="";

private String numStr2 ="";

boolean done =false;

privatechar op;

privateboolean firstInput =true;

public calc()

{

setTitle("My Calculator");

setSize(230, 200);

Container pane = getContentPane();

pane.setLayout(null);

displayText.setSize(200, 30);

displayText.setLocation(10, 10);

pane.add(displayText);

int x, y;

x = 10;

y = 40;

for (int ind = 0; ind < 17; ind++)

{

button[ind] =new JButton(keys[ind]);

button[ind].addActionListener(this);

button[ind].setSize(50, 30);

button[ind].setLocation(x, y);

pane.add(button[ind]);

x = x + 50;

if((ind + 1) % 4 == 0)

{

x = 10;

y = y + 30;

}

}

this.addWindowListener(new WindowAdapter()

{

publicvoid windowClosing(WindowEvent e)

{

System.exit(0);

}

}

);

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

publicvoid actionPerformed(ActionEvent e)

{

String resultStr;//Step 1

String str = String.valueOf(e.getActionCommand());//Steps 1 and 2

char ch = str.charAt(0);//Steps 1 and 3

switch (ch)//Step 4

{

case'1':case'2':case'3'://Step 4a

case'4':case'5':case'6':

case'7':case'8':case'a':case'b':case'c'://Step 4b

case'd':case'e':case'f':case'g':

if (firstInput)

{

//numStr1 = "";

numStr1 = numStr1 + ch;

}

break;

case'z':

resultStr = evaluate();

//Step 4c

//displayText.setText(resultStr);

if (resultStr =="1"){

displayText.setText("Correct");

done =true;

}

elseif (resultStr =="2"){

displayText.setText("Higher");

}

else{//(resultStr == "3"){

displayText.setText("Lower");

}

numStr1 = resultStr;

// numStr2 = "";

firstInput =false;

break;

}

}

private String evaluate()

{

int num;

boolean done;

num = (int) (Math.random() * 16);

// try

// {

int num1 = Integer.parseInt(numStr1);

// int num2 = Integer.parseInt(numStr2);

String result;

//booleandone = false;

if (num1 == num)

{

result ="1";

return result;

// done = true;

}

else

if (num1 < num){

result="2";

return result;

}

else{

result="3";

return result;

}

}

publicstaticvoid main(String[] args)

{

calc c =new calc();

}

}

[8840 byte] By [thunderbbolta] at [2007-11-26 22:17:17]
# 1

If you want to select a random number one time when the program starts, then put the code to select the random number in the constructor. Store it in a member valiable, and access it during the comparison in the actionperformed method. Also, use .equals() to compare strings, not ==, it's more reliable.

hunter9000a at 2007-7-10 11:11:11 > top of Java-index,Java Essentials,New To Java...
# 2
When I move random to the constructor it will pick a number but it will only let me push two buttons to guess and then if I push any more it comes up with the same anwer the rest of the time.
thunderbbolta at 2007-7-10 11:11:11 > top of Java-index,Java Essentials,New To Java...
# 3

I don't see where you ever reset the firstInput boolean to true. You only update the user's guess if firstInput is true, and when they hit 'z', you set it to false. If you want them to be able to make another guess after hitting 'z', then you should reset the flag. Am I understanding what you're trying to do?

hunter9000a at 2007-7-10 11:11:11 > top of Java-index,Java Essentials,New To Java...