【问题标题】:FizzBuzz using Methods in JavaFizzBu​​zz 使用 Java 中的方法
【发布时间】:2016-01-22 07:43:22
【问题描述】:

我已经在 main 方法中解决了这一切,但是教授要求我们遵循非常具体的指导方针。以下是说明:

  1. 使用以下签名创建方法(不要忘记static):

    • private static boolean isFizz (int val) 检查是否是 3 的倍数。
    • private static boolean isBuzz(int val) 检查是否是 5 的倍数。
  2. 创建一个类成员变量(这意味着它不在方法内,而是在类内):

    • private static int counter
    • 注意:您可以在声明它时初始化它,也可以在 main 方法中初始化它。
  3. 在主方法中:
    1. 使用计数器从 1 迭代到 100。
    2. 使用您定义的另外两种方法来确定要打印的内容。
      • 请注意,这些方法不应打印任何内容,它们只返回一个布尔值。
    3. 您的程序应至少包括以下各项之一:
      • 分支控制语句(如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)。这可能是错误的来源。此外,根据程序的用途,您可能在哪里调用isFizzisBuzz 是正确的。它很可能在您的循环中。
  • “创建一个类成员变量”有点模棱两可:它是一个类的成员变量,还是你类中的一个成员变量?从以下几点我推断是后者。

标签: java class methods fizzbuzz


【解决方案1】:

与其他人所说的大致相同,但风格略有不同:fizzbuzz 案例是 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();    
}

【讨论】:

  • @Sailanarmo 如果您需要解释,请在评论中提问 :)
【解决方案2】:

你已经写了 isFuzz 和 isFizz 方法,为什么还要检查 % 3 和 5 呢?

相反,在 for 循环中,使用 counter 作为参数调用您的 isFizz 和 isFuzz 方法。将返回的布尔值存储在变量中。

您的代码:

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

}

已编辑:我让你去做。

【讨论】:

  • else if ( ){ 不确定您尝试使用该代码显示什么,但您可能希望以至少可以编译的方式对其进行编辑。
  • 我对这段代码试图在这里显示的内容感到非常困惑。
  • @Sailanarmo:他说你不需要第一次检查是对的。你写的代码比你应该写的多。您的 isFizz 和 isBuzz 方法比您需要的实际代码长。
  • 你没有太多事情要做。这是练习中最难的部分,你已经为他做了。他明确表示他希望为他完成作业。
  • 哦,好吧,现在更有意义了,抱歉,我不知道你把它留给我填写了。
【解决方案3】:

这是一个可能的解决方案:

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

通常我只会给出指点,但在这里你是如此接近一个解决方案,以至于“给出指点”或多或少与“展示它是如何工作的”相同。

【讨论】:

  • 天啊,现在我重新阅读了这句话,一切都说得通了!我一直在努力想弄清楚为什么我首先需要这门课。现在下面的所有答案也都说得通了。
  • 注意:OP 的要求是 [...] 从 1 迭代到 100。因此,你在 for 循环中的 counter 实际上应该初始化为 1。
  • 请注意,它的范围是 0 到 100。此外,通过将方法调用的结果分配给main。然而,@Sailanarmo 应该问他的老师的真正问题是,为什么指南要求将 counter 传递给 ìsFizzìsBuzz。计数器已经是一个成员变量,不应作为方法参数传递。像这样:private static boolean isFizz() { return counter % 3 == 0; }
  • OP 特别要求指出正确的方向。他确实希望有人为他做作业。你没有帮助他,恰恰相反。
猜你喜欢
  • 2016-01-15
  • 1970-01-01
  • 1970-01-01
  • 2016-10-15
  • 1970-01-01
  • 1970-01-01
  • 2012-02-21
  • 2018-07-18
  • 2012-11-04
相关资源
最近更新 更多