【问题标题】:I cannot get rid of "Method is undefined for type romanCalculator error along with illegal modifiers what is the issue?我无法摆脱“类型 romanCalculator 错误的方法未定义以及非法修饰符是什么问题?
【发布时间】: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


【解决方案1】:

您在以下代码中缺少},最后一个} 丢失。

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

您需要从方法convertFromRoman 中删除最后一个}

result = doMath(getOperator(), getOperands(), getOperands()); 行中也有一个错误,您需要将参数传递给getOperands() 或将方法签名更改为getOperator() 或您正在尝试的任何内容。

给您的提示:格式化您的代码可以更容易地阅读和查看层(例如,对于 Eclispe IDE 是 Ctrl + Shift + F

【讨论】:

  • 非常感谢,我不敢相信我没有听懂。如果您不介意,我还有一个问题,也许我将不得不发布新问题,但对于getOperand 它要求我两次(例如控制台输出:please choose an operator: +, - , * , or / * Enter operand: Enter operand: xx 0 do you want to continue? y/n ,因为程序将其读取为 20*0=0不是 10*10。)我假设它允许我输入两个输入,但我不确定如何输入两个单独的值。你看到问题了吗?
猜你喜欢
  • 2021-03-19
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
  • 2015-07-05
  • 2015-04-23
  • 2016-01-08
  • 1970-01-01
相关资源
最近更新 更多