gpt4 book ai didi

java - 如何在 junit 测试中将值初始化为字符串数组?

转载 作者:行者123 更新时间:2023-12-01 17:22:55 26 4
gpt4 key购买 nike

如何将值初始化为名为 texti 的字符串数组。我需要它来编写 junit 测试(如下所示)


MorsenEnglisch s;

@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("In setUpBeforeClass()");
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("In tearDownAfterClass()");
}

@Before
public void setUp() throws Exception {
System.out.println("In setUp()");
s = new MorsenEnglisch();
}

@After
public void tearDown() throws Exception {
System.out.println("In tearDown()");
s = null;
}

@Test
public void FirstTest() {
System.out.println("In FirstTest.");
s.setTexti(); // I don't know how to give texti a value right here. Texti should have only one
// field which holds the value "A".

s.code(s);
assertTrue(s.getOutput().equals(".- ")); //Since my program is a morse code translator it should
//convert the letter A into .-
// fail("Not yet implemented");
}

}

这是我的类(class),其中包含所有方法。正如您在下面看到的,“setTexti”方法用于为 Texti 提供一个值。当我编写 junit 测试时,我不知道如何给 Texti 一个值。所有其他名称都非常不言自明。

    private String[] texti;         // the whole text split up into an array
private int amount; // the number of fields in the array texti
private String output = ""; // this is the translated text in a single string

MorsenEnglisch() {
}

MorsenEnglisch(String[] texti, int amount) {
this.texti = texti;
this.amount = amount;
}

public void setTexti(String[] texti) { //Here is the method that I need to use in my junit test
this.texti = texti;
}

public void setOutput(String output) {
this.output = output;
}

public void setAmount(int anzahl) {
this.amount = anzahl;
}

public String[] getTexti() {
return this.texti;
}

public String getOutput() {
return this.output;
}

public int getAmount() {
return this.amount;
}

