有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java中将整数转换为罗马数字的变量

Java类的家庭作业指导我们从用户那里获取整数输入(最多3999),并将其转换为罗马数字。我在代码中遇到的问题是,当我在测试时输入整数时,它将整数设置为0,而罗马数字输出为null。这可能是在对象中声明变量的结果,但我现在不确定

public class RomanNumerals
{

    // instance variables

    int input;
    String romanNum;

    /**
     * Object that records the input from a user.
     */
    public RomanNumerals()
    {
        int input=0;
        String romanNum="";

        //System.out.println("Input a number to get a Roman Numeral");
        //int input = integer.nextInt();
    }
    public int Scanner()
    {
        Scanner integer = new Scanner(System.in);
        System.out.println("Input a number to get a Roman Numeral: ");
        int input = integer.nextInt();
        return input;
    }
    /**
     * Object that takes the input from the user and determines what roman numerals are appropriate.
     */
    public String Int2Roman()
    {
        if (input < 1 || input > 3999)
        {
            System.out.println("Please input a number between 1 and 3999.");
        }
        while (input >= 1000)
        {
            romanNum = romanNum + "M";
            input = input - 1000;
        }
        while (input >= 900)
        {
            romanNum = romanNum + "CM";
            input = input - 900;
        }
        while (input >= 500)
        {
            romanNum = romanNum + "S";
            input = input - 500;
        }
        while (input >= 400)
        {
            romanNum = romanNum + "CS";
            input = input - 1000;
        }
        while (input >= 100)
        {
            romanNum = romanNum + "C";
            input = input - 100;
        }
        while (input >= 90)
        {
            romanNum = romanNum + "XC";
            input = input - 1000;
        }
        while (input >= 50)
        {
            romanNum = romanNum + "L";
            input = input - 1000;
        }
        while (input >= 40)
        {
            romanNum = romanNum + "XL";
            input = input - 40;
        }
        while (input >= 10)
        {
            romanNum = romanNum + "X";
            input = input - 10;
        }
        while (input >= 9)
        {
            romanNum = romanNum + "IX";
            input = input - 9;
        }
        while (input >= 5)
        {
            romanNum = romanNum + "V";
            input = input - 5;
        }
        while (input >= 4)
        {
            romanNum = romanNum + "IV";
            input = input - 4;
        }
        while (input >= 1)
        {
            romanNum = romanNum + "I";
            input = input - 5;
        }
        System.out.println("The Roman Numeral of " + input + " is " + romanNum);
        return romanNum;
    }
    public static void main(String[] args)
    {
        RomanNumerals a = new RomanNumerals();
        a.Scanner();
        a.Int2Roman();
    }
}

