FizzBuzz 使用 Java 中的方法
FizzBuzz using Methods in Java
我已经在 main 方法中解决了所有问题,但是教授要求我们遵循非常具体的指导方针。以下是说明:
Create methods with the following signatures (don’t forget static
):
private static boolean isFizz (int val)
check if a multiple of 3.
private static boolean isBuzz(int val)
check if a multiple of 5.
Create a class member variable (this means it’s not inside a method, but is inside a class):
private static int counter
- Note: you can initialize this when you declare it, or inside the main method.
- In the main method:
- Use the counter to iterate from 1 to 100.
- Use the two other methods you define to determine what to print.
- Note that the methods should not print anything, they just return a boolean value.
- Your program should include at least one of each of the following:
- branch control statement (like
if
).
- loop.
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
以此类推
您已经编写了 isFuzz 和 isFizz 方法,为什么还要检查 %3 和 5?
相反,在 for 循环中,使用计数器作为参数调用 isFizz 和 isFuzz 方法。将 return 布尔值存储在变量中。
您的代码:
for(counter; counter <= 100; ++counter){
//if Statement to check if a multiple of 3 and 5.
if ( isFizz(counter) && isBuzz(counter) ){
System.out.println("FizzBuzz");
}
// else if statement to check if multiple of 3
else if ( isFuzz(counter) ) {
System.out.println("Fizz");
}
else if( isBuzz(counter) )
System.out.println("Buzz");
else {
System.out.println(counter);
}
}
已编辑:我让你去做了。
这是一个可能的解决方案:
public class Fizzy {
//a class *variable*, not a class
private static int counter;
//checking if a multiple of 3
private static boolean isFizz(int i) {
return i % 3 == 0; //no need for if
}
//checking if a multiple of 5
private static boolean isBuzz(int i) {
return i % 5 == 0; //no need for if
}
public static void main(String[] args) {
for (counter = 1; counter <= 100; ++counter) {
if (isFizz(counter) && isBuzz(counter)) { //reuse the methods
System.out.println("FizzBuzz");
} else if (isFizz(counter)) { //no need for "== true"
System.out.println("Fizz");
} else if (isBuzz(counter)) { //no need for "== true"
System.out.println("Buzz");
} else {
System.out.println(counter);
}
}
}
}
通常我只会给出指点,但在这里你非常接近一个解决方案,"giving pointers" 或多或少与 "show how it works" 相同。
大部分和其他人说的一样,但略有不同:fizzbuzz case 是 fizz 和 buzz 的组合,所以不需要做不同:你可以直接提取代码去一个新的线并将它们组合在一起。看看你是否能理解这种另一种方法:
for (int counter = 1; counter<=100; counter++) {
// if it's fizz or buzz, we need to print not the number but a word
if (isFizz(counter) || isBuzz(counter)) {
//if it's fizz, we write fizz (note that fizzbuzz are fizz!)
if (isFizz(counter)) {
System.out.print("Fizz");
}
//if it's buzz, we write buzz (note that fizzbuzz are buzz!)
if (isBuzz(counter)) {
System.out.print("Buzz");
}
}
//if it's neither fizz nor buzz, we need to print the number itself
else {
System.out.print(counter);
}
//once we wrote whatever we had to write, we go to a newline for the next one
System.out.println();
}
我已经在 main 方法中解决了所有问题,但是教授要求我们遵循非常具体的指导方针。以下是说明:
Create methods with the following signatures (don’t forget
static
):
private static boolean isFizz (int val)
check if a multiple of 3.private static boolean isBuzz(int val)
check if a multiple of 5.Create a class member variable (this means it’s not inside a method, but is inside a class):
private static int counter
- Note: you can initialize this when you declare it, or inside the main method.
- In the main method:
- Use the counter to iterate from 1 to 100.
- Use the two other methods you define to determine what to print.
- Note that the methods should not print anything, they just return a boolean value.
- Your program should include at least one of each of the following:
- branch control statement (like
if
).- loop.
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
以此类推
您已经编写了 isFuzz 和 isFizz 方法,为什么还要检查 %3 和 5?
相反,在 for 循环中,使用计数器作为参数调用 isFizz 和 isFuzz 方法。将 return 布尔值存储在变量中。
您的代码:
for(counter; counter <= 100; ++counter){
//if Statement to check if a multiple of 3 and 5.
if ( isFizz(counter) && isBuzz(counter) ){
System.out.println("FizzBuzz");
}
// else if statement to check if multiple of 3
else if ( isFuzz(counter) ) {
System.out.println("Fizz");
}
else if( isBuzz(counter) )
System.out.println("Buzz");
else {
System.out.println(counter);
}
}
已编辑:我让你去做了。
这是一个可能的解决方案:
public class Fizzy {
//a class *variable*, not a class
private static int counter;
//checking if a multiple of 3
private static boolean isFizz(int i) {
return i % 3 == 0; //no need for if
}
//checking if a multiple of 5
private static boolean isBuzz(int i) {
return i % 5 == 0; //no need for if
}
public static void main(String[] args) {
for (counter = 1; counter <= 100; ++counter) {
if (isFizz(counter) && isBuzz(counter)) { //reuse the methods
System.out.println("FizzBuzz");
} else if (isFizz(counter)) { //no need for "== true"
System.out.println("Fizz");
} else if (isBuzz(counter)) { //no need for "== true"
System.out.println("Buzz");
} else {
System.out.println(counter);
}
}
}
}
通常我只会给出指点,但在这里你非常接近一个解决方案,"giving pointers" 或多或少与 "show how it works" 相同。
大部分和其他人说的一样,但略有不同:fizzbuzz case 是 fizz 和 buzz 的组合,所以不需要做不同:你可以直接提取代码去一个新的线并将它们组合在一起。看看你是否能理解这种另一种方法:
for (int counter = 1; counter<=100; counter++) {
// if it's fizz or buzz, we need to print not the number but a word
if (isFizz(counter) || isBuzz(counter)) {
//if it's fizz, we write fizz (note that fizzbuzz are fizz!)
if (isFizz(counter)) {
System.out.print("Fizz");
}
//if it's buzz, we write buzz (note that fizzbuzz are buzz!)
if (isBuzz(counter)) {
System.out.print("Buzz");
}
}
//if it's neither fizz nor buzz, we need to print the number itself
else {
System.out.print(counter);
}
//once we wrote whatever we had to write, we go to a newline for the next one
System.out.println();
}