gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:21:33 26 4
gpt4 key购买 nike

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

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;
}

最佳答案

因此,就其值(value)而言,罗马数字不能代表零或负数,但以下编辑应该让您的程序能够做到这一点。

您所在的地方:

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 在拉丁语中是“零”的意思,因为不存在等效的罗马数字。

关于java - 如何让罗马数字计算器处理负数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27613180/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com