public void code(MorsenEnglisch s) {
for (int i = 0; i <= amount - 1; i++) {
if (texti[i].equals("a") || texti[i].equals("A")) {
texti[i] = ".- ";
output = output + ".- ";
}

if (texti[i].equals("b") || texti[i].equals("B")) {
texti[i] = "-... ";
output = output + "-... ";
}

if (texti[i].equals("c") || texti[i].equals("C")) {
texti[i] = "-.-. ";
output = output + "-.-. ";
}

if (texti[i].equals("d") || texti[i].equals("D")) {
texti[i] = "-.. ";
output = output + "-.. ";
}

if (texti[i].equals("e") || texti[i].equals("E")) {
texti[i] = ". ";
output = output + ". ";
}

if (texti[i].equals("f") || texti[i].equals("F")) {
texti[i] = "..-. ";
output = output + "..-. ";
}

if (texti[i].equals("g") || texti[i].equals("G")) {
texti[i] = "--. ";
output = output + "--. ";
}

if (texti[i].equals("h") || texti[i].equals("H")) {
texti[i] = ".... ";
output = output + ".... ";
}

if (texti[i].equals("i") || texti[i].equals("I")) {
texti[i] = ".. ";
output = output + ".. ";
}

if (texti[i].equals("j") || texti[i].equals("J")) {
texti[i] = ".--- ";
output = output + ".--- ";
}

if (texti[i].equals("k") || texti[i].equals("K")) {
texti[i] = "-.- ";
output = output + "-.- ";
}

if (texti[i].equals("l") || texti[i].equals("L")) {
texti[i] = ".-.. ";
output = output + ".-.. ";
}

if (texti[i].equals("m") || texti[i].equals("M")) {
texti[i] = "-- ";
output = output + "-- ";
}

if (texti[i].equals("n") || texti[i].equals("N")) {
texti[i] = "-. ";
output = output + "-. ";
}

if (texti[i].equals("o") || texti[i].equals("O")) {
texti[i] = "--- ";
output = output + "--- ";
}

if (texti[i].equals("p") || texti[i].equals("P")) {
texti[i] = ".--. ";
output = output + ".--. ";
}

if (texti[i].equals("q") || texti[i].equals("Q")) {
texti[i] = "--.- ";
output = output + "--.- ";
}

if (texti[i].equals("r") || texti[i].equals("R")) {
texti[i] = ".-. ";
output = output + ".-. ";
}

if (texti[i].equals("s") || texti[i].equals("S")) {
texti[i] = "... ";
output = output + "... ";
}

if (texti[i].equals("t") || texti[i].equals("T")) {
texti[i] = "- ";
output = output + "- ";
}

if (texti[i].equals("u") || texti[i].equals("U")) {
texti[i] = "..- ";
output = output + "..- ";
}

if (texti[i].equals("v") || texti[i].equals("V")) {
texti[i] = "...- ";
output = output + "...- ";
}

if (texti[i].equals("w") || texti[i].equals("W")) {
texti[i] = ".-- ";
output = output + ".-- ";
}

if (texti[i].equals("x") || texti[i].equals("X")) {
texti[i] = "-..- ";
output = output + "-..- ";
}

if (texti[i].equals("y") || texti[i].equals("Y")) {
texti[i] = "-.-- ";
output = output + "-.-- ";
}

if (texti[i].equals("z") || texti[i].equals("Z")) {
texti[i] = "--.. ";
output = output + "--.. ";
}

if (texti[i].equals(" ")) {
texti[i] = " ";
output = output + " ";
}

if (texti[i].equals("1")) {
texti[i] = ".---- ";
output = output + ".---- ";
}

if (texti[i].equals("2")) {
texti[i] = "..--- ";
output = output + "..--- ";
}

if (texti[i].equals("3")) {
texti[i] = "...-- ";
output = output + "...-- ";
}

if (texti[i].equals("4")) {
texti[i] = "....- ";
output = output + "....- ";
}

if (texti[i].equals("5")) {
texti[i] = "..... ";
output = output + "..... ";
}

if (texti[i].equals("6")) {
texti[i] = "-.... ";
output = output + "-.... ";
}

if (texti[i].equals("7")) {
texti[i] = "--... ";
output = output + "--... ";
}

if (texti[i].equals("8")) {
texti[i] = "---.. ";
output = output + "---.. ";
}

if (texti[i].equals("9")) {
texti[i] = "----. ";
output = output + "----. ";
}

if (texti[i].equals("0")) {
texti[i] = "----- ";
output = output + "----- ";
}

if (texti[i].equals("ä") || texti[i].equals("Ä")) {
texti[i] = ".-.- ";
output = output + ".-.- ";
}

if (texti[i].equals("ö") || texti[i].equals("Ö")) {
texti[i] = "---. ";
output = output + "---. ";
}

if (texti[i].equals("ü") || texti[i].equals("Ü")) {
texti[i] = "..-- ";
output = output + "..-- ";
}

if (texti[i].equals("ß")) {
texti[i] = "...--.. ";
output = output + "...--.. ";
}

if (texti[i].equals(".")) {
texti[i] = ".-.-.- ";
output = output + ".-.-.- ";
}

if (texti[i].equals(",")) {
texti[i] = "--..-- ";
output = output + "--..-- ";
}

if (texti[i].equals(":")) {
texti[i] = "---... ";
output = output + "---... ";
}

if (texti[i].equals(";")) {
texti[i] = "-.-.-. ";
output = output + "-.-.-. ";
}

if (texti[i].equals("?")) {
texti[i] = "..--.. ";
output = output + "..--.. ";
}

if (texti[i].equals("-")) {
texti[i] = "-....- ";
output = output + "-....- ";
}

if (texti[i].equals("_")) {
texti[i] = "..--.- ";
output = output + "..--.- ";
}

if (texti[i].equals("(")) {
texti[i] = "-.--. ";
output = output + "-.--. ";
}

if (texti[i].equals(")")) {
texti[i] = "-.--.- ";
output = output + "-.--.- ";
}

if (texti[i].equals("'")) {
texti[i] = ".----. ";
output = output + ".----. ";
}

if (texti[i].equals("=")) {
texti[i] = "-...- ";
output = output + "-...- ";
}

if (texti[i].equals("+")) {
texti[i] = ".-.-. ";
output = output + ".-.-. ";
}

if (texti[i].equals("/")) {
texti[i] = "-..-. ";
output = output + "-..-. ";
}

if (texti[i].equals("@")) {
texti[i] = ".--.-. ";
output = output + ".--.-. ";
}

if (texti[i].equals("!")) {
texti[i] = "-.-.-- ";
output = output + "-.-.-- ";
}
}
}

public void decode(MorsenEnglisch s) {
for (int i = 0; i <= amount - 1; i++) {
if (texti[i].equals(".-")) {
texti[i] = "A";
output = output + "A";
}

if (texti[i].equals("-...")) {
texti[i] = "B";
output = output + "B";
}

if (texti[i].equals("-.-.")) {
texti[i] = "C";
output = output + "C";
}

if (texti[i].equals("-..")) {
texti[i] = "D";
output = output + "D";
}

if (texti[i].equals(".")) {
texti[i] = "E";
output = output + "E";
}

if (texti[i].equals("..-.")) {
texti[i] = "F";
output = output + "F";
}

if (texti[i].equals("--.")) {
texti[i] = "G";
output = output + "G";
}

if (texti[i].equals("....")) {
texti[i] = "H";
output = output + "H";
}

if (texti[i].equals("..")) {
texti[i] = "I";
output = output + "I";
}

if (texti[i].equals(".---")) {
texti[i] = "J";
output = output + "J";
}

if (texti[i].equals("-.-")) {
texti[i] = "K";
output = output + "K";
}

if (texti[i].equals(".-..")) {
texti[i] = "L";
output = output + "L";
}

if (texti[i].equals("--")) {
texti[i] = "M";
output = output + "M";
}

if (texti[i].equals("-.")) {
texti[i] = "N";
output = output + "N";
}

if (texti[i].equals("---")) {
texti[i] = "O";
output = output + "O";
}

if (texti[i].equals(".--.")) {
texti[i] = "P";
output = output + "P";
}

if (texti[i].equals("--.-")) {
texti[i] = "Q";
output = output + "Q";
}

if (texti[i].equals(".-.")) {
texti[i] = "R";
output = output + "R";
}

if (texti[i].equals("...")) {
texti[i] = "S";
output = output + "S";
}

if (texti[i].equals("-")) {
texti[i] = "T";
output = output + "T";
}

if (texti[i].equals("..-")) {
texti[i] = "U";
output = output + "U";
}

if (texti[i].equals("...-")) {
texti[i] = "V";
output = output + "V";
}

if (texti[i].equals(".--")) {
texti[i] = "W";
output = output + "W";
}

if (texti[i].equals("-..-")) {
texti[i] = "X";
output = output + "X";
}

if (texti[i].equals("-.--")) {
texti[i] = "Y";
output = output + "Y";
}

if (texti[i].equals("--..")) {
texti[i] = "Z";
output = output + "Z";
}

if (texti[i].equals("")) {
texti[i] = " ";
output = output + " ";
}

if (texti[i].equals(".----")) {
texti[i] = "1";
output = output + "1";
}

if (texti[i].equals("..---")) {
texti[i] = "2";
output = output + "2";
}

if (texti[i].equals("...--")) {
texti[i] = "3";
output = output + "3";
}

if (texti[i].equals("....-")) {
texti[i] = "4";
output = output + "4";
}

if (texti[i].equals(".....")) {
texti[i] = "5";
output = output + "5";
}

if (texti[i].equals("-....")) {
texti[i] = "6";
output = output + "6";
}

if (texti[i].equals("--...")) {
texti[i] = "7";
output = output + "7";
}

if (texti[i].equals("---..")) {
texti[i] = "8";
output = output + "8";
}

if (texti[i].equals("----.")) {
texti[i] = "9";
output = output + "9";
}

if (texti[i].equals("-----")) {
texti[i] = "0";
output = output + "0";
}

if (texti[i].equals(".-.-")) {
texti[i] = "Ä";
output = output + "Ä";
}

if (texti[i].equals("---.")) {
texti[i] = "Ö";
output = output + "Ö";
}

if (texti[i].equals("..--")) {
texti[i] = "Ü";
output = output + "Ü";
}

if (texti[i].equals("...--..")) {
texti[i] = "ß";
output = output + "ß";
}

if (texti[i].equals(".-.-.-")) {
texti[i] = ".";
output = output + ".";
}

if (texti[i].equals("--..--")) {
texti[i] = ",";
output = output + ",";
}

if (texti[i].equals("---...")) {
texti[i] = ":";
output = output + ":";
}

if (texti[i].equals("-.-.-.")) {
texti[i] = ";";
output = output + ";";
}

if (texti[i].equals("..--..")) {
texti[i] = "?";
output = output + "?";
}

if (texti[i].equals("-....-")) {
texti[i] = "-";
output = output + "-";
}

if (texti[i].equals("..--.-")) {
texti[i] = "_";
output = output + "_";
}

if (texti[i].equals("-.--.")) {
texti[i] = "(";
output = output + "(";
}

if (texti[i].equals("-.--.-")) {
texti[i] = ")";
output = output + ")";
}

if (texti[i].equals(".----.")) {
texti[i] = "'";
output = output + "'";
}

if (texti[i].equals("-...-")) {
texti[i] = "=";
output = output + "=";
}

if (texti[i].equals(".-.-.")) {
texti[i] = "+";
output = output + "+";
}

if (texti[i].equals("-..-.")) {
texti[i] = "/";
output = output + "/";
}

if (texti[i].equals(".--.-.")) {
texti[i] = "@";
output = output + "@";
}
if (texti[i].equals("-.-.--")) {
texti[i] = "!";
output = output + "!";
}
}
}

public String toString() {
return this.output;
}
}

最佳答案

如果我很理解你的话,你想用一个元素=“A”来设置数组。所以,它是s.setTexti(new String[]{"A"}); .

关于java - 如何在 junit 测试中将值初始化为字符串数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61259501/

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