为什么用我的代码将玩家姓名打印两次?

时间:2019-01-08 23:32:54

标签: java junit

主班

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”的名称,名字应小写,姓氏应大写。它应该打印出来 cassie, DOWNTURN nila, SINGH是名称,它们之间有换行符,但是当我打印时,它会打印以下内容;

cassie, DOWNTURN
cassie, DOWNTURN
nila, SINGH

我对为什么cassie,DOWNTURN被打印两次感到困惑,因为我找不到我的代码中的错误,非常感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

即使if不匹配,您也将打印它,因为它在if语句之外。而是将其移入内部。

<li>

答案 1 :(得分:0)

您的打印语句不是条件语句,它将始终执行,将其移至if中。您要打印两次,因为如果if条件的结果不为true,则不会更改fullName中的值。此外,在比较大写和小写字母时,您的姓氏比较将始终为假。

您的代码也可以绑定在一起并使其易于阅读:

    for(Player player : players){
        if(player.getName().getFirstName()).toLowerCase().contains("a")||player.getName().getFamilyName().toUpperCase.contains("A"))){
            String  fullName = players.get(i).getName().getFirstName().toLowerCase() + ", " + players.get(i).getName().getFamilyName().toUpperCase() + "\n";
            System.out.println(fullname);
        }
    }