没有得到我的代码的输出

蓝色的

我将罗马数字转换为十进制形式的代码似乎是正确的,但是由于某些原因,我无法弄清楚如何获得任何形式的输出。最后我想说"The Roman Number" + roman numeral + "is" + decimal number我该怎么办,在哪里放呢?

 import java.util.Scanner;
    public class RomantoInteger {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.println("Enter an Roman numeral with an equivalent between 0 and 3999:");
    }

    public class Roman {

    private final char[] romanNumerals = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };
    private final int[] romanEquivalent = { 1000, 500, 100, 50, 10, 5, 1 };
    private final String romanNum;   
    private int decimalNum; 

    public Roman(String input) {
        romanNum = input.toUpperCase();
    }

    //public void checkRoman(){
    //  for(int x=romanNum.length()-1;x<romanNum.length(); x++){
    //      if(romanNum.charAt(x)=='M')
    //  }
    //}

    /*
     * Method to convert roman number to decimal
     */
    public int convertToDecimal() {
        int sum[];                                     //Create a integer array
        sum = new int[romanNum.length()];               //set the size of sum array to the length of romanNum

        for (int y = 0; y < romanNum.length(); y++) {
            for (int i = 0; i < romanNumerals.length; i++) {
                if ((romanNum.charAt(y) == romanNumerals[i])       
                        && (romanNum.length() > 1)) {             //and if length of romanNum is greater than 1
                    sum[y] = romanEquivalent[i];                       //store the value of the matching romanNumeral to an indexed variable of array sum
                }
                if ((romanNum.charAt(y) == romanNumerals[i])           //if
                        && (romanNum.length() == 1)) {       //and if length of romanNum is just 1
                    System.out.println(romanEquivalent[i]);     //print out the value of the matching romanNumeral
                    break;                                       //terminate looping (for loop)
                }
            }
        }
        return add(sum);                                            //call method add
    }

    public int add(int[] sum) {
        int lastIndex = sum.length - 1;
        int decimalNum = sum[lastIndex];
        int count = 0;
        for (int i = lastIndex; count < sum.length - 1; count++) {
            if ((sum[i - 1] <= sum[i])) {                 
                decimalNum = decimalNum - sum[i - 1];
                --i;
            } else {
                decimalNum = decimalNum + sum[i - 1];
                --i;
            }
        }
        return decimalNum;
    }

}

}
朱尼德

良好的概念需要明确

关于最终变量的事实

最终变量应在减速时初始化,例如

private final romanNum = "SomeRomanNumber";

或者

private final ronamNum = "";

所以你的解决方案是

import java.util.Scanner;


    public class RomantoInteger {
        public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter an Roman numeral with an equivalent between 0 and 3999:");
        int value = input.nextInt();
        RomantoInteger rn = new RomantoInteger();
        Roman roman = rn.new Roman(""+value); //Roman is subclass of RomantoInteger and it is been called from static method so it should be call like this rn.new Roman(""+value);

        // Here you can call any function of Roman class
        }

        public class Roman {

        private final char[] romanNumerals = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };
        private final int[] romanEquivalent = { 1000, 500, 100, 50, 10, 5, 1 };
        private final String romanNum = "";   
        private int decimalNum; 

        public Roman(String input) {
            romanNum = input.toUpperCase();
        }

        //public void checkRoman(){
        //  for(int x=romanNum.length()-1;x<romanNum.length(); x++){
        //      if(romanNum.charAt(x)=='M')
        //  }
        //}

        /*
         * Method to convert roman number to decimal
         */
        public int convertToDecimal() {
            int sum[];                                     //Create a integer array
            sum = new int[romanNum.length()];               //set the size of sum array to the length of romanNum

            for (int y = 0; y < romanNum.length(); y++) {
                for (int i = 0; i < romanNumerals.length; i++) {
                    if ((romanNum.charAt(y) == romanNumerals[i])       
                            && (romanNum.length() > 1)) {             //and if length of romanNum is greater than 1
                        sum[y] = romanEquivalent[i];                       //store the value of the matching romanNumeral to an indexed variable of array sum
                    }
                    if ((romanNum.charAt(y) == romanNumerals[i])           //if
                            && (romanNum.length() == 1)) {       //and if length of romanNum is just 1
                        System.out.println(romanEquivalent[i]);     //print out the value of the matching romanNumeral
                        break;                                       //terminate looping (for loop)
                    }
                }
            }
            return add(sum);                                            //call method add
        }

        public int add(int[] sum) {
            int lastIndex = sum.length - 1;
            int decimalNum = sum[lastIndex];
            int count = 0;
            for (int i = lastIndex; count < sum.length - 1; count++) {
                if ((sum[i - 1] <= sum[i])) {                 
                    decimalNum = decimalNum - sum[i - 1];
                    --i;
                } else {
                    decimalNum = decimalNum + sum[i - 1];
                    --i;
                }
            }
            return decimalNum;
        }

    }

    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

qbasic-我的代码有什么问题我没有得到我想要的

来自分类Dev

我没有得到正确的输出

来自分类Dev

#定义b + c混淆。.我没有得到我期望的输出

来自分类Dev

我需要这样的输出,但没有得到我的查询结果

来自分类Dev

我没有得到我想要的东西

来自分类Dev

没有得到我想要的加入

来自分类Dev

Haskell没有得到我的功能依赖

来自分类Dev

没有得到我想要的迭代

来自分类Dev

为什么我没有得到以下代码的输出需要解释?

来自分类Dev

为什么我在这段代码中没有得到正确的输出?

来自分类Dev

没有得到以下代码的实际输出:

来自分类Dev

为什么我没有得到NSString的输出

来自分类Dev

C ++:我没有得到正确的输出(菜鸟问题)

来自分类Dev

为什么我没有得到想要的输出?

来自分类Dev

没有得到像我这样的输出

来自分类Dev

为什么我没有得到任何输出?

来自分类Dev

为什么我没有得到任何输出

来自分类Dev

为什么我没有得到正确的乘法输出?

来自分类Dev

为什么我没有得到任何输出

来自分类Dev

汇编输出(我没有得到它)

来自分类Dev

我没有得到这段代码在Perl中的作用

来自分类Dev

我的C代码没有得到正确的球体体积?

来自分类Dev

我在机器学习中没有得到这行代码

来自分类Dev

没有值时,我的$ _POST没有得到我的选择名称

来自分类Dev

为什么我没有得到任何输出,对于我在链表中插入的代码?

来自分类Dev

dmesg后没有得到输出

来自分类Dev

jq没有得到预期的输出

来自分类Dev

Java没有得到输出Piglatin

来自分类Dev

强制关闭但没有得到输出