在Java中将十进制转换为罗马数字?(Converting Decimal to Roman Numeral in Java? [closed])

编程入门 行业动态 更新时间:2024-10-24 16:28:39
在Java中将十进制转换为罗马数字?(Converting Decimal to Roman Numeral in Java? [closed])

我是计算机科学的新手,我知道我现在最有可能犯很简单的错误。 我正在制作一个程序,将数字转换为罗马数字,而我的“上一个”变量不起作用。

编辑说明:V(X,L,C,D,M,)之后的每个数字组合都在标记之上。 此外,我的输出处理输入相同的向前和向后。 例如:(IV = 6)

这是我的代码:

import java.util.*; public class roman { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a Roman numeral: "); String R = in.nextLine(); System.out.println("Converted to Decimal: " + RomanToDecimal(R)); } static int RomanToDecimal(String R) { int Decimal = 0; char Previous; for (int x = 0; x < R.length(); x++) { Previous = R.charAt(x); if(R.charAt(x) == 'I') Decimal += 1; if(R.charAt(x) == 'V') { if (Previous == 'I') Decimal -= 1; else Decimal += 5; } if(R.charAt(x) == 'X') { if (Previous == 'I') Decimal-= 1; else Decimal+= 10; } if(R.charAt(x) == 'L') { if (Previous == 'X') Decimal -= 10; else Decimal+=50; } if(R.charAt(x) == 'C') { if (Previous == 'X') Decimal -= 10; else Decimal += 100; } if(R.charAt(x) == 'D') { if (Previous == 'C') Decimal -= 100; else Decimal += 500; } if(R.charAt(x) == 'M') { if (Previous == 'C') Decimal -= 100; else Decimal+= 1000; } Previous = R.charAt(x); } return Decimal; } }

I'm really new to computer science and I know that I'm most likely making very simple mistakes right now. I'm making a program to convert numbers to roman numerals and my 'Previous' variable just isn't working.

Edit for clarification: Every numeral combination after V (X,L,C,D,M,) are over the mark. Also, my output treats input the same forward and backward. Ex:(IV=6)

Here is my code:

import java.util.*; public class roman { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a Roman numeral: "); String R = in.nextLine(); System.out.println("Converted to Decimal: " + RomanToDecimal(R)); } static int RomanToDecimal(String R) { int Decimal = 0; char Previous; for (int x = 0; x < R.length(); x++) { Previous = R.charAt(x); if(R.charAt(x) == 'I') Decimal += 1; if(R.charAt(x) == 'V') { if (Previous == 'I') Decimal -= 1; else Decimal += 5; } if(R.charAt(x) == 'X') { if (Previous == 'I') Decimal-= 1; else Decimal+= 10; } if(R.charAt(x) == 'L') { if (Previous == 'X') Decimal -= 10; else Decimal+=50; } if(R.charAt(x) == 'C') { if (Previous == 'X') Decimal -= 10; else Decimal += 100; } if(R.charAt(x) == 'D') { if (Previous == 'C') Decimal -= 100; else Decimal += 500; } if(R.charAt(x) == 'M') { if (Previous == 'C') Decimal -= 100; else Decimal+= 1000; } Previous = R.charAt(x); } return Decimal; } }

最满意答案

这是更正的代码:

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a Roman numeral: "); String R = in.nextLine(); System.out.println("Converted to Decimal: " + RomanToDecimal(R)); } static int RomanToDecimal(String R) { int Decimal = 0; char Previous = 0; for (int x = 0; x < R.length(); x++) { if (R.charAt(x) == 'I') Decimal += 1; if (R.charAt(x) == 'V') { System.out.println(Previous); if (Previous == 'I') { Decimal -= 2; } Decimal += 5; } if (R.charAt(x) == 'X') { if (Previous == 'I') { Decimal -= 2; } Decimal += 10; } if (R.charAt(x) == 'L') { if (Previous == 'X') { Decimal -= 20; } Decimal += 50; } if (R.charAt(x) == 'C') { if (Previous == 'X') { Decimal -= 20; } Decimal += 100; } if (R.charAt(x) == 'D') { if (Previous == 'C') { Decimal -= 200; } Decimal += 500; } if (R.charAt(x) == 'M') { if (Previous == 'C') { Decimal -= 200; } Decimal += 1000; } Previous = R.charAt(x); } return Decimal; } }

当前一个字符出现时,您不能很好地计算行为,不仅您想要折扣之前添加的字符,而且您想要从下一个字符中折扣相同的数字,因此需要打折两次。

This is the code corrected:

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a Roman numeral: "); String R = in.nextLine(); System.out.println("Converted to Decimal: " + RomanToDecimal(R)); } static int RomanToDecimal(String R) { int Decimal = 0; char Previous = 0; for (int x = 0; x < R.length(); x++) { if (R.charAt(x) == 'I') Decimal += 1; if (R.charAt(x) == 'V') { System.out.println(Previous); if (Previous == 'I') { Decimal -= 2; } Decimal += 5; } if (R.charAt(x) == 'X') { if (Previous == 'I') { Decimal -= 2; } Decimal += 10; } if (R.charAt(x) == 'L') { if (Previous == 'X') { Decimal -= 20; } Decimal += 50; } if (R.charAt(x) == 'C') { if (Previous == 'X') { Decimal -= 20; } Decimal += 100; } if (R.charAt(x) == 'D') { if (Previous == 'C') { Decimal -= 200; } Decimal += 500; } if (R.charAt(x) == 'M') { if (Previous == 'C') { Decimal -= 200; } Decimal += 1000; } Previous = R.charAt(x); } return Decimal; } }

You weren't counting well on the behaviour when Previous character appears, not only you want to discount the previous added but you want to discount the same number from the next character so needs to be discounted twice.

更多推荐

本文发布于:2023-08-02 02:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1368427.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:罗马数字   转换为   中将   十进制   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!