Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何让罗马数字计算器处理负数_Java_Calculator_Roman Numerals - Fatal编程技术网

Java 如何让罗马数字计算器处理负数

Java 如何让罗马数字计算器处理负数,java,calculator,roman-numerals,Java,Calculator,Roman Numerals,当计算器的结果为负数时,罗马数字计算器不工作, 我真的不知道该怎么修。 当计算器给出一个正值时,一切正常 例如,结果将如下所示 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 r

当计算器的结果为负数时,罗马数字计算器不工作, 我真的不知道该怎么修。 当计算器给出一个正值时,一切正常 例如,结果将如下所示

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;
}
publicstaticvoidmain(字符串[]args){
System.out.println(“请输入所需的两个整数值”
+“用“)”完成操作;
扫描仪扫描=新扫描仪(System.in);
int first=scan.nextInt();
int sec=scan.nextInt();
System.out.println(“请输入要执行的操作”);
字符串opera=scan.next();
System.out.println(“这是答案”);
int值=Acalc(opera、first、sec);
字符串罗马=罗马(值);
System.out.println(“这是罗马数字的答案”);
System.out.println(罗马);
}
公共静态int-Acalc(字符串歌剧,int-n1,int-n2){
int结果=0;
//编写计算器
如果(opera.equals(“+”){result=n1+n2;}
如果(opera.equals(“-”){result=n1-n2;}
如果(opera.equals(“*”){result=n1*n2;}
如果(opera.equals(“/”){result=n1/n2;}
系统输出打印项次(结果);
返回结果;
}
公共静态字符串罗马(双输入){
字符串s=“”;
如果(输入=100){
s+=“C”;
输入-=100;
}
同时(输入>=90){
s+=“XC”;
输入-=90;
}
同时(输入>=50){
s+=“L”;
输入-=50;
}
同时(输入>=40){
s+=“XL”;
输入-=40;
}
同时(输入>=10){
s+=“X”;
输入-=10;
}
while(输入>=9){
s+=“IX”;
输入-=9;
}
while(输入>=5){
s+=“V”;
输入-=5;
}
while(输入>=4){
s+=“IV”;
输入-=4;
}
while(输入>=1){
s+=“I”;
输入-=1;
} 
返回s;
}

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

相反,您是否可以存储
结果
是否为正或负?如果是负数,您可以将其更改为正数,执行罗马数字转换,然后在
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
}
orig_result=acalc(一些参数)
如果(原始结果>0){
结果=结果
}
如果(原始结果<0){
结果=结果*-1
}
//转换成罗马数字
如果(原始结果<0){
s=“-”+s
}

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

相反,您是否可以存储
结果
是否为正或负?如果是负数,您可以将其更改为正数,执行罗马数字转换,然后在
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
}
orig_result=acalc(一些参数)
如果(原始结果>0){
结果=结果
}
如果(原始结果<0){
结果=结果*-1
}
//转换成罗马数字
如果(原始结果<0){
s=“-”+s
}

因此,罗马数字不能代表零或负数,但下面的编辑应该可以让您的程序执行此操作。

如果您有:

if (input <1 || input < 999)
    System.out.println("negative roman numeral value ");
if(输入999)
返回“输出太大”;
else if(输入=0)
返回“空”;

Nulla在拉丁语中表示零,因为不存在与之等价的罗马数字。

因此,罗马数字不能代表零或负数,但以下编辑应允许您的程序执行此操作。

如果您有:

if (input <1 || input < 999)
    System.out.println("negative roman numeral value ");
if(输入999)
返回“输出太大”;
else if(输入=0)
返回“空”;

Nulla在拉丁语中表示零,因为不存在罗马数字等价物。

罗马人甚至有负数吗?诀窍是实现II的补码运算。罗马数字既不能表示负数,也不能表示零。你能解释这一行吗
if(输入@rp,这是由于Paul指出的错误。77小于999,因此它输出该字符串(并继续,这可能不应该)。罗马人甚至有负数吗?诀窍是实现II的补码运算。罗马数字既不能表示负数,也不能表示零。你能解释这一行吗?
if(输入@rp,这是由于Paul指出的错误。77小于999,因此它输出该字符串(并继续,这可能不应该)。