Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

while loop

I need a while loop that generates random numbers between 1 and 100 and stops looping after it generates the same number twice.  This is what I have,  the only error I get when I try to run it is that " m might not have be initialized" but then again the whole program might not even work the way I want it too.  I am confused... I don't know what to do from here or if here is a start at all.

public class loop {

  public static void main(String args[]) {
 
boolean temp = true;
int m;
int t;

int n = (int) (Math.random() * 100 + 1);
t = n;

while (temp = true) {
//temp = true;
if (t == m) {
          temp = false;
     System.out.println("the number hit twice" + n + " " +  temp);
     }

     else {
          m = t;    
     int b = (int) (Math.random() * 100 + 1);
          t = b;    
     System.out.println("the number did not hit " + temp + " " + n);
 }

     
    }
  }

}
0
Dudah
Asked:
Dudah
  • 5
  • 2
  • 2
  • +5
1 Solution
 
antons061400Commented:
try this
int m = 0;
int t = 0;
0
 
allahabadCommented:
public class loop {

 public static void main(String args[]) {

boolean temp = true;
int m=0;
int t=0;

int n = (int) (Math.random() * 100 + 1);
t = n;

while (temp = true) {
//temp = true;
if (t == m) {
         temp = false;
    System.out.println("the number hit twice" + n + " " +  temp);
     return;
    }

    else {
         m = t;
    int b = (int) (Math.random() * 100 + 1);
         t = b;
    System.out.println("the number did not hit " + temp + " " + n);
}


   }
 }

}
0
 
DudahAuthor Commented:
well the
int m = 0;
int t = 0;
worked but no when I run the program it continues the loop for ever, well it just keeps printing out "the number hit twice (number that hit twice) FALSE"  So how to I get it to end the loop once it hits twice and just end the program.

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
DudahAuthor Commented:
allahabad that worked out the way I wanted it too. The only thing is I can't figure out what you did different for it to all of a sudden work.  thanks though I am still tring to figure out what I did wrong.
0
 
yongsingCommented:
Change:

while (temp = true) {

to:

while (temp == true) {
0
 
DudahAuthor Commented:
Oh I just saw the difference,
return;
thanks I knew nothing about that command.  If I use that will it take me out of any loop and end the program or move on.
0
 
DudahAuthor Commented:
Oh I just saw the difference,
return;
thanks I knew nothing about that command.  If I use that will it take me out of any loop and end the program or move on.
0
 
delphi3Commented:

Dudah,
Just another version and maybe fewer variables?

public class loop {

  public static void main(String args[]) {

    boolean temp = false;// it has not happened yet
    int m = 0;
    int t = 0;
    // gives m a value to compare with
    m = (int) (Math.random() * 100 + 1);
    System.out.println(" the value of m is " + m);
    while (temp == false) {
      t = (int) (Math.random() * 100 + 1);
      System.out.println(" the value of t is " + t);
      if (t == m) {
        temp = true;
        System.out.println("the number hit twice " + t + " " + temp);
      }
    }

  }
}

Delphi3
0
 
allahabadCommented:
public class loop {

 public static void main(String args[]) {

   boolean temp = false;// it has not happened yet
   int m = 0;
   int t = 0;

   while (!(temp)) {
     m=t;
     t = (int) (Math.random() * 100 + 1);
     System.out.println(" the value of t is " + t);
     if (t == m) {
       temp = true;
       System.out.println("the number hit twice " + t + " " + temp);
     }
   }

 }
}
0
 
Mayank SAssociate Director - Product EngineeringCommented:
If you want the program to stop anytime it generates a number twice, then you need to have an array which'll hold all the random numbers. Once you get a random number, check if its there in the array. If not so, then add it to the array, else terminate the program.


int array[] = new int[100] ; // it needs to hold only 100 random numbers (1 to 100 ) - we're stopping at repetition
int count = 0, n ;
boolean flag = true ;

while ( flag )
{
  n = Math.round ( Math.random () * 100 ) ) ;
 
  for ( int i = 0 ;  i < count ; i ++ )
    if ( n == array[i] )
    {
      flag = false ;
      break ;
   
    } // end if, for

  if ( flag )
  {
    System.out.println ( n ) ;
    array[count++] = n ;
 
  } // end if

} // end of while

 
Hope that helps!

Mayank.

0
 
DudahAuthor Commented:
mayankeagle >> I never thought about doing it that way, thanks but this command line comes up with a error when I try to run it at the first "(" I can't figure out what I have to add or delet for the program to run sucessfully.

n = Math.round ( Math.random () * 100 ) )
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Oh, man! Its damn easy! I made a silly mistake. I've put an extra closing-parenthesis in that statement!

It should be:

n = Math.round ( Math.random () * 100 ) ;

That's it!

Mayank.
0
 
delphi3Commented:
Dudah,

Here is another look

public class Looping {
  public static void main(String args[]) {
    int m = 0;
    boolean temp = false;
    int Nums[] = new int[100];
    while (temp == false) {
      Nums[0] = (int) (Math.random() * 100 + 1);
      m++;
      Nums[m] = (int) (Math.random() * 100 + 1);
      System.out.println(" the value of random is " + Nums[m]);
      for (int i = 0; i < m-1; i++) {
        if (Nums[i] == Nums[m]) {
          System.out.println("the number hit twice is " + Nums[m]);
          temp = true;
        }
      }
    }
  }
}
0
 
CleanupPingCommented:
Dudah:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
girionisCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points to yongsing

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 2
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now