• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do you print a character a random number of times?

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm trying to write a method that will print a string of a random number of X's (from 5 to 20) on a line, then another number of X's on the next line, etc. until the random number = 16.
I have to use while loops and probably fencepost algorithms. I'm confused with how to print the number of x's the random number of times over and over.
Right now, I'm pretty sure my for loop is wrong or unnecessary.
Any help is greatly appreciated, thanks!

So far, this is my code:

 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what happens when you run that code? I can only see one error.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make that two errors.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
three. I ought to learn to read.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Four. Most of them standard beginners’ errors. Look at the end of the line beginning for
 
Ashley Kin
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it's only printing one line of x's. I need it to continue to print lines of random numbers of x's until the random number is 16.
I'm thinking I went wrong with the for loop?
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Earlier, I wrote:. . . Look at the end of the line beginning for

If I drop any heavier hints, they will go through the floor.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ashley Kin wrote:Well, it's only printing one line of x's.
I'm thinking I went wrong with the for loop?


Yes, there's a small problem with big consequences at the end of line 7.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you really not read that line, AK? Look very carefully. You have even had hints which part of the line to look at first.

By the way: Insert this line before the line saying print("x")If you are lucky, that will bring the other three errors to your notice. If you are unlucky, only two of them. One of those three errors is related to an error I pointed out yesterday in a different thread of yours about a different program.
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, no, no, no, no. You, Harsha Smith, are going to deprive AK of any chance of learning if you simply post code like that
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello members of CodeRanch!

I have the same problem as Ashley. I believe if Ashley runs her code, there will be an infinite loop because of the while loop, it will never equal to 0.

Here is my version of my code so far.

It does print x randomly, however, not like how the exercise wants it.

Here is the expected output:
xxxxxxx
xxxxxxxxxxxxx
xxxxxx
xxxxxxxxxxx
xxxxxxxxxxxxxxxxx


Here is my output:
xxx
xxxxxxxxx
xx
xxxxxxx
xxxxxxxxxxxxx
x
x
x
xxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxxxx


Can someone please tell me where I've gone wrong? Thank you! I appreciate your help!

[ One-time courtesy moderator edit: added code tags ]
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kita Kata wrote:
It does print x randomly, however, not like how the exercise wants it.

Here is the expected output:
xxxxxxx
xxxxxxxxxxxxx
xxxxxx
xxxxxxxxxxx
xxxxxxxxxxxxxxxxx


Here is my output:
...

Can someone please tell me where I've gone wrong?


Welcome to the Ranch!

Where you have gone wrong is thinking that the "expected output" is literally what your program should output. Since you are using random numbers, then the actual output will randomly vary from the "expected" output. What that really is is an example of what you might reasonably expect from the program. The key thing to note is that all the lines in the output, except for the last one, has less than 16 "x"s.

For future reference, when you are posting code, please UseCodeTags (←click on that; it's a link to a page that tells you how to use code tags). As a first-time courtesy, I will edit your post to add them for you.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic