這聽起來可能聽起來像一個愚蠢的問題,但我是一個新生,我正在努力學習。這是一個程序,它接受用戶輸入的羅馬數字並將其轉換爲十進制值。我試圖測試這個程序,但我不知道我的主要方法必須做什麼,才能做到這一點。我有其他方法來計算,但現在我該如何測試它?讓我告訴你我有什麼:如何在主要方法中測試我的程序?
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);
}
}
你可以看到,我試圖在以romanToDecimal
在getUserInput
封堵,但我知道,我沒有在main方法的參數,我甚至不認爲Java允許我這樣做。但是,我認爲這代表了我想要做的。真的我想要做的是:
System.out.println("The number you entered is " + userInput
System.out.println("The converted number is " + romanToDecimal
也許我應該把它放在一個單獨的方法?
謝謝喬恩讓我走過這個,並告訴我我做錯了什麼。 – NEPat10 2014-11-22 16:23:40