-2

I am new to java coding and while loops are a little confusing to me.

For this program I am asked to generate a random number between 1 and 100, I think I've got that part down.

First the code would print out the random value, and then, if the number is even, it prints out that number of "*" characters, and if its odd, it prints out that number of "&" characters.

I know you could use like modulus to determine odd/even but I don't know how to print out a certain number of characters using the while loop, thanks!

Example output would be:

Random number generated: 8
The output pattern: ********

Random number generated:3
The output pattern: &&&

AndyG
  • 39,700
  • 8
  • 109
  • 143
Clarisa
  • 125
  • 2
  • 12
  • 1
    You need to write the code in the question if you want to people help to find the error. – Mahmut Ali ÖZKURAN Mar 03 '15 at 23:04
  • 1
    It might even be less confusing if you also add the Java tag. – Jongware Mar 03 '15 at 23:06
  • There are multiple ways ou can do that listed here: http://stackoverflow.com/questions/1235179/simple-way-to-repeat-a-string-in-java – Calum Mar 03 '15 at 23:08
  • Does this answer your question? [Simple way to repeat a string](https://stackoverflow.com/questions/1235179/simple-way-to-repeat-a-string) – pjs Dec 31 '21 at 20:03

2 Answers2

0

Generate the number first; test to see if it's even; then print the appropriate strings.

public static void main(String args[]) throws Exception {
    Random rand = new Random();

    int x = rand.nextInt(100) + 1; // nextInt excludes 100, so add 1

    // decide which char to print
    String character = ((x%2) == 0) ? "*" : "&";
    // print
    int i = x;
    while (i > 0) {
        System.out.print(character);
        i--;
    }
}
Aify
  • 3,543
  • 3
  • 24
  • 43
0

I have done it with Math.random() function. This code is working, I've tested it my self.

public class random100
{
    public static void main (String args[])
    {
        int rn=0,i=0;
        rn=(int)(Math.random()*100)+1;
        i=rn;
        System.out.println("Random Number="+rn);
        System.out.println("The Output Pattern=");
        if(rn%2==0)
        {
            while(i>0)
            {
                System.out.print("*");
                i--;
            }
        }
        else if(rn%2!=0)
        {
            while(i>0)
            {
                System.out.print("&");
                i--;
            }
        }
    }
}

Here is the screenshot of BlueJ Terminal Window :- Screenshot

cigien
  • 57,834
  • 11
  • 73
  • 112
  • `i` and `rn` don't need to be initialized to `0` as they immediately get a different value. You can also use a single variable instead of two unless you want to save the random number for later. Your `else if` can just be an `else` as you know `rn%2 != 0` if the first check fails. It might be simpler if you use `i-- > 0` instead of decrementing it in the while loop as it centralizes all the continue logic in one place. – user904963 Dec 31 '21 at 21:25
  • As the user is struggling with `while`, a description of how the program works would help him. – user904963 Dec 31 '21 at 21:28