-1

这是代码:

public String ToRoman(int number)
{

    if ((number < 1 || (number > 3999)))

        if (number >= 1000) return "M" + ToRoman(number - 1000);

        if (number >= 900) return "CM" + ToRoman(number - 900); 

        if (number >= 500) return "D" + ToRoman(number - 500);

        if (number >= 400) return "CD" + ToRoman(number - 400);

        if (number >= 100) return "C" + ToRoman(number - 100);            

        if (number >= 90) return "XC" + ToRoman(number - 90);

        if (number >= 50) return "L" + ToRoman(number - 50);

        if (number >= 40) return "XL" + ToRoman(number - 40);

        if (number >= 10) return "X" + ToRoman(number - 10);

        if (number >= 9) return "IX" + ToRoman(number - 9);

        if (number >= 5) return "V" + ToRoman(number - 5);

        if (number >= 4) return "IV" + ToRoman(number - 4);

        if (number >= 1) return "I" + ToRoman(number - 1);

    Scanner myKeyboard = new Scanner (System.in);
    System.out.println("Enter the integer: ");
    number = myKeyboard.nextInt();

    myKeyboard.close();


    }
}

我遇到的问题是我收到一条错误消息“该方法必须返回结果类型字符串”。

4

1 回答 1

1

你需要一个辅助函数:

public String ToRomanWrapper()
{
    Scanner myKeyboard = new Scanner (System.in);
    System.out.println("Enter the integer: ");
    number = myKeyboard.nextInt();
    myKeyboard.close();
    return ToRoman(number);
}

public String ToRoman(int number){
    if ((number < 1 || (number > 3999)))
        return "INVALID"
    if (number >= 1000) return "M" + ToRoman(number - 1000);
    if (number >= 900) return "CM" + ToRoman(number - 900); 
    if (number >= 500) return "D" + ToRoman(number - 500);
    if (number >= 400) return "CD" + ToRoman(number - 400);
    if (number >= 100) return "C" + ToRoman(number - 100);            
    if (number >= 90) return "XC" + ToRoman(number - 90);
    if (number >= 50) return "L" + ToRoman(number - 50);
    if (number >= 40) return "XL" + ToRoman(number - 40);
    if (number >= 10) return "X" + ToRoman(number - 10);
    if (number >= 9) return "IX" + ToRoman(number - 9);
    if (number >= 5) return "V" + ToRoman(number - 5);
    if (number >= 4) return "IV" + ToRoman(number - 4);
    if (number >= 1) return "I" + ToRoman(number - 1);
    return "INVALID"
}

或者,如果您无法定义新函数,您可以在同一个函数中执行此操作:

public String ToRoman(int number)
{                                                                                                         
    if ((number < 1 || (number > 3999)))
    {                           
        Scanner myKeyboard = new Scanner (System.in);
        System.out.println("Enter the integer: ");
        number = myKeyboard.nextInt();
        myKeyboard.close();     
        return ToRoman(number); 
    }                           
    else if (number >= 1000) return "M" + ToRoman(number - 1000);
    else if (number >= 900) return "CM" + ToRoman(number - 900); 
    else if (number >= 500) return "D" + ToRoman(number - 500);
    else if (number >= 400) return "CD" + ToRoman(number - 400);
    else if (number >= 100) return "C" + ToRoman(number - 100);            
    else if (number >= 90) return "XC" + ToRoman(number - 90);
    else if (number >= 50) return "L" + ToRoman(number - 50);
    else if (number >= 40) return "XL" + ToRoman(number - 40);
    else if (number >= 10) return "X" + ToRoman(number - 10);
    else if (number >= 9) return "IX" + ToRoman(number - 9);
    else if (number >= 5) return "V" + ToRoman(number - 5);
    else if (number >= 4) return "IV" + ToRoman(number - 4);
    else if (number >= 1) return "I" + ToRoman(number - 1);
}

我不确定你为什么要这样做。在处理输入的同一函数中读取输入将非常讨厌。

于 2013-10-02T21:51:10.503 回答