【发布时间】:2016-01-22 07:43:22
【问题描述】:
我已经在 main 方法中解决了这一切,但是教授要求我们遵循非常具体的指导方针。以下是说明:
使用以下签名创建方法(不要忘记
static):
private static boolean isFizz (int val)检查是否是 3 的倍数。private static boolean isBuzz(int val)检查是否是 5 的倍数。创建一个类成员变量(这意味着它不在方法内,而是在类内):
private static int counter- 注意:您可以在声明它时初始化它,也可以在 main 方法中初始化它。
- 在主方法中:
- 使用计数器从 1 迭代到 100。
- 使用您定义的另外两种方法来确定要打印的内容。
- 请注意,这些方法不应打印任何内容,它们只返回一个布尔值。
- 您的程序应至少包括以下各项之一:
- 分支控制语句(如
if)。- 循环。
public class Fizzy {
//checking if a multiple of 3
private static boolean isFizz(int i){
if (i % 3 == 0){
return true;
}
return false;
}
//checking if a multiple of 5
private static boolean isFuzz(int i){
if (i % 5 == 0){
return true;
}
return false;
}
//professor wants a class here outside of main with a private static int.
//But I get an error and I'm not sure what I need to do to fix it.
//also, is this where my booleans need to be called?
public class Counter {
private static int counter(int x);
}
public static void main (String [] args){
//I think I'm supposed to call something here?
//I've tried Counter a = new Counter(); but it doesn't like it.
//I've tried new booleans but also doesn't like it.
/**
* for loop to iterate i to 100
*/
//counter is supposed to be iterated here. However I am not sure
//how to exactly access counter from a separate class.
for(counter; counter <= 100; ++counter){
//if Statement to check if a multiple of 3 and 5.
if (counter % 3 == 0 && counter % 5 == 0){
System.out.println("FizzBuzz");
}
// else if statement to check if multiple of 3
else if (isFizz == true){
System.out.println("Fizz");
}
//else if statement to check if multiple of 5
else if (isFuzz == true){
System.out.println("Buzz");
}
//else just run the loop
else {
System.out.println(counter);
}
}
}
}
}
应该是这样的:
1
2
Fizz
4
Buzz
Fizz
.
.
.
14
FizzBuzz
16
等等。
【问题讨论】:
-
也许第一个“非常具体的指导方针”可能是给出一些实际编译的示例代码?
-
public class Counter:这应该是另一个文件..或者只是删除“public”:见stackoverflow.com/questions/3578490/…
-
isFuzz 是一种方法,你却把它当成会员?将此更改为 isFuzz(counter),isFizz(counter) 也是如此。这个 fizzfuzz 只是两者都回归真实。所以应该是 isFizz(counter) && isFuzz(counter)
-
private static int counter(int x);不应该有(int x)。这可能是错误的来源。此外,根据程序的用途,您可能在哪里调用isFizz和isBuzz是正确的。它很可能在您的循环中。 -
“创建一个类成员变量”有点模棱两可:它是一个类的成员变量,还是你类中的一个成员变量?从以下几点我推断是后者。
标签: java class methods fizzbuzz