Monday, July 27, 2009

I need help with a Java program -- converting Roman Numerals to Arabic and Arabic to Roman Numerals...?

I have to make two static methods converting Roman numeral to Arabic and opposite My teacher suggested loops and string character methods and I don't understand what they are How do I make those two methods?


so far:


import java.util.Scanner;


public class RomanNumeral{


private static String roman;


private static String choice;


private static int number;


public static void main (String[] args){


Scanner scan = new Scanner (System.in);


do{


System.out.print("Would you like to convert Roman or Arabic Numerals? (r or a): ");


choice = scan.nextLine();


System.out.println();


if (choice = "r"){


System.out.print("Enter the Roman Numeral in capital letters: ");


roman = scan.nextLine();


System.out.println();


}


else{


System.out.print("Enter the number: ");


number = scan.nextLine();


System.out.println();


}


}


while (line.length( ) != 0);


}


public static convertRoman(){


switch(roman){


case I:


I = 1;


case V:


V = 5;


case X:


X = 10;


case L:


L = 50;


case C:


C = 100;


case D:


D = 500;


case M:


M = 1000;


}

I need help with a Java program -- converting Roman Numerals to Arabic and Arabic to Roman Numerals...?
Well it's a little bit harder than your switch statements as they are set up (which I don't see how they will work) because of how roman numerals treat the value before a major revision.





i.e. 3 = III and 4 = IV


300 = CCC and 400 = CD





etc.





I would use something like





div = 1000;


index = 10;


roman_num[10];





while (number %26gt; 0)


y = number / div;


z = number mod div


if (z %26gt; 0)


roman_num[index] = 'M'


decrement index


number -= y * 1000


select case (div)


case: 1000


if y %26gt; 899


roman_num[index] = 'C'


index--


number -= 900


case: 500


if y %26gt; 499


....





case: 5


if y = 4


roman_num = I


end case


div = next div (1000 -%26gt; 500, 500 -%26gt; 100, etc)


loop





start at the highest numbers and subtract until you get down to 1 or 0


No comments:

Post a Comment