我是计算机科学的新手,我知道我现在最有可能犯很简单的错误。 我正在制作一个程序,将数字转换为罗马数字,而我的“上一个”变量不起作用。
编辑说明: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.
更多推荐
发布评论