【问题标题】:FizzBuzz, java codeFizzBu​​zz,Java 代码
【发布时间】:2016-01-15 11:44:19
【问题描述】:

我正在尝试为游戏“fizzbuzz”创建一段代码,如果 n|3 => n=Fizz,如果 n|5 => n= Buzz,如果 n|3 和 n|5 则 n=嘶嘶声。

由于某种原因,我的代码只显示了 46 行代码,有人可以帮帮我吗?谢谢。

这是我的代码:

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;
                }
            }  
    }
}

【问题讨论】:

  • 请描述预期的行为。
  • n 的值是多少?
  • FizzBu​​zz 是一个游戏,如果 n|3,n 被分配单词 Fizz,如果 n|5,n 被分配单词 Buzz,如果 n 除 3 和 5,那么 n 被分配 FizzBu​​zz。程序对 0 到 n 执行此操作,其中 n 由用户选择。问题是当我选择 n=100 时,程序只显示 46 行文本,所以它从 n=54 开始显示,但它意味着从 n=0 开始。
  • n|3 表示 n 除以 3。您的意思是 3|n 代替吗?
  • 您可以安全地删除 所有 这些导入 --- 您没有使用它们。

标签: java loops conditional fizzbuzz


【解决方案1】:

代码看起来几乎没问题,检查n 是什么。 另外,请注意您在第二个 if 语句中缺少 else 。应该是:

else if (FizzTest)

【讨论】:

  • 感谢您的回复,我将第二个“if”更改为“else if”,尽管它仍然只显示 46 行文本。因此,如果我输入 n=100,它将从“54 = Fizz”开始。不是从 0 开始。:S
  • 对我来说运行良好:ideone.com/UNvQMt 请确保 n 在前往 FizzBu​​zz 的途中未更改
  • 或者由于窗口高度导致结果不显示。尝试向上滚动或运行较小的 n 进行检查。如果您无法滚动或重定向到文本文件,请使用暂停。
  • 如果它适合你真的很烦人,我已经删除了导入并将重定向到一个文本文件。
【解决方案2】:

GentleCynic 您的解决方案非常接近,请查看我的视频以获取位于youtube 的详细说明 这个特定的模块将运行 60 次

package java_basics;

公共类 FizzBu​​zz {

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 2018-07-18
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多