This is a homework assignment. Convert an integer (1 - 2999) to roman numerals. I was wondering if I could make my code more efficient. The way I have it set up is that I take the digits of the input (the thousands, hundreds, tens, ones) and have cases for each of them. However, the approach I took to this problem is that there are a lot of if statements. My professor prefers that I utilize nothing that is not taught yet. I can only use for, if, while statements - the basics of Java. I was wondering if there are different approaches I could take to this problem so that the code is much more shorter as it is relatively long.
class romanNum {
public static void main (String [] args) {
//Initialize all variables including Scanner
Scanner input = new Scanner (System.in) ;
int userInput;
int onesDigit ;
int tensDigit ;
int hundredsDigit ;
int thousandsDigit ;
String one = "I";
String five = "V";
String ten = "X";
String fifty = "L";
String hundred = "C";
String fiveHundred = "D";
String thousand = "M";
boolean keepGoing = true ;
//User Input
userInput = input.nextInt();
//Seperate the digits
thousandsDigit = (userInput/1000) % 10;
hundredsDigit = (userInput / 100) % 10;
tensDigit = (userInput / 10) % 10;
onesDigit = (userInput / 1) % 10;
//Thousands (Because range is 1 - 2999, i didnt do more cases for thousands
while (keepGoing) {
for (int count = 0 ; count < thousandsDigit ; count++) {
System.out.print (thousand);
}
//Hundreds (Checks all cases 1-9)
//Checks 1 - 3
if ((hundredsDigit >= 1) && (hundredsDigit <= 3)) {
for (int count = 0 ; count < hundredsDigit ; count++) {
System.out.print (hundred);
}
}
//Checks 4
else if (hundredsDigit == 4) {
System.out.print (hundred + fiveHundred);
}
// Checks 9
else if (hundredsDigit == 9) {
System.out.print (hundred + thousand);
}
//Omit 0
else if (hundredsDigit == 0) {
}
// Checks 5 - 8
else {
System.out.print (fiveHundred);
for (int count = 0 ; count < hundredsDigit - 5 ; count++ ) {
System.out.print (hundred);
}
}
//Tens (Checks all cases 1-9)
//Checks 1 - 3
if ((tensDigit >= 1) && (tensDigit <= 3)) {
for (int count = 0 ; count < tensDigit ; count++) {
System.out.print (ten);
}
}
// Checks 4
else if (tensDigit == 4) {
System.out.print (ten + fifty);
}
// Checks 9
else if (tensDigit == 9) {
System.out.print (ten + hundred);
}
// Omit 0
else if (tensDigit == 0) {
}
// Checks 5 - 8
else {
System.out.print (fifty);
for (int count = 0 ; count < tensDigit - 5 ; count++ ) {
System.out.print (ten);
}
}
//Ones (Checks all cases 1-9)
//Checks 1 - 3
if ((onesDigit >= 1) && (onesDigit <= 3)) {
for (int count = 0 ; count < onesDigit ; count++) {
System.out.print (one);
}
}
// Checks 4
else if (onesDigit == 4) {
System.out.print (one + five);
}
//Checks 9
else if (onesDigit == 9) {
System.out.print (one + ten);
}
//Omit 0
else if (onesDigit == 0) {
}
// else (checks 5 - 9)
else {
System.out.print (five);
for (int count = 0 ; count < onesDigit - 5 ; count++ ) {
System.out.print (one);
}
}
//Print line outside of all conditions
System.out.println("");
keepGoing = false ;
}
}
}