Question: Suppose that you are trying to write a program that produces the following output using loops. The program below is an attempt at a solution,
Suppose that you are trying to write a program that produces the following output using loops. The program below is an attempt at a solution, but it contains errors. Identify and fix them all. You will submit answer as a corrected file. Your solution should contain and use writeOdds method, which uses a for loop.
Expected output:
1 3 5 7 9 11 13 15 17 19 21
public class BadNews { public static final int MAX_ODD = 21; public static void writeOdds() { // print each odd number for (int count = 1; count <= (MAX_ODD - 2); count++) { System.out.print(count + " "); count = count + 2; } // print the last odd number System.out.print(count + 2); } public static void main(String[] args) { // write all odds up to 21 writeOdds(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
