2014-11-22 51 views
1

這聽起來可能聽起來像一個愚蠢的問題,但我是一個新生,我正在努力學習。這是一個程序,它接受用戶輸入的羅馬數字並將其轉換爲十進制值。我試圖測試這個程序,但我不知道我的主要方法必須做什麼,才能做到這一點。我有其他方法來計算,但現在我該如何測試它?讓我告訴你我有什麼:如何在主要方法中測試我的程序?

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); 

} 
} 

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

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

也許我應該把它放在一個單獨的方法?

回答

2

有你需要做一些修改:

  • 如果你打算從main打電話給你getUserInput方法,你要麼需要,使其static或創建類的實例。我建議使它成爲一種靜態方法。
  • 目前您romanToDecimal方法打印出來的結果 - 但它會整潔(在我看來),如果相反,它返回的結果,所以你可以在main
  • 打印在romanToDecimal(getUserInput)你想使用getUserInput就好像它是一個變量,但它是一種方法。

改變getUserInputstatic後,並改變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); 
} 

...還有很多更多的測試,你可以想到的一切。(根據不同的單元測試框架你選擇,你也許能夠非常緊湊寫單件試驗代碼的,並指定輸入和預期結果作爲參數。)

+0

謝謝喬恩讓我走過這個,並告訴我我做錯了什麼。 – NEPat10 2014-11-22 16:23:40

0

在main方法做到這一點:

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

這應該得到的代碼工作像它應該。

0

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

的測試將是這個樣子:

@Test 
public void testMyMethod() { 

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

PS:爲了成功做到這一點,你需要在你的romanToDecimal方法返回一個字符串!

0

試試這個:

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 
} 
0

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); 
} 

噢,正如有人指出的,你需要使這兩種方法你打電話靜態。

0

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

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);` 

    } 
} 
+0

小代碼格式化,也許? – 2014-11-22 16:39:11

相關問題