英文:
I cannot get rid of "Method is undefined for type romanCalculator error along with illegal modifiers what is the issue?
问题
抱歉,我看不到您的代码。请注意,您可能需要通过粘贴代码文本来与我共享代码内容。如果您愿意,您可以将代码文本粘贴在消息中,然后我将为您提供翻译。
英文:
Sorry if this is a simple error, I am still very new to Java and have trouble with all the rules. I am trying to create a calculator that takes in roman numerals, converts them to integers, them performs operations with them, and then converts them back roman numerals to print by calling methods. I put this together looking at some examples and it seems to almost all work out except for the convertFromRoman method. If anyone sees any other errors please let me know because I really feel lost. Heres the code:
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!");
}
}
答案1
得分: 0
以下是翻译好的内容:
在以下代码中,缺少一个 }
,最后一个 }
丢失。
public static int getOperands(int which) {
while (true) {
System.out.print("输入操作数" + ": ");
String roman = kbInput.nextLine().toUpperCase();
int romanNum = convertFromRoman(roman);
if (romanNum >= 0)
return romanNum;
else
System.out.println("错误的操作数,请重试");
}
}
并且需要从方法 convertFromRoman
中删除最后一个 }
。
另外,在行 result = doMath(getOperator(), getOperands(), getOperands());
中存在错误,您需要向 getOperands()
传递一个参数,或者将方法签名更改为 getOperator()
或其他您尝试的内容。
给您一个提示:对代码进行格式化可以使其更易阅读,也能看到层级结构(例如对于 Eclipse IDE,可以使用 Ctrl
+ Shift
+ F
)。
英文:
You have a missing }
in the following code, the last }
was missing.
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");
}
}
And you need to remove the last }
from the methode convertFromRoman
.
Also there is a error in the line result = doMath(getOperator(), getOperands(), getOperands());
where you either need to pass a parameter to getOperands()
or change the method signiture to getOperator()
or whatever you are trying.
A tip for you: Formating your code makes it easier to read and see the layers (e.g for Eclispe IDE is it Ctrl
+ Shift
+ F
)
专注分享java语言的经验与见解,让所有开发者获益!
评论