在Java中将十进制数字转换为罗马数字?
我对计算机科学是个新手,我知道我现在很可能会犯一些非常简单的错误。我正在制作一个程序,将数字转换成罗马数字,而我的'Previous'变量就是不起作用 编辑以澄清: V(X,L,C,D,M,)之后的每个数字组合都超过标记。而且,我的输出将输入处理为相同的向前和向后。例:(IV=6) 这是我的密码:在Java中将十进制数字转换为罗马数字?,java,decimal,roman-numerals,Java,Decimal,Roman Numerals,我对计算机科学是个新手,我知道我现在很可能会犯一些非常简单的错误。我正在制作一个程序,将数字转换成罗马数字,而我的'Previous'变量就是不起作用 编辑以澄清: V(X,L,C,D,M,)之后的每个数字组合都超过标记。而且,我的输出将输入处理为相同的向前和向后。例:(IV=6) 这是我的密码: import java.util.*; public class roman { public static void main(String[] args) { S
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.*;
公营罗马人
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(系统输入);
System.out.print(“输入罗马数字:”);
字符串R=in.nextLine();
System.out.println(“转换为十进制:+RomanToDecimal(R));
}
静态int-RomanToDecimal(字符串R)
{
整数小数=0;
前半句;
对于(int x=0;x
这只是一个简单的逻辑错误
如果我们使用示例输入IX
,一步一步地运行您的代码,我们可以看到读入的第一个字符是I
,这会导致十进制值增加1。然后,当我们读入X
时,我们从十进制中减去1,使其为零
这意味着程序为输入IX
输出0
我们应该重新思考这个程序是如何运行的。这是正确的代码:
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;
}
}
import java.util.Scanner;
公共班机{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
System.out.print(“输入罗马数字:”);
字符串R=in.nextLine();
System.out.println(“转换为十进制:+RomanToDecimal(R));
}
静态int-RomanToDecimal(字符串R){
整数小数=0;
char-Previous=0;
对于(int x=0;x
当上一个字符出现时,你的表现并不好,你不仅想打折上一个添加的字符,而且想打折下一个字符的相同数字,因此需要打折两次。“然后添加10。”实际上,这不是-更仔细地阅读。啊,当然,对不起-教我略读!假设,正如海报上所说,结果总是结束的!编辑。我在我的
for
循环中写入了类似的内容,但程序仍然前后输出相同的数字。我知道R.charAt()
表示位置,但我不确定如何编写代码,使程序在一个数字之前注册另一个数字。Previous=x>0?R.charAt(x-1):“\0”
,或类似名称。您需要返回“一个字符”,但也要处理字符串中的第一个字符,因此无法返回的情况。谢谢,就我所知,这实际上工作得很好。不过,我确实删除了System.out.println(上一个);。哦,对不起,我在调试时忘了。