Fizz Buzz黄瓜Java

问题描述

在这里遇到问题-我正在研究Fizz-Buzz-None。如果被3-Fizz,5-Buzz,3和5-FizzBu​​zz除以其他情况-无。我在IntliiJ终端中收到一条错误消息,提示“值应该不同。实际:无”。请告诉我我做错了什么:) 这是我的代码

public class FizzBuzzNoneChecker {

    public String checkIfFizz(int number) {
        return "Fizz";
    }

    public String checkBuzz(int number) {
        return "Buzz";
    }

    public String checkFizzBuzz(int number) {
        return "FizzBuzz";
    }

    public String checkNone (int number) {
        return "None";
    }
}




import io.cucumber.java8.En;
import org.junit.Assert;

public class IsItFizzBuzzNonesteps implements En{
        private String answer;
        private int number;

    public IsItFizzBuzzNonesteps () {
        Given("Given number is 3",() -> {

            // Write code here that turns the phrase above into concrete actions
            this.number = 3;

        });

        Given("Given number is 6",() -> {

            // Write code here that turns the phrase above into concrete actions

            this.number = 6;

        });

        Given("Given number is 5",() -> {

            // Write code here that turns the phrase above into concrete actions

            this.number = 5;

        });

        Given("Given number is 10",() -> {

            // Write code here that turns the phrase above into concrete actions

            this.number = 10;

        });

        Given("Given number is 15",() -> {

            // Write code here that turns the phrase above into concrete actions

            this.number = 15;

        });

        Given("Given number is 30",() -> {

            // Write code here that turns the phrase above into concrete actions

            this.number = 30;

        });

        Given("Given number is 1",() -> {

            // Write code here that turns the phrase above into concrete actions

            this.number = 1;

        });



        Given("Given number is 91",() -> {

            // Write code here that turns the phrase above into concrete actions

            this.number = 91;
        });



        When("I ask about the password for the given number",() -> {

            // Write code here that turns the phrase above into concrete actions
            FizzBuzzNoneChecker fizzBuzzNoneChecker = new FizzBuzzNoneChecker();
            this.answer = fizzBuzzNoneChecker.checkIfFizz(this.number);
            this.answer = fizzBuzzNoneChecker.checkBuzz(this.number);
            this.answer = fizzBuzzNoneChecker.checkFizzBuzz(this.number);
            this.answer = fizzBuzzNoneChecker.checkNone(this.number);

        });

        Then("I should be told {string}",(String string) -> {

           
            Assert.assertNotEquals(string,this.answer);
         


        });

    }
}



Feature: This is a game "Fizz-Buzz-None"
  if a number is divisible by 5 & 3 - say "FizzBuzz",if by 3 - say "Fizz",if by 5 - say "Buzz"
  in another case - say "None"

  Scenario Outline: Answer the right word as a reaction to a given number
    Given Given number is <number>
    When I ask about the password for the given number
    Then I should be told <answer>
    Examples:
      | number | answer     |
      | 3      | "Fizz"     |
      | 6      | "Fizz"     |
      | 5      | "Buzz"     |
      | 10     | "Buzz"     |
      | 15     | "FizzBuzz" |
      | 30     | "FizzBuzz" |
      | 1      | "None"     |
      | 91     | "None"     |

因此,当我放入Assert.assertEquals(string,this.answer);没有一个测试通过,AssertNotEquals-其中6个和2个失败。

解决方法

  1. 您的FizzBu​​zzNoneChecker方法实际上并未检查任何内容。每个方法始终返回其固定字符串,无论您提供哪个数字。您需要在此处添加一些逻辑,以检查数字是否可以被3或5整除,以确定要返回的字符串,例如
public String checkIfFizz(int number) {
  if (number % 3 == 0) {
    return "Fizz";
  } else {
    return "";
  }
}

我实际上会在三元操作中执行此操作,该操作要短得多,但是我不知道您是否遇到过这些操作。

  1. 就您的代码而言,this.answer始终最终是checkNone()的结果。您需要使用一系列if()语句找出要返回的字符串,从而将四个检查方法组合为一个。以我从第1点开始的代码为起点,并扩展逻辑,直到它以正确的顺序涵盖四种可能的情况,以给出正确的响应。。

  2. 要从“给定...”输入中提取数字,由于输入显然总是遵循该格式,因此请查看https://docs.oracle.com/javase/8/docs/api/index.html中的子字符串。

  3. 您的输入仅会是这些值之一,还是您需要考虑用户可能想知道42的可能性?

  4. 编写测试代码非常棒。向我们展示您的测试代码会更好,它为我们提供了更多信息,可以为您提供帮助:)

仔细研究以上几点,让我知道您的情况。如果需要,随时问更多问题!