hello
i have problem creating a Sequence application that generates a series of random numbers from 0 to 9 until a 0 is generated and then displays the length of the sequence of numbers.
the output should look similar to this
3 6 7 8 9 0
Length of the sequence was 6

please help complete the program because i dont know what to do or where to begin.
this is what i have so far

import java.util.Scanner
import java.util.Random 
public class Sequence
{
  public static void main(String[] args)
  {
  Scanner input = new Scanner (System.in);
Random r = new Random();
int randomNum = r.nextInt(9 - 0 +1)+0; 


}
}

Recommended Answers

All 3 Replies

import java.util.Random;
public class Sequence{
	public static void main(String args[]){
                //loop counter
		int counter = 0;
		int randomNum;
		Random r = new Random();
		do{
			randomNum = r.nextInt(9);
			System.out.print(randomNum);
			counter++;
		}while(randomNum != 0);
		System.out.print("\nLength of the sequence was "+counter);
	}
}

Sample Output:

7176430
Length of the sequence was 7

@jazz_vill
Why do you do the OPs homework instead of trying to help him learn to do it himself?
You don't learn much copying and pasting.

If you are going to post code, you should add enough comments to the code to describe your logic and what the code does to get the answer.

Or are you just trying to show off that you know how to write a program?

My bad I though it was pretty clear while going through the code. Can't edit the first post I have to wait 30 min.

Edit: I can't edit the first post.

import java.util.Random;
public class Sequence{
	public static void main(String args[]){
        //loop counter; keeps track on the number of generated random numbers
		int counter = 0;
		int randomNum;
		final int N = 9;
		Random r = new Random();
		do{
			randomNum = r.nextInt(N) + 1;//generate a random number from 0-9
			System.out.print(randomNum);
			counter++;                   //increment the counter each time a random number is generated
		}while(randomNum != 0);    //while randomNum is not equal to 0 continue generating random numbers, If 0 then stop
		
		System.out.print("\nLength of the sequence was "+counter);
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.