Skip to content
Advertisement

How do I fix it to output what I want to output?

I recently started coding in Java, I just started a week ago. The previous post was deleted because I did not specify it enough, I am sorry for that. I’ve been having troubles with for, do and while statements, and how do I output the same results in the expected outputs that I have typed in? With the use of while or do or for only.

Current code:

JavaScript

Current outputs:

JavaScript

Expected outputs:

Set 1

JavaScript

Set 2

JavaScript

I’m really having a hard time in solving this problem, any help will be very appreciated! Thank you 🙂

Advertisement

Answer

Here’s a solution to your problem. I started with a row number, 1..5. Then, I figured out from that value how many times I needed to perform the inner loop based on the row number. Once I’m iterating the right number of times in the inner loop, then it’s just a matter of making the math come out right to give the numbers you desire:

JavaScript

Result:

JavaScript

NOTE: I’m confident that the first line of the second set should end in 23, not 22 as you show as the desired output.

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