Re: Troubles with My code
public class Rand1 extends JFrame implements ActionListener
{
Random rand = new Random();
private int numberToGuess = rand.nextInt(100);
private int numTries = 0;
private int guess;
private boolean win = false;
private JLabel instructions;
private JLabel status;
private JTextField enternum;
private JButton button;
private String convert;
public Rand1()
{
numberToGuess = rand.nextInt(100);
setLayout(null);
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
instructions = new JLabel("Enter name:");
button = new JButton("Enter");
enternum = new JTextField("");
instructions.setBounds(60, 30, 120, 30);
enternum.setBounds(80, 160, 130, 60);
button.setBounds(200, 30, 120, 30);
status.setBounds(200, 200, 120, 30);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == button)
{
convert = enternum.getText();
guess = Integer.parseInt(convert);
numTries++;
if(guess == numberToGuess)
win = true;
}
if(win == true)
{
status = new JLabel("You Win");
status.setBounds(80, 160, 130, 60);
}
}
}
首先你少了一个"}"
其次private int numberToGuess = rand.nextInt(100);直接赋值
-------------------------------------------------------------------------
First you have one less "}"
Second, private int numberToGuess = rand.nextInt(100); the direct assignment
Re: Troubles with My code
What do you mean
HTML Code:
Second, private int numberToGuess = rand.nextInt(100); the direct assignment
?
Re: Troubles with My code
@jcloud, use the code tags and read the following: http://www.javaprogrammingforums.com...n-feeding.html
grimrader22, outside methods (with a few exceptions - this not being one of them) you cannot assign a variable unless you do so in combination with its declaration
Code :
private int first = 1;//Good
private int second;
second = 2;//compile time error
//or through a method
public void setSecond(int s){
second = s;
}
Re: Troubles with My code
So how would I use that to fix my code?
Re: Troubles with My code
Read the advice given above, and the code that you quoted in post #3: declare and assign in the same statement
Re: Troubles with My code
I'm really confused on what I am supposed to do.
Re: Troubles with My code
@copeg
maybe you are right .whatever I will abide by the provisions of this community