简体   繁体   中英

Random number sequence that stops with 2 equal numbers

Create a sequence of numbers consisting of numbers from 0-9, and if two numbers appear back to back, end the sequence and display the length of the sequence.

I've been trying to find out a way to write a program with the above requirements. I just can't think of a way to do this. The most I've gotten is:

import java.util.Random;
public class RandomSequence{
  public static void main(String[]args){
    int num1, num2, num3, i=2;
    Random r=new Random();
    num1=r.nextInt(10);
    num2=r.nextInt(10);
    System.out.print(num1+", "+num2+", ");
    while (num1!=num2){
      num3=r.nextInt(10);
      i++;
      System.out.print(num3+", ");
      if (num3==num2){
        System.out.println("There are "+i+" numbers in the sequence");

..

and I just can't figure out how to end the sequence when two equal numbers appear back to back.

Here's an example given:

1,6,2,9,8,1,4,2,8,2,2

This sequence is 11 numbers long. "

Thank you, and help is much appreciated!

I don't see anywhere where num2 is changing. You want num2 to hold the number that you generated in the previous iteration of the loop. So, just before the end of your loop you need to set num2 = num3;

Really, there isn't a need for num3 at all. You get set up before the loop to use num1 and num2 as the previous and current number, respectively, and then add num3 to the mix. Renaming 'num1' and 'num2' (say to 'previous' and 'current')might help clarify what's going on .

Just noticed, one more little issue. If the first term and second term are equal, you won't go through the loop at all, and so miss the output. You should output after your loop exits instead (the while condition is already checking for equality, after all).

========

Something like this:

 get first number, store as 'previous'.
 get second number, store as 'current'.
 print first couple numbers
 while (previous!=current){
   count up.
   move 'current' to 'previous'
   get next number, store as 'current'
   print current value
}
print final count
    import java.util.Random;
public class RandomSequence{
  public static void main(String[]args){
    int num1, num2, num3, i=2;
    Random r=new Random();
    num1=r.nextInt(10);
    num2=r.nextInt(10);
    System.out.print(num1+", "+num2);
    while (num1!=num2){
      num1=num2;
      num2=r.nextInt(10);
      i++;
      System.out.print(", "+num2);
    }
    System.out.println("\n\nThis sequence consists of "+i+" numbers");
  }
}

Thank you so much femtoRgon. I took your tip and re-edited the program and it works perfectly. It was easier than I thought it would be (I over-thought it).

Thank you again!

This would be pretty easy with an ArrayList

import java.util.ArrayList;
import java.util.Random;
public class RandomSequenceB {
 public static void main(String[]args){
   int i=0;
   ArrayList sequence = new ArrayList();

   while (true) {
     sequence.add(new Random().nextInt(10));
     System.out.println(sequence);
     if (i > 1 && sequence.get(i) == sequence.get(i - 1)){
       System.out.println("There are "+i+" numbers in the sequence");
       break;
     }
     i++;
   } 
 }   

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM