Skip to content
Advertisement

Separate “for” loop iterations

How would I go about making each iteration of my four loop be random. The outcome as of now makes all 20 sentences put the same output, although it is choosing random words each time from the four arrays created in static class OuterWord. I have omitted the parts of the code that I don’t think are necessary including the getMethods created for each array and sentence, hopefully that will not negatively effect my question.

`public class SentenceMaker {

JavaScript

}`

Advertisement

Answer

Just move the OuterSentence object to for loop.

JavaScript

The above one is much readable or the other way is to just create the new object in the System.out.println statement like as follows:

JavaScript

In both the ways we are creating a new object for every statement so that new random will get generated so that new sentence will be formed.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement