我正在尝试为游戏'fizzbuzz'创建一段代码,如果n | 3 => n = Fizz,如果n | 5 => n = Buzz,如果n | 3和n | 5那么n = Fizzbuzz。
出于某种原因,我的代码只显示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;
}
}
}
}
答案 0 :(得分:0)
代码看起来几乎没问题,检查n
是什么。
另外,请注意第二个if语句中的其他内容。它应该是:
else if (FizzTest)