-2

Ok, so what I would like to do is make a picture using asterisks. The picture is supposed to be no more or less than 10 rows and in each row is supposed to be any random amount of asterisks in the range of 1-10. The only problem I'm having is that I am not getting 10 rows, instead, I am getting anywhere from 11 - 17 rows. I'm not sure what I'm missing and I appreciate any insight you can offer me. Thank you!

public class RandomPicture {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {     
        // Create a Random picture using asterisks
        Random rand = new Random();

        for (int count = 0; count <=2; count++)

        {
        if (rand.nextInt(2) == 0){    
        System.out.println("*");
        } if (rand.nextInt(2) == 0){
        System.out.println("**");
        } if (rand.nextInt(2) == 0){
        System.out.println("***");
        } if (rand.nextInt(2) == 0){
        System.out.println("****");        
        } if (rand.nextInt(2) == 0){
        System.out.println("*****");        
        } if (rand.nextInt(2) == 0){
        System.out.println("******");        
        } if (rand.nextInt(2) == 0){
        System.out.println("*******");        
        } if (rand.nextInt(2) == 0){
        System.out.println("********");        
        } if (rand.nextInt(2) == 0){
        System.out.println("*********");       
        } if (rand.nextInt(2) == 0){ 
        System.out.println("**********");
        }
     }
    }
}
Sandeep Kokate
  • 825
  • 4
  • 16
  • Why so you have 10 `if` statements all evaluating to `true`? You could get away with two compounding `for-loop`s – MadProgrammer Feb 21 '18 at 05:00
  • what logic are you trying to implement? why are you looping three times and getting 10 random numbers each time? – Scary Wombat Feb 21 '18 at 05:01
  • 1
    Can you provide us with an example output that you desire? I don't get what you are trying to do with the code – JYun Feb 21 '18 at 05:02
  • 1
    You have 10 `if` statements that each randomly evaluate to true 50% of the time, surrounded by a loop that iterates 3 times. 3 * 10 * 50% = 15, so program will print 15 rows +/- whatever random variation is introduced. --- If you want exactly 10 rows printed, and each row consisting of 1-10 stars, then loop 10 times, and generate one random number per iteration, and print the number of stars accordingly. – Andreas Feb 21 '18 at 05:06
  • Your for (int count = 0; count <=2; count++) runs from 0, to 1, to 2. 2<=2, but you meant count < 2 or from count = 1; count <= 2; @Andreas hint is good for constantly 10 rows. – user unknown Feb 21 '18 at 06:09

1 Answers1

1

I think you are looking for something like that:

public static void main(String[] args) 
{     
    // Create a Random picture using asterisks
    Random rand = new Random();

    for (int lineCnt = 0; lineCnt < 10; lineCnt++) {
        for (int i = 0; i < (rand.nextInt(9) + 1); i++) {
            System.out.print("*");
        }
        System.out.println();
    }
}

Please hold in mind, that there are some more sophisticated methods to create a string with n characters (see 2804827).

Please take attention to the formatting of your code. It's awful...

Manfred Steiner
  • 1,215
  • 2
  • 13
  • 27
  • I see where my thinking went wrong now. I was not thinking about the nested loop process where the first "for" statement establishes how many times the next statement will run and the 2nd "for" statement is generating a random integer which determines (in this case) the amount of asterisks to print in each line. The amount is determined by setting "rand.nextInt(9) + 1" to tell java to pick a random integer between 1 - 10. I really appreciate your help with this Manfred. – Sophia Green Feb 21 '18 at 06:10