【问题标题】:how to get a roman numeral calculator to work with negative numbers如何让罗马数字计算器处理负数
【发布时间】:2014-12-23 02:13:18
【问题描述】:

当计算器的结果为负数时,罗马数字计算器将无法工作, 老实说,我不知道如何解决它。 当计算器给出正值时,一切正常 例如,结果将如下所示

please enter the two two integer values that you want to vomplete the operation with
> 33
> 44
please enter the operation you want preformed
> +
Here is the answer 77 negative roman numeral value   Here is the answer in roman numerals
LXXVII

代码在这里:

public static void main(String[] args) {
  System.out.println("please enter the two two integer values that you want"
           + " to vomplete the operation with ");
  Scanner scan = new Scanner(System.in);
  int first = scan.nextInt();
  int sec = scan.nextInt();
  System.out.println(" please enter the operation you want preformed");
  String opera = scan.next();
  System.out.println(" Here is the answer");
  int value = Acalc(opera, first, sec);
  String roman = Roman(value);
  System.out.println(" Here is the answer in roman numerals ");
  System.out.println(roman);            
}

public static int Acalc(String opera, int n1, int n2){
  int result = 0;
  //Write the calulator 

 if (opera.equals("+")) {result=n1+n2;}

 if (opera.equals("-")) {result=n1-n2;}

 if (opera.equals("*")) {result=n1*n2;}

 if (opera.equals("/")) {result=n1/n2;}

 System.out.println(result);

 return result;
}

public static String Roman(double input){

  String s = "";

  if (input <1 || input < 999)
    System.out.println("negative roman numeral value ");

  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 -= 9;
  }
  while (input >= 5) {
    s += "V";
    input -= 5;
  }
  while (input >= 4) {
    s += "IV";
    input -= 4;
  }
  while (input >= 1) {
    s += "I";
    input -= 1;
  } 
  return s;
}

【问题讨论】:

  • 罗马人有没有负数?
  • 诀窍是实现 II 的补码算法。
  • 罗马数字不能表示负数,也不能表示零。
  • 你能解释一下这条线吗? if (input &lt;1 || input &lt; 999)
  • @rp,这是由于保罗指出的错误。 77 小于 999 因此它输出该字符串(并继续,它可能不应该)。

标签: java calculator roman-numerals


【解决方案1】:

因此,尽管如此,罗马数字不能代表零或负数,但以下编辑应该可以让您的程序做到这一点。

你在哪里:

if (input <1 || input < 999)
    System.out.println("negative roman numeral value ");

用途:

if (input < 0){
    s="-";
    input *= -1;
}
else if (input > 999)
    return "Output too large";
else if (input == 0)
    return "nulla";

Nulla 在拉丁语中表示零,因为不存在对应的罗马数字。

【讨论】:

    【解决方案2】:

    这不起作用的原因是,当result 为负数时,因为input -= # 没有意义,因为这会使结果更负数。它应该是input += #。但是,这会为您的代码增加相当多的工作量/长度。

    相反,您是否可以存储result 是正数还是负数?那么如果是负数,可以改成正数,转换成罗马数字,然后在s前面加一个负号。

    例如:

    orig_result = acalc(some parameters)
    
    if (orig_result > 0){
        result = result
    }
    if (orig_result < 0){
        result = result * -1
    }
    
    //Convert to roman numerals
    
    if (orig_result < 0){
        s = "-" + s
    }
    

    【讨论】:

      猜你喜欢
      • 2014-01-23
      • 1970-01-01
      • 2020-12-28
      • 1970-01-01
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      • 2015-07-24
      • 1970-01-01
      相关资源
      最近更新 更多