共 (2) 个答案

  1. # 1 楼答案

    试试这个,看看是否有效,对我有效,简单,基本,非常直接

    public static void main(String[] args) {
    
    
    
        //INPUNT INTEGER VALUE
        // OUTPUT STRING VALUE (SYSTEM CONSOLE OUTPUT
    
        //**********************************************************************************//
    
        int _inValue = 117; //int parameter value
    
    
        //**********************************************************************************//
    
        //check if the number is between 1 and 3999
        if (_inValue < 1){
            System.out.println("integer can't be less than 1");
            return;
        }
        if (_inValue > 3999){
            System.out.println("integer can't be more than 3999");
            return;
        }
    
        //convert the value in array to work by separate numbers
        char[] arg = String.valueOf(_inValue).toCharArray();
    
        //count the number of characters to use a case condition or length of the integer in value
        int _count = Integer.toString(_inValue).length();
    
        // output variable
        String _output = "";
    
        //case condition base in the length of the integer value
        switch (_count){
            case 1: // case base in one figure (1)
                switch (_inValue){
                    case 1:
                        _output = "I";
                        break;
                    case 2:
                        _output = "II";
                        break;
                    case 3:
                        _output = "III";
                        break;
                    case 4:
                        _output = "IV";
                        break;
                    case 5:
                        _output = "V";
                        break;
                    case 6:
                        _output = "VI";
                        break;
                    case 7:
                        _output = "VII";
                        break;
                    case 8:
                        _output = "VIII";
                        break;
                    case 9:
                        _output = "IX";
                        break;
                }
                break;
            case 2: //case base in two figures (10)
                switch (Character.getNumericValue(arg[0])){//base in the first character of the integer value lets use another case condition
                    case 1:
                        _output = "X";
                        break;
                    case 2:
                        _output = "XX";
                        break;
                    case 3:
                        _output = "XXX";
                        break;
                    case 4:
                        _output = "XL";
                        break;
                    case 5:
                        _output = "L";
                        break;
                    case 6:
                        _output = "LX";
                        break;
                    case 7:
                        _output = "LXX";
                        break;
                    case 8:
                        _output = "LXXX";
                        break;
                    case 9:
                        _output = "XC";
                        break;
                }
                _output += _getValue10(Character.getNumericValue(arg[1])); //lets call this function to get the other two figures or number to complete the integer amount and build the roman value in string
                break;
            case 3: //case base in three figures (100)
                switch (Character.getNumericValue(arg[0])){ //base in the first character of the integer value lets use another case condition
                    case 1:
                        _output = "C";
                        break;
                    case 2:
                        _output = "CC";
                        break;
                    case 3:
                        _output = "CCC";
                        break;
                    case 4:
                        _output = "CD";
                        break;
                    case 5:
                        _output = "D";
                        break;
                    case 6:
                        _output = "DC";
                        break;
                    case 7:
                        _output = "DCC";
                        break;
                    case 8:
                        _output = "DCCC";
                        break;
                    case 9:
                        _output = "CM";
                        break;
                }
                _output += _getValue100(Character.getNumericValue(arg[1]),Character.getNumericValue(arg[2])); //lets call this function to get the other two figures or number to complete the integer amount and build the roman value in string
                break;
            case 4: // case base in 4 figures (1000)
    
                if(_inValue != 1000){ // validate the value if its different of a 1000
                    // use a cycle to concat the string base of the number of the integer in value
                    for (int i = 0; i < Character.getNumericValue(arg[0]);i++ ){
                        _output += "M";
                    }
                }else{
                    // if its a 1000 use this one and that's it
                    _output += "M";
                }
                _output += _getValue1000(Character.getNumericValue(arg[1]),Character.getNumericValue(arg[2]),Character.getNumericValue(arg[3])); //lets call this function to get the other three figures or number to complete the integer amount and build the roman value in string
                break;
        }
        System.out.println("the integer number is: " + _inValue);
        System.out.println("the roman number is: " + _output);
    
    }
    
    ///method who return the other three values
    private static String _getValue1000(int valueOne, int valueTwo, int valueThree){
        String _output = "";
        if(valueOne != 0){
            switch (valueOne){
                case 1:
                    _output += "C";
                    break;
                case 2:
                    _output += "CC";
                    break;
                case 3:
                    _output += "CCC";
                    break;
                case 4:
                    _output += "CD";
                    break;
                case 5:
                    _output += "D";
                    break;
                case 6:
                    _output += "DC";
                    break;
                case 7:
                    _output += "DCC";
                    break;
                case 8:
                    _output += "DCCC";
                    break;
                case 9:
                    _output += "CM";
                    break;
            }
    
        }
        if(valueTwo != 0){
            switch (valueTwo){
                case 1:
                    _output += "X";
                    break;
                case 2:
                    _output += "XX";
                    break;
                case 3:
                    _output += "XXX";
                    break;
                case 4:
                    _output += "XL";
                    break;
                case 5:
                    _output += "L";
                    break;
                case 6:
                    _output += "LX";
                    break;
                case 7:
                    _output += "LXX";
                    break;
                case 8:
                    _output += "LXXX";
                    break;
                case 9:
                    _output += "XC";
                    break;
            }
        }
        if(valueThree != 0){
            switch (valueThree){
                case 1:
                    _output += "I";
                    break;
                case 2:
                    _output += "II";
                    break;
                case 3:
                    _output += "III";
                    break;
                case 4:
                    _output += "IV";
                    break;
                case 5:
                    _output += "V";
                    break;
                case 6:
                    _output += "VI";
                    break;
                case 7:
                    _output += "VII";
                    break;
                case 8:
                    _output += "VIII";
                    break;
                case 9:
                    _output += "IX";
                    break;
            }
    
        }
    
        return _output;
    }
    
    /// method who return the other two values
    private static String _getValue100(int valueOne, int valueTwo){
        String _output = "";
    
        if(valueOne != 0){
            switch (valueOne){
                case 1:
                    _output += "X";
                    break;
                case 2:
                    _output += "XX";
                    break;
                case 3:
                    _output += "XXX";
                    break;
                case 4:
                    _output += "XL";
                    break;
                case 5:
                    _output += "L";
                    break;
                case 6:
                    _output += "LX";
                    break;
                case 7:
                    _output += "LXX";
                    break;
                case 8:
                    _output += "LXXX";
                    break;
                case 9:
                    _output += "XC";
                    break;
            }
    
        }
        if(valueTwo != 0){
            switch (valueTwo){
                case 1:
                    _output += "I";
                    break;
                case 2:
                    _output += "II";
                    break;
                case 3:
                    _output += "III";
                    break;
                case 4:
                    _output += "IV";
                    break;
                case 5:
                    _output += "V";
                    break;
                case 6:
                    _output += "VI";
                    break;
                case 7:
                    _output += "VII";
                    break;
                case 8:
                    _output += "VIII";
                    break;
                case 9:
                    _output += "IX";
                    break;
            }
    
        }
    
        return _output;
    }
    
    /// method who return one other value
    private static String _getValue10(int valueOne){
        String _output = "";
    
        if(valueOne != 0){
            switch (valueOne){
                case 1:
                    _output += "I";
                    break;
                case 2:
                    _output += "II";
                    break;
                case 3:
                    _output += "III";
                    break;
                case 4:
                    _output += "IV";
                    break;
                case 5:
                    _output += "V";
                    break;
                case 6:
                    _output += "VI";
                    break;
                case 7:
                    _output += "VII";
                    break;
                case 8:
                    _output += "VIII";
                    break;
                case 9:
                    _output += "IX";
                    break;
            }
    
        }
    
        return _output;
    }
    
  2. # 2 楼答案

    您的Scanner方法不会将键盘输入存储在字段(实例变量)input中,而是存储在局部变量input

    替换

    int input = integer.nextInt(); //this declares a new local variable
    

    input = integer.nextInt(); //uses the existing field