Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: (Simple) How to use an if statement to respond to random integers

  1. #1
    Member DusteroftheCentury's Avatar
    Join Date
    Jan 2012
    Location
    Northern California
    Posts
    42
    My Mood
    Fine
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default (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.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default 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)

  3. The Following User Says Thank You to Tjstretch For This Useful Post:

    DusteroftheCentury (January 27th, 2012)

  4. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: (Simple) How to use an if statement to respond to random integers

    Quote Originally Posted by DusteroftheCentury View Post
    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.

  5. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default 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.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  6. #5
    Member DusteroftheCentury's Avatar
    Join Date
    Jan 2012
    Location
    Northern California
    Posts
    42
    My Mood
    Fine
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default 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
    Last edited by DusteroftheCentury; January 27th, 2012 at 07:22 PM.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

  8. #7
    Member DusteroftheCentury's Avatar
    Join Date
    Jan 2012
    Location
    Northern California
    Posts
    42
    My Mood
    Fine
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: (Simple) How to use an if statement to respond to random integers

    Quote Originally Posted by Norm View Post
    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:
    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

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    Last edited by Norm; January 27th, 2012 at 08:24 PM.

  10. The Following User Says Thank You to Norm For This Useful Post:

    DusteroftheCentury (January 27th, 2012)

  11. #9
    Member DusteroftheCentury's Avatar
    Join Date
    Jan 2012
    Location
    Northern California
    Posts
    42
    My Mood
    Fine
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default 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

  12. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

Similar Threads

  1. how to use nextInt() to generate random integers?
    By rph in forum Object Oriented Programming
    Replies: 6
    Last Post: January 3rd, 2013, 02:32 AM
  2. Servlet to respond many requests
    By CarlosM87 in forum Java Servlet
    Replies: 3
    Last Post: January 31st, 2012, 04:18 AM
  3. Get Box to Respond to JScrollPane
    By bgroenks96 in forum AWT / Java Swing
    Replies: 1
    Last Post: September 25th, 2011, 10:18 AM
  4. [SOLVED] Writing Integers to .txt File; Returning Random Characters Instead of Integers
    By verbicidalmaniac in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 8th, 2011, 09:42 PM
  5. Determine the two smallest integers from a set of user input integers
    By bpontin in forum Loops & Control Statements
    Replies: 4
    Last Post: October 17th, 2010, 06:38 PM