?
Solved

while loop

Posted on 2003-03-11
16
Medium Priority
?
200 Views
Last Modified: 2010-03-31
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
Comment
Question by:Dudah
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +5
16 Comments
 
LVL 4

Expert Comment

by:antons061400
ID: 8117185
try this
int m = 0;
int t = 0;
0
 
LVL 3

Expert Comment

by:allahabad
ID: 8117250
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
 

Author Comment

by:Dudah
ID: 8117258
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
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.

 

Author Comment

by:Dudah
ID: 8117271
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
 
LVL 9

Accepted Solution

by:
yongsing earned 100 total points
ID: 8117279
Change:

while (temp = true) {

to:

while (temp == true) {
0
 

Author Comment

by:Dudah
ID: 8117293
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
 

Author Comment

by:Dudah
ID: 8117318
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
 
LVL 4

Expert Comment

by:delphi3
ID: 8117543

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
 
LVL 3

Expert Comment

by:allahabad
ID: 8117590
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8118239
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
 

Author Comment

by:Dudah
ID: 8121328
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8124981
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
 
LVL 4

Expert Comment

by:delphi3
ID: 8125344
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
 

Expert Comment

by:CleanupPing
ID: 9058755
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
 
LVL 35

Expert Comment

by:girionis
ID: 9714319
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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month13 days, 6 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question