gpt4 book ai didi

java - FizzBu​​zz 在 Java 中使用方法

转载 作者:行者123 更新时间:2023-12-01 06:10:53 26 4
gpt4 key购买 nike

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

  1. 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.
  2. 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.
  3. In the main method:
    1. Use the counter to iterate from 1 to 100.
    2. 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.
    3. 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

等等。

最佳答案

大部分如其他人所说,但略有不同:fizzbuzz 情况是 fizz 和嗡嗡声的组合,因此无需使其有所不同:您只需提取代码即可转到新行,然后将它们组合在一起。看看您是否能理解另一种方法:

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

关于java - FizzBu​​zz 在 Java 中使用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34941467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com