-2

I am trying to create a piece of code for the game 'fizzbuzz', if n|3 => n=Fizz, if n|5 => n= Buzz and if n|3 and n|5 then n=Fizzbuzz.

For some reason my code only displays 46 lines of code, can someone help me out? Thanks.

Here is my code:

import static java.lang.Math.*;
import java.io.*;

public class P2InventedExercise
{
    static void FizzBuzz(int n)
    {

        /** Welcome Message **/
        System.out.println("+----------------------------+");
        System.out.println("|    WELCOME TO FIZZ BUZZ    |");
        System.out.println("+----------------------------+");


        /** Creating Strings to Print & Defines integer 'k'. **/
        String Fizz = "Fizz";
        String Buzz = "Buzz";
        String FizzBuzz = "FizzBuzz";
        int k = 0;

        /** Printing Strings **/   
        while (k <= n)
           {   
               /** Boolean Tests **/

                boolean FizzTest = (k%3 == 0);
                boolean BuzzTest = (k%5 == 0);
                boolean FizzBuzzTest = (k%3 == 0 && k%5 == 0);

               /** If Tests **/

                 if (FizzBuzzTest)
                {
                    System.out.println(k+"= " + FizzBuzz);
                    k=k+1;
                    continue;
                }
                if (FizzTest)
                {
                    System.out.println(k + "= " + Fizz);
                    k=k+1;
                    continue;
                }
                else if (BuzzTest)
                {
                    System.out.println(k + "= " + Buzz);
                    k=k+1;
                    continue;
                }
                else
                {
                    System.out.println(k + "= " + k);
                    k=k+1;
                    continue;
                }
            }  
    }
}
skypjack
  • 49,335
  • 19
  • 95
  • 187
GentleCynic
  • 73
  • 1
  • 9

3 Answers3

0

The code looks almost OK, check what is the n. Also, pay attention you're missing else in the second if statement. It should be:

else if (FizzTest)
Roman Pustylnikov
  • 1,937
  • 1
  • 10
  • 18
  • Thanks for responding, I change the second "if" to "else if" although it still only displays 46 lines of text. So if I put n=100, it will start from "54 = Fizz". Not from 0. :S – GentleCynic Oct 16 '15 at 17:21
  • Runs fine with me: https://ideone.com/UNvQMt Please make sure n is not changed on its way to FizzBuzz – Roman Pustylnikov Oct 16 '15 at 17:29
  • Or the results are not displayed due to the window height. Try to scroll up or run a smaller n to check. Use pause if you can't scroll or redirect to a text file. – Roman Pustylnikov Oct 17 '15 at 05:18
  • Really annoying if it works for you, Ive deleted imports and will redirect to a text file. – GentleCynic Oct 17 '15 at 09:38
0

GentleCynic your solution is very close, please checkout my video for a detailed explanation located on youtube This particular module will run 60 times

package java_basics;

public class FizzBuzz {

public static void main(String[] args) {
    printFizzBuzz(60); // shorthand for initializing the runtimes

}
public static void printFizzBuzz(int runtimes) {       //internal method using the "100 times to run" with declaration of n as integer
    for (int number = 0; number <= runtimes; number++) {      // create the number of times it should run
        if ((number % 3 == 0) && (number % 5 == 0)) {     //control flow using modulus operator "%" which means divisible by 3 and "&&" 5
            System.out.println("FizzBuzz because the number is "  + number + " which is divisible by both 3 and 5");  //prints out the actual number and fizzbuzz
        }
        else if (number % 3 == 0) {        //otherwise if number is only divisible by 3 print "fizz"
            System.out.println("Fizz because th number is "  + number + " which is only divisible by 3 which is");  //prints fizz
        }
        else if (number % 5 == 0) {
            System.out.println("Buzz because the number is " + number + " which is only divisbe by 5");  //prints buzz
        }
        else {
            System.out.println(number + " This number is not divisible by 3 or 5, therfore there is no fizz or buzz condition met");  // this number is printed without a fizzbuzz
        }
    }
    
}

}

0

function fizzBuzz(n) {

//'n' is integer to know the limit
    for(let i =1; i <= n;i++){
    
    //if i multiple of 3 and 5 print 'FizzBuzz'
    if(i % 3==0 && i%5==0){
        console.log('FizzBuzz')}
        
    //if i multiple of 3 but not 5 print 'Fizz'    
    else if(i % 3==0 && i%5!==0){
        console.log('Fizz')}
        
    //if i not multiple of 3 and multiple 5 print 'Buzz'   
    else if(i % 3!==0 && i%5==0){
        console.log('Buzz')}
        
    //if i not multiple of 3 and not multiple 5 print 'Buzz'      
    else if(i % 3!==0 && i%5!==0)
    {
    console.log(i);}

    }
}

/*
if n = 15

output
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
undefined
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 23 '22 at 18:10