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!
if (current_number == previous_number) {?