logonin
logonin

Reputation: 103

Printing 11 numbers per line?

I have to make a variation of the famous "FizzBuzz" code where it counts up to a number that the user inputs and only prints 11 numbers per line. My code seems to be somewhat working, but it repeats the same thing 11 times each line.

Can someone please help me fix this?

import javax.swing.JOptionPane;

public class CozaLoza2 {
    public static void main(String[] args) {

        String n = JOptionPane.showInputDialog("Please input an interger");
        int result = Integer.parseInt(n);
        for (int i = 0; i <= result; i++) {
            for (int j = 0; j <= 10; j++) {

                System.out.print(" ");

                if (i % 3 == 0 && i % 5 == 0 && i % 7 == 0) {
                    System.out.print("CozaLozaWoza");
                }

                if (i % 3 == 0 && i % 5 == 0) {
                    System.out.print("CozaLoza");

                } else if (i % 5 == 0) {
                    System.out.print("Loza");

                } else if (i % 3 == 0) {
                    System.out.print("Coza");

                } else if (i % 7 == 0) {
                    System.out.print("Woza");

                } else {
                    System.out.print(" " + i);
                }
            }
        }
        System.out.println();
    }
}

Here is the output I get if I enter the number "20" into that program

CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza 4 4 4 4 4 4 4 4 4 4 4 Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Woza Woza Woza Woza Woza Woza Woza Woza Woza Woza Woza 8 8 8 8 8 8 8 8 8 8 8 Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza 11 11 11 11 11 11 11 11 11 11 11 Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza 13 13 13 13 13 13 13 13 13 13 13 Woza Woza Woza Woza Woza Woza Woza Woza Woza Woza Woza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza 16 16 16 16 16 16 16 16 16 16 16 17 17 17 17 17 17 17 17 17 17 17 Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza 19 19 19 19 19 19 19 19 19 19 19 Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza

Upvotes: 0

Views: 589

Answers (3)

Mateusz Korwel
Mateusz Korwel

Reputation: 1148

Probably you have to move System.out.println(); one bracket above. This code should be works fine

import javax.swing.JOptionPane;

public class CozaLoza2 {
    public static void main(String[] args) {

        String n = JOptionPane.showInputDialog("Please input an interger");
        int result = Integer.parseInt(n);
        for (int i = 0; i <= result; i++) {
            for (int j = 0; j <= 10; j++) {

                System.out.print(" ");

                if (i % 3 == 0 && i % 5 == 0 && i % 7 == 0) {
                    System.out.print("CozaLozaWoza");
                }

                if (i % 3 == 0 && i % 5 == 0) {
                    System.out.print("CozaLoza");

                } else if (i % 5 == 0) {
                    System.out.print("Loza");

                } else if (i % 3 == 0) {
                    System.out.print("Coza");

                } else if (i % 7 == 0) {
                    System.out.print("Woza");

                } else {
                    System.out.print(" " + i);
                }
            }
            System.out.println();  // moved line
        }
    }
}

Upvotes: 0

Your code produce exactly :

input (20 in your test) : for (int i=0; i<=result; i++)

x 11   :  for (int j=0; j<=10; j++)

And only one CRLF (at the end !) => put it before

Upvotes: 0

user1803551
user1803551

Reputation: 13427

I'm happy to announce that your own lack of indentation was your downfall. This mess

  }
}
  }
     System.out.println();
}
}

should be this mess:

  }
}
     System.out.println();
  }
}
}

And now go learn how to indent and fix your indentation.

Also, in the outer loop for (int i = 0; i <= result; i++) you are running on result + 1 values. Probably you need i < result.

(Just in case the question gets edited, I am talking about the original.)

Upvotes: 1

Related Questions