I am having trouble getting the 9 and 4 values( 900,400,90,40,etc)
I think I have the logic correct
Value = Value + 1000('M')
etc..
But I am having trouble getting my else if statements to print 'CM', 'DC' , basically any combination of more than one
Roman Numeral. I am wondering if I use a while loop within the for loop would this be correct?
Here's original homework instruction:
3. Create a program named roman.java. Ask the user to enter a Roman Numeral. Output the numeric value of the Roman Numeral.
Test your program with these numbers: MCMXLII, MCMLXIX, DCCCLXXXVIII
Hint: Create a for loop that steps through each character of roman numeral.
M = 1000, D = 500, C = 100, L = 50, X = 10, V = 5, I = 1.
MDCLXVI = 1000 + 500 + 100 + 50 + 10 + 5 + 1 = 1666
If a digit is out of order, then it is subtracted from the next digit. This is only used for these combinations:
CM = 900, CD = 400, XC = 90, XL = 40, IX = 9, IV = 4
Here's my code:
I have tried many different variations and the above code is only one that compliles?
I would like to know if I am in the right direction and if so, how should I go about
getting the special case values where the Roman Numeral is more than one letter?
I don't want answers~ I would like to better understand what needs to be done next and why- trying to show these guys in my programming class that I am just as capable.
/> Thank you.
I think I have the logic correct
Value = Value + 1000('M')
etc..
But I am having trouble getting my else if statements to print 'CM', 'DC' , basically any combination of more than one
Roman Numeral. I am wondering if I use a while loop within the for loop would this be correct?
Here's original homework instruction:
3. Create a program named roman.java. Ask the user to enter a Roman Numeral. Output the numeric value of the Roman Numeral.
Test your program with these numbers: MCMXLII, MCMLXIX, DCCCLXXXVIII
Hint: Create a for loop that steps through each character of roman numeral.
M = 1000, D = 500, C = 100, L = 50, X = 10, V = 5, I = 1.
MDCLXVI = 1000 + 500 + 100 + 50 + 10 + 5 + 1 = 1666
If a digit is out of order, then it is subtracted from the next digit. This is only used for these combinations:
CM = 900, CD = 400, XC = 90, XL = 40, IX = 9, IV = 4
Here's my code:
import javax.swing.*;
import java.util.*;
public class roman
{
public static void main (String[] args)
{
String S = JOptionPane.showInputDialog(null, "Enter a Roman Numeral:");
int Value =0;
int[] Letters = {'M','D','C','L','X','V','I'};
int[] Numebers = {1000, 500, 100, 50, 10, 5, 1};
String Numbers = String.valueOf(Letters);
for(int x=0; x<S.length(); x++)
{
if (S.charAt(x) == 'M')
Value = Value + 1000;
else if (S.charAt(x) == 'D')
Value = Value + 500;
else if (S.charAt(x) == 'C')
Value = Value + 100;
else if (S.charAt(x) == 'L')
Value = Value + 50;
else if (S.charAt(x) == 'X')
Value = Value + 10;
else if (S.charAt(x) == 'V')
Value = Value + 5;
else if (S.charAt(x) == 'I')
Value = Value + 1;
}
JOptionPane.showMessageDialog(null,"Value of Roman Numeral: " + Value);
}
}
I have tried many different variations and the above code is only one that compliles?
I would like to know if I am in the right direction and if so, how should I go about
getting the special case values where the Roman Numeral is more than one letter?
I don't want answers~ I would like to better understand what needs to be done next and why- trying to show these guys in my programming class that I am just as capable.