4 Replies - 4744 Views - Last Post: 28 October 2009 - 07:43 PM Rate Topic: -----

#1 Laythe  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 76
  • Joined: 09-June 09

How to execute the if loop just once even if the condition gets true a

Posted 28 October 2009 - 06:57 PM

Hello EveryOne :)

How to execute the if loop just once even if the condition gets true again?
I need The random numbers to keep regenerating till 8 times but with executing the if loop just the first time the number 3 is generator and not if it get regenerated again.

This is my code:
import java.util.*;

public class ExecuteIfJustOnce
{
	public static void main(String Laythe[])
	{
		Random Generate = new Random();
		int RandomNumber;

		for(int i=0; i < 8; i++)
		{
			RandomNumber = Generate.nextInt(5);
			System.out.println(RandomNumber);

			if (RandomNumber == 3)
			{
				System.out.println("This will be printed once even if the " +
											  "number 3 is occured next time");
			}

			// I need The random numbers to keep regenerating till 8 times
			// but with executing the if loop just the first time the number 3
			// is generator and not if it get regenerated again;
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: How to execute the if loop just once even if the condition gets true a

#2 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12147
  • View blog
  • Posts: 45,157
  • Joined: 27-December 08

Re: How to execute the if loop just once even if the condition gets true a

Posted 28 October 2009 - 07:23 PM

Use a boolean variable. Set it to false initially, then change it inside the if statement for comparing randomNumber to 3. Also, make it a prerequisite in the if statement for your boolean to be false. Let me illustrate:

boolean occurred = false;
for(..statements..){
  ..statements..
  if(randomNum == 3 && occurred == false){
	   occurred = true;
	   ..your print statement..
	  }
}


This post has been edited by macosxnerd101: 28 October 2009 - 07:36 PM

Was This Post Helpful? 1
  • +
  • -

#3 Laythe  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 76
  • Joined: 09-June 09

Re: How to execute the if loop just once even if the condition gets true a

Posted 28 October 2009 - 07:34 PM

Good Idea macosxnerd101 but i get an error that say:
unexpected type
required: variable
found: value
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12147
  • View blog
  • Posts: 45,157
  • Joined: 27-December 08

Re: How to execute the if loop just once even if the condition gets true a

Posted 28 October 2009 - 07:38 PM

View PostLaythe, on 28 Oct, 2009 - 10:34 PM, said:

Good Idea macosxnerd101 but i get an error that say:
unexpected type
required: variable
found: value


It seems that I only hit the = sign once. It should say now ocurred == false instead of ocurred = false. Typo on my part. Sorry. :D
Was This Post Helpful? 0
  • +
  • -

#5 Laythe  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 76
  • Joined: 09-June 09

Re: How to execute the if loop just once even if the condition gets true a

Posted 28 October 2009 - 07:43 PM

Thank very You macosxnerd101 :D
I guessed that may be the problem and i thought i tried it but didnt worked, i dont know maybe cos i am sleepy so thought i tried == befor telling about the error :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1