【问题标题】:Convert Numerical Value to Roman Numeral将数值转换为罗马数字
【发布时间】:2014-10-25 18:50:24
【问题描述】:

我需要使用不同的方法将数值转换为罗马数值。下面是我已经创建的代码。

public static void main (String[]args){
    Scanner in = new Scanner(System.in);
    int input = in.nextInt();
    String s = "";
}
private static int promptUserForNumber(Scanner inScanner) {
    System.out.print ("Enter a number between 1 and 3999 (0 to quit): ");
    int input = inScanner.nextInt();
    if((input < 1 && input > 3999) && input != 0){
        System.out.println("Error, number must be between 1 and 3999");
         if (input == 0){
            System.out.println("Goodbye");
         }  
        }
    return input;
}
public static String convertNumberToNumeral(int input) {    
    String s = "";
    Scanner in = new Scanner(System.in);
    while (input >= 1000){
        s += "M";
        input -= 1000;
    }
    while (input >= 900){
        s += "CM";
        input -= 900;
    }
    while (input >= 500){
        s += "D";
        input -= 500;
    }
    while (input >= 400){
        s += "CD";
        input -= 400;
    while (input >= 100){
        s += "C";
        input -= 100;
    }
    while (input >= 90){
        s += "XC";
        input -= 90;
    }
    while (input >= 50){
        s += "L";
        input -= 50;
    }
    while (input >= 40){
        s += "XL";
        input -= 40;
    }
    while (input >= 10){
        s += "X";
        input -= 10;
    }
    while (input >= 9){
        s += "IX";
        input -= 10;
    }
    while (input >= 5){
        s += "V";
        input -= 5;
    }
    while (input >= 4){
        s += "IV";
        input -= 4;
        }
    while (input >= 1){
        s += "I";
        input -= 1;
    }
    }
    return s;
}

截至目前有一个无限循环,我不知道我哪里出错了。输出应该是输入一个介于 0 和 3999 之间的数值,然后在下一行它应该输出罗马数字。谢谢!

【问题讨论】:

  • 检查你的大括号!
  • 您不会从 main 调用任何方法,因此您的程序会在用户为 input 赋值后结束。
  • 我检查了我的大括号!它们都在正确的位置,我是否需要在输出中添加另一种方法?请帮忙!我的方法很糟糕!

标签: java methods roman-numerals


【解决方案1】:
    while (input >= 400){
    s += "CD";
    input -= 400;

} 这里你需要一个“}”

【讨论】:

    【解决方案2】:

    我不知道你是否留下了一些代码,但这段代码不会带你到任何地方,因为 main 方法没有调用其他方法。

    你想要做的是调用可以通过这种方式完成的其他方法:

    System.out.println(convertNumberToNumeral(promptUserForNumber(in)));
    

    立即打印出答案。 在测试您的代码时,我发现了另一个问题,即您的程序没有正确显示数字 3999 和 2999。问题是这样的:

        while (input >= 500){
            s += "D";
            input -= 500;
        }
        while (input >= 400){
            s += "CD";
            input -= 400;
        } // You forgot this curly bracket and you have 
          // to delete one curly bracket at the end of your method
        while (input >= 100){
            s += "C";
            input -= 100;
        }
    

    您的代码也没有很好地编写脚本。有很多未使用的代码。我把它改成了这样:

    public class RomanNumbers{
    static String s = "";
    static Scanner scan;
    
    public static void main (String[] args){
        scan = new Scanner(System.in);
        System.out.println(convertNumberToNumeral(promptUserForNumber()));
    }
    
    private static int promptUserForNumber() {
        System.out.print ("Enter a number between 1 and 3999 (0 to quit): ");
        int input = scan.nextInt();
    
        if(input < 0 && input > 3999){
            System.out.println("Error, number must be between 1 and 3999");
        } else if(input == 0) {
            System.out.println("Goodbye");
        }
        return input;
    }
    
    public static String convertNumberToNumeral(int input) {    
        while (input >= 1000){
            s += "M";
            input -= 1000;
        }
        while (input >= 900){
            s += "CM";
            input -= 900;
        }
        while (input >= 500){
            s += "D";
            input -= 500;
        }
        while (input >= 400){
            s += "CD";
            input -= 400;
        }
        while (input >= 100){
            s += "C";
            input -= 100;
        }
        while (input >= 90){
            s += "XC";
            input -= 90;
        }
        while (input >= 50){
            s += "L";
            input -= 50;
        }
        while (input >= 40){
            s += "XL";
            input -= 40;
        }
        while (input >= 10){
            s += "X";
            input -= 10;
        }
        while (input >= 9){
            s += "IX";
            input -= 10;
        }
        while (input >= 5){
            s += "V";
            input -= 5;
        }
        while (input >= 4){
            s += "IV";
            input -= 4;
            }
        while (input >= 1){
            s += "I";
            input -= 1;
        }
        return s;
    }
    }
    

    【讨论】:

    • 我修复了我的代码,但是现在在第二种方法中,为什么“scan”会出现错误,说无法解析,而在第三种方法中“s”无法解析为方法在任何 while 循环中。
    • scan 出现什么错误? s 也应该被很好地阅读,因为它是在类中声明的,因此所有方法都可以访问。
    【解决方案3】:
    public static void main (String[]args){
        Scanner in = new Scanner(System.in);
        int i = promptUserForNumber(in);
        if (i != -1) {
            //convertNumberToNumeral
        } else {
            //invalid no is entered
        }
    }
    private static int promptUserForNumber(Scanner inScanner) {
        System.out.print ("Enter a number between 1 and 3999 (0 to quit): ");
        int input = inScanner.nextInt();
        if((input < 1 && input > 3999) || input == 0) {
            System.out.println("Error, number must be between 1 and 3999");
             return -1;
        }
        return input;
    }
    

    此外,正如 Jimmy 已经指出的那样,您缺少一个 }。

    【讨论】:

      猜你喜欢
      • 2019-01-09
      • 2011-10-25
      • 2012-10-27
      • 2012-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多