(Simple) How to use an if statement to respond to random integers
Hello,
I have a simple question. How do I use an if statement to check and respond to a randomly generated integer? For instance if the number is 4, it would print "Chicken"
but for 5 it would print "Steak" and so on. Is their a simple way to do this?
Thanks.
Re: (Simple) How to use an if statement to respond to random integers
Well first you have to generate a random number, which can be done in the Random class, then you simply compare the int it returns with either a switch statement or if statement.
The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
Re: (Simple) How to use an if statement to respond to random integers
Quote:
Originally Posted by
DusteroftheCentury
Hello,
I have a simple question. How do I use an if statement to check and respond to a randomly generated integer? For instance if the number is 4, it would print "Chicken"
but for 5 it would print "Steak" and so on. Is their a simple way to do this?
Thanks.
By randomly generated integer, you mean within some range, right? Also, is there any behavior of your program to print a certain statement in particular conditions or it's iterative? Means it will print different String at different Integers?
Well, in any case you must stick to switch and if.
Re: (Simple) How to use an if statement to respond to random integers
Alternately, you could use an array that has predefined values, and then just tell the program to print out the value of the array at a random index.
Re: (Simple) How to use an if statement to respond to random integers
Thanks guys.
Tjstretches link did it for me. Thanks for helping :)
Also for a related issue,(Part of same code) how do I change the text of a JLabel when I am in a different block of code? It seems to not recognize it unless it is in the same block. Is their a simple work around?
-Duster
Re: (Simple) How to use an if statement to respond to random integers
What do you mean by block of code? Are you talking about code with a pair of {} within a method
or do you mean a class?
You need a reference to the component whose methods you want to call.
Post a small piece of code to show what you are trying to do.
Re: (Simple) How to use an if statement to respond to random integers
Quote:
Originally Posted by
Norm
What do you mean by block of code? Are you talking about code with a pair of {} within a method
^this
Here is the peice of code Im talking about:
Code :
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == button1);
{
int low = 1;
int high = 5;
int rnd = (int)(Math.random() * (high - low + 1)) + low;
if (textbox1.getText().equals("Hi"))
//For all the printlns I would like something like label1.setText("Hello"), while keeping label1 created in the main code for the panel if possible
{
if (rnd == 1)
System.out.println("Hello.");
}
{
if (rnd == 2)
System.out.println("Greetings!");
}
{
if (rnd == 3)
System.out.println("Salutations.");
}
{
if (rnd == 4)
System.out.println("Hey there.");
}
{
if (rnd == 5)
System.out.println("Whats up?");
}
}
}
}
Thanks for helping,
-Duster
Re: (Simple) How to use an if statement to respond to random integers
That is called a method.
You need to move the definition of the variable out of a method to the class level so all methods can see it.
You can then give it values or call its methods in any method.
Re: (Simple) How to use an if statement to respond to random integers
Thanks Norm that worked like a charm! ;)
Also thanks for the definition, I hat not being able to use proper terms.
Thanks so much for the simple explanation!
-Duster
Re: (Simple) How to use an if statement to respond to random integers
Getting the right vocabulary can be important. There are many terms and techniques in IT with their own names.
You'll be able to ask better questions with the right word.