【发布时间】:2020-07-23 18:55:53
【问题描述】:
对不起,如果这是一个简单的错误,我对 Java 还是很陌生,并且对所有规则都有问题。我正在尝试创建一个计算器,它接收罗马数字,将它们转换为整数,它们对它们执行操作,然后通过调用方法将它们转换回罗马数字以打印。我把这些放在一起看一些例子,除了 convertFromRoman 方法之外,它似乎几乎都解决了。如果有人看到任何其他错误,请告诉我,因为我真的感到迷茫。代码如下:
package roman_calculator;
import java.util.Scanner;
public class RomanCalculator {
public static Scanner kbInput = new Scanner(System.in);
public static String doMath(char operator, int firstNum, int secondNum) {
switch (operator) {
case '+':
return convertToRoman(firstNum + secondNum);
case '-':
return convertToRoman(firstNum - secondNum);
case '*':
return convertToRoman(firstNum * secondNum);
case '/':
return convertToRoman(firstNum / secondNum);
}
return "omething";
/*
* This method will perform the arithmetic
* indicated by the operator (+ -* /),
* invoke convertToRoman to convert answer to Roman number,
* then return answer
* */
}
public static char getOperator() {
System.out.println("please choose an operator: +, - , * , or /");
return kbInput.next().charAt(0);
}
public static int getOperands(int which) {
while (true) {
System.out.print("Enter operand" + ": ");
String roman = kbInput.nextLine().toUpperCase();
int romanNum = convertFromRoman(roman);
if (romanNum >= 0)
return romanNum;
else
System.out.println("Bad operand, please try again");
/*This routine should prompt the user to enter Roman number.
convertFromRoman needs to be invoked to convert the Roman number to an integer.
If the input is invalid (-1 returned from convertFromRoman)
then complain and prompt the user again.
*/
}
public static int convertFromRoman(String roman) {
int result = 0;
for (int i = 0; i < roman.length(); i++) {
int s1 = num(roman.charAt(i));
if (s1 < 0) {
System.out.println("Invalid operand");
}
if (i + 1 < roman.length()) {
int s2 = num(roman.charAt(i + 1));
if (s2 < 0) {
System.out.println("Invalid operand");
}
if (s1 >= s2) {
result = result + s1;
} else {
result = result + s2 - s1;
i++;
}
} else {
result = result + s1;
i++;
}
}
return result;
}
/*
* This method will convert Roman number to integer
* return -1 when input is invalid
*
* */
}
public static String convertToRoman(int num) {
System.out.println(num);
String roman = "";
while (num > 0) {
while (num >= 1000) {
roman = roman + "M";
num -= 1000;
}
while (num >= 500) {
roman = roman + "M";
num -= 500;
}
while (num >= 100) {
roman = roman + "D";
num -= 100;
}
while (num >= 50) {
roman = roman + "C";
num -= 50;
}
while (num >= 10) {
roman = roman + "X";
num -= 10;
}
while (num >= 5) {
roman = roman + "V";
num -= 5;
}
while (num >= 1) {
roman = roman + "I";
num -= 1;
}
}
return roman;
}
static int num (char r) {
if (r == 'I')
return 1;
if (r == 'V')
return 5;
if (r == 'X')
return 10;
if (r == 'L')
return 50;
if (r == 'C')
return 100;
if (r == 'D')
return 500;
if (r == 'M')
return 1000;
return -1;
/*
* This method will convert integer to Roman number
* */
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String result;
do {
result = doMath(getOperator(), getOperands(), getOperands());
System.out.println(result);
System.out.println("do you want to continue? y/n");
kbInput.nextLine();
} while (kbInput.nextLine().charAt(0) == 'y');
System.out.println("Have a nice day!");
}
}
【问题讨论】:
-
欢迎来到stackoverflow。通常你需要更具体。 “除了 convertFromRoman 方法之外,它似乎几乎都能解决。如果有人发现任何其他错误,请告诉我,因为我真的感到迷茫。” -> 这太模糊了。如果您不确定,您仍然可以提出其他您认为可能是错误的建议以及您为什么这么认为。
标签: java methods invoke roman-numerals