簡體   English   中英

如何用main方法測試程序?

[英]How do I test out my program in the main method?

對於你們中的許多人來說,這聽起來可能是一個愚蠢的問題,但是我是一名新學生,我正在嘗試學習。 這是一個程序,它接受用戶輸入的羅馬數字並將其轉換為十進制值。 我正在嘗試測試該程序,但我不完全知道要執行該操作的主要方法。 我還有其他計算方法,但是現在應該如何測試呢? 讓我告訴你我所擁有的:

public class RomanNumeralConverter {  

public String getUserInput() {
    Scanner numberInput = new Scanner (System.in);
    System.out.print("Enter a roman numeral in uppercase: ");
    String userInput = numberInput.next();
    numberInput.close();
    return userInput;
}   

public static void romanToDecimal(String userInput) {
int decimal = 0;
int lastNumber = 0;
userInput = userInput.toUpperCase();
for (int x = userInput.length() - 1; x >= 0 ; x--) {
    char convertToDecimal = userInput.charAt(x);

    switch (convertToDecimal) {
        case 'M':
            decimal = processDecimal(1000, lastNumber, decimal);
            lastNumber = 1000;
            break;

        case 'D':
            decimal = processDecimal(500, lastNumber, decimal);
            lastNumber = 500;
            break;

        case 'C':
            decimal = processDecimal(100, lastNumber, decimal);
            lastNumber = 100;
            break;

        case 'L':
            decimal = processDecimal(50, lastNumber, decimal);
            lastNumber = 50;
            break;

        case 'X':
            decimal = processDecimal(10, lastNumber, decimal);
            lastNumber = 10;
            break;

        case 'V':
            decimal = processDecimal(5, lastNumber, decimal);
            lastNumber = 5;
            break;

        case 'I':
            decimal = processDecimal(1, lastNumber, decimal);
            lastNumber = 1;
            break;
    }
}
System.out.println(decimal);
}

public static int processDecimal(int decimal, int lastNumber, int lastDecimal) {
if (lastNumber > decimal) {
    return lastDecimal - decimal;
} else {
    return lastDecimal + decimal;
}
}


public static void main(String[] args) {

romanToDecimal(getUserInput);

}
}

你可以看到,我試圖在封堵getUserInputromanToDecimal但我知道,我沒有在main方法的參數,我甚至不認為Java允許我這樣做。 但是,我認為這代表了我正在嘗試做的事情。 我真正想做的是:

System.out.println("The number you entered is " + userInput
System.out.println("The converted number is " + romanToDecimal

也許我應該將其放在單獨的方法中?

您需要進行一些更改:

  • 如果要從main調用getUserInput方法,則需要使其成為static或創建類的實例。 我建議使其成為靜態方法。
  • 目前,您的romanToDecimal方法會打印出結果-但如果返回結果,它會更整潔(在我看來),因此您可以在main中將其打印出來
  • romanToDecimal(getUserInput)您嘗試使用getUserInput ,就像它是一個變量一樣,但這是一個方法。

getUserInput更改為static ,並將romanToDecimal更改為返回String而不是打印它后,您的main方法可能如下所示:

public static void main(String[] args) {
    String input = getUserInput();
    String result = romanToDecimal(input);
    System.out.println("The number you entered is " + input);
    System.out.println("The converted number is " + result);
}

作為一個程序就可以了。 一旦將romanToDecimal作為返回結果的方法,您還可以輕松地為其編寫單元測試 ,其中將輸入硬編碼到測試中,然后檢查結果。 例如:

public void test5() {
    String result = RomanNumeralConverter.romanToDecimal("V");
    Assert.assertEquals(5, result);
}

...還有更多您可以想到的測試。 (根據您選擇的單元測試框架,您也許可以編寫單個測試代碼 ,並非常緊湊地將輸入和預期結果指定為參數。)

在主要方法中執行此操作:

String input = getUserInput();
romanToDecimal(input);

這樣可以使代碼正常工作。

只需編寫單元測試,即可使用各種輸入測試出romanToDecimal方法。 在Java中, JUnit是最流行的框架。

測試看起來像這樣:

@Test
public void testMyMethod() {

   assertEquals("expected result", romanToDecimal("input"));
}

PS:為了成功完成此操作,您將需要在romanToDecimal方法中返回一個String!

嘗試這個:

public static void main(String[] args) {
    RomanNumeralConverter rnc = new RomanNumeralConverter();
    String userInput = rnc.getUserInput(); // get user input
    System.out.println(userInput); // print user input
    Tests.romanToDecimal(userInput); // call the romanToDecimal static method to convert and print converted decimal
}

getUserInput是一個函數名稱。 getUserInput()是對函數的調用 ,稱為getUserInput ,不getUserInput傳遞任何參數(空括號)。 那就是您想要的:調用該函數,並使用它返回的字符串。 romanToDecimal(getUserInput()); 在您的主要職能應該工作。 您還可以先將其分配給變量,以便可以在調用romanToDecimal之前將其打印出來,並且還可以使romatToDecimal返回一個String而不是僅打印出來。 然后,您可以執行以下操作:

public static void main(String argv[]) {
    String input = getUserInput();
    String output = romanToDecimal(input);
    System.out.ptinln("You enetered: " + input + "\nThe converted number is: " + output);
}

哦,正如有人指出的那樣,您需要將兩個方法都設為靜態。

如果將userInput方法放入主方法,則將userInput傳遞給romanToDecimal(getUserInput); 在main方法中,它將在這里工作的是main方法的代碼

public static void main(String[] args) {
Scanner numberInput = new Scanner (System.in);

System.out.print("Enter a roman numeral in uppercase: ");
String userInput = numberInput.next();
numberInput.close();
romanToDecimal(userInput);`

  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM