Results 1 to 5 of 5
  1. #1
    logonin is offline Member
    Join Date
    Nov 2015
    Posts
    8
    Rep Power
    0

    Default Help with FizzBuzz variation

    Hi I need to program a variation of the famous "FizzBuzz" code where the program counts up to the number the user inputs and only shows 11 numbers per line, as it of course replaces certain numbers with words like the program is supposed to. My program can do almost everything but for some reason it repeats the same thing 11 times on each line. I think it has something to do with the inner loop with the int "j" in it but I am not sure. I am still new to programming so can someone help me please? Here is my code

    Java Code:
    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 when I input the number "20" into the code above

    " 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"

    Thanks in advanced :)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Help with FizzBuzz variation

    it repeats the same thing 11 times on each line.
    Is that from line 30?
    Can you explain what is wrong with the current output and show what the output should be?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    7

    Default Re: Help with FizzBuzz variation

    the nested loop loops 11 times for the same i, so it prints 11 times the same result.
    If I understand correctly, the idea is to insert a line break after 11 elements have been added to one line/string.

  4. #4
    logonin is offline Member
    Join Date
    Nov 2015
    Posts
    8
    Rep Power
    0

    Default Re: Help with FizzBuzz variation

    Quote Originally Posted by benji2505 View Post
    the nested loop loops 11 times for the same i, so it prints 11 times the same result.
    If I understand correctly, the idea is to insert a line break after 11 elements have been added to one line/string.
    So I tried adding a break command to my code and I got rid of the j thing. Here is my new code

    Java Code:
    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++){
        
       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 if (result==11){
            break;
            
          } else {
            System.out.print(" "+i);
      }
    }
           System.out.println();
        }
    }
    Here is my new output when I try inputting "50"
    "CozaLozaWozaCozaLoza 1 2 Coza 4 Loza Coza Woza 8 Coza Loza 11 Coza 13 Woza CozaLoza 16 17 Coza 19 Loza Coza 22 23 Coza Loza 26 Coza Woza 29 CozaLoza 31 32 Coza 34 Loza Coza 37 38 Coza Loza 41 Coza 43 44 CozaLoza 46 47 Coza Woza"

    So how can I fix this so it only displays 11 numbers per line?

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Help with FizzBuzz variation

    Put in a counter to count how many times you print. You should also add some spaces to your individual numbers.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. FizzBuzz? Is it really 'that' trivial?
    By BustTheCode in forum Forum Lobby
    Replies: 3
    Last Post: 07-29-2014, 09:52 PM
  2. Applet execution timing variation
    By minakshi in forum Java Applets
    Replies: 8
    Last Post: 09-18-2013, 04:50 PM
  3. Variation on the Swing invokelater() issue
    By Digital Larry in forum New To Java
    Replies: 3
    Last Post: 05-12-2013, 03:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •