gpt4 book ai didi

java - 为什么我的代码会打印两次玩家姓名?

转载 作者:行者123 更新时间:2023-12-02 01:43:21 25 4
gpt4 key购买 nike

主类

package main;

import static org.junit.Assert.*;
import java.util.ArrayList;
import org.junit.Test;

import lib.Die;
import lib.Name;
import lib.PairOfDice;
import lib.Player;

public class PlayerAppTest {

/* Please note - when we come to mark the solution to this unit test we will change the input
* data set for the players added to the list to ensure the solution works dynamically based
* upon any given data set and is not hardcoded in any way.
*/
@Test
public void testExecute() {
ArrayList<Player> players = new ArrayList<>();
players.add(new Player(new Name("Joe", "Bloggs"), new PairOfDice()));
players.add(new Player(new Name("Fred", "Jones"), new Die()));
players.add(new Player(new Name("Nila", "Singh"), new PairOfDice(new Die(5), new Die(5))));

String result = PlayerApp.execute(players, "Cassie Downturn");

String expectedResult = "cassie, DOWNTURN\nnila, SINGH\n";

assertEquals("The string returned should match the expected result (run 1)", expectedResult, result);


/* Test with a second set of input data */
ArrayList<Player> players2 = new ArrayList<>();
players2.add(new Player(new Name("David", "Blunt"), new PairOfDice()));
players2.add(new Player(new Name("Tim", "Jonas"), new Die(5)));
players2.add(new Player(new Name("Remi", "Patel"), new Die()));

String result2 = PlayerApp.execute(players2, "Cassie Downturn");

String expectedResult2 = "cassie, DOWNTURN\ntim, JONAS\nremi, PATEL\n";

assertEquals("The string returned should match the expected result (run 2)", expectedResult2, result2);
}
}

您好,这是我必须通过的 JUnit 测试,下面是我在主包中编写的代码;

JUnit 测试类

package main;

import java.util.ArrayList;
import lib.Player;
public class PlayerApp {

public static String execute(ArrayList<Player> players, String fullName) {
players.get(0).setFullPlayerName(fullName);

fullName = "";

for (int i = 0; i <players.size(); i ++) {
if (players.get(i).getName().getFirstName().toLowerCase().contains("a") || players.get(i).getName().getFamilyName().toUpperCase().contains("a")) {
fullName = players.get(i).getName().getFirstName().toLowerCase() + ", " + players.get(i).getName().getFamilyName().toUpperCase() + "\n";
}
System.out.println(fullName);
}
return fullName;
}
}

这是我的主包中的代码,我试图打印出名字中包含字符“a”的名字,名字应该小写,姓氏应该大写。它应该打印出来卡西,低迷
nila, SINGH
这是名称之间有一个新行,但是,当我打印它时,它会打印以下内容;

cassie, DOWNTURN
cassie, DOWNTURN
nila, SINGH

我很困惑为什么 cassie, DOWNTURN 被打印了两次,因为我在代码中找不到错误,任何帮助将不胜感激,谢谢。

最佳答案

即使 if 不匹配,您也会打印它,因为它位于 if 语句之外。将其移至内部。

for (int i = 0; i <players.size(); i ++) {
if (players.get(i).getName().getFirstName().toLowerCase().contains("a") || players.get(i).getName().getFamilyName().toUpperCase().contains("a")) {
fullName = players.get(i).getName().getFirstName().toLowerCase() + ", " + players.get(i).getName().getFamilyName().toUpperCase() + "\n";
System.out.println(fullName);
}
}

关于java - 为什么我的代码会打印两次玩家姓名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54101234/

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