我正在從事類中的Java程序工作,並且我不確定爲什麼或者如何失敗,因爲我在程序運行時得到正確的打印輸出。Java的新功能 - 返回單元測試失敗
這裏是我的代碼:
import java.util.Scanner;
public class TextAnalyzer {
// Create scanner
private static Scanner scnr = new Scanner(System.in);
// Create text string
private static String text;
public static void main(String[] args) {
System.out.println("Enter a sentence or phrase: ");
// Scan for text input
text = scnr.nextLine();
System.out.println("You entered: " + text);
// Call getNumOfCharacters
int count = getNumOfCharacters();
System.out.println();
System.out.println("Number of characters: " + count);
// Call outputWithoutWhitespace
String modifiedstring = outputWithoutWhitespace();
System.out.println("String with no whitespace: " + modifiedstring);
}
// Method outputs string without spaces
private static String outputWithoutWhitespace() {
text = text.trim().replaceAll(" ", "");
return text;
}
// Method to return number of characters
private static int getNumOfCharacters() {
return text.length();
}
}
的輸出經過各級,它是在輸入字符失敗的數量進行單元測試,我真的只是需要一些指導新學生到Java編程。
下面是測試的打印:
我的假設是,getNumOfCharacters()
正在返回的字符數之後的空白字符已被刪除,但我可能是錯的。
任何幫助/指導將不勝感激。
提示:避免鏈接到外部網站獲取更多信息。並且:只發布相關代碼。含義:不是在這裏粘貼主要方法,而是更好地發佈失敗單元測試的源代碼。換句話說:請轉到幫助中心,閱讀「如何問」。 – GhostCat
您的方法名稱錯誤?測試說** GetNumOfCharacters **(大寫G) –
@GhostCat新用戶不能內嵌圖像,直到他們有10個代表。你可以也應該通過編輯來幫助他們。這裏有足夠的信息來看待這個問題,而且程序本身足夠小以便一目瞭然。這是一個非常明確的問題,我們經常問人們的所有信息。 –