fork(1) download
  1. import java.util.Scanner;
  2.  
  3. class NumerosRomanos {
  4. public static void main (String args[]) {
  5. System.out.println ("Digite um Número em Romanos, para saber qual é esse Número em Decimais");
  6. Scanner sc = new Scanner (System.in);
  7. String numeroRomanoInicial = sc.nextLine();
  8. String numeroRomano = numeroRomanoInicial.toUpperCase();
  9. System.out.println(verificarNumerosRomanos(numeroRomano));
  10. }
  11.  
  12. public static int verificarNumerosRomanos(String numeroRomano) {
  13. int digitoRomano = 0;
  14. int digitoRomanoemconjunto = 0;
  15. for (int i = 0; i < numeroRomano.length(); i++) {
  16. digitoRomano = numeroRomano.charAt(i);
  17. switch (digitoRomano) {
  18. case 'I': digitoRomano += 1;
  19. break;
  20. case 'V': digitoRomano += 5;
  21. break;
  22. case 'X': digitoRomano += 10;
  23. break;
  24. case 'L': digitoRomano += 50;
  25. break;
  26. case 'C': digitoRomano += 100;
  27. break;
  28. case 'D': digitoRomano += 500;
  29. break;
  30. case 'M': digitoRomano += 1000;
  31. break;
  32. default: System.out.println("Dígito Romano Errado.");
  33. }
  34. }
  35. int soma = 0;
  36. for (int i = 0; i < numeroRomano.length(); i++) {
  37. char dr = numeroRomano.charAt(i);
  38. char drSeguinte = i < numeroRomano.length() - 1 ? numeroRomano.charAt(i + 1) : ' ';
  39. if (dr == 'I' && drSeguinte == 'V') {
  40. digitoRomanoemconjunto += 4;
  41. } else if (dr == 'I' && drSeguinte == 'X') {
  42. digitoRomanoemconjunto += 9;
  43. } else if (dr == 'X' && drSeguinte == 'L') {
  44. digitoRomanoemconjunto += 50;
  45. } else if (dr == 'X' && drSeguinte == 'C') {
  46. digitoRomanoemconjunto += 90;
  47. } else if (dr == 'C' && drSeguinte == 'D') {
  48. digitoRomanoemconjunto += 400;
  49. } else if (dr == 'C' && drSeguinte == 'M') {
  50. digitoRomanoemconjunto += 900;
  51. }
  52. }
  53. soma += digitoRomano + digitoRomanoemconjunto;
  54. return soma;
  55. }
  56. }
  57.  
  58. //https://pt.stackoverflow.com/q/160022/101
Success #stdin #stdout 0.1s 35416KB
stdin
MDCLXVI
stdout
Digite um Número em Romanos, para saber qual é esse Número em Decimais
74