在Android

时间:2016-06-16 03:09:14

标签: java android

关于我关于setting the TextView as the method output的最后一个问题,这个问题是关于将整数拆分成单独的数字。我的Roman Numeral程序在这里使用翻译方法:

'public static String translator(int integer) {
        String result = "";
        LinkedList<String> stack = new LinkedList<String>();
        // if (integer > 0 && integer <= 4999) {
        //ArrayList<Integer> placement = new ArrayList<Integer>();
        int place = (int) Math.log10(integer);
        for (int i = 0; i <= place; i++) {
            //while(integer > 0){
            //System.out.println(integer);
            int placeOfValue = integer % 10;
            //stack.push(placeOfValue);
            //System.out.print(stack);

            //System.out.print(placeOfValue +":" + i);
            String placement = "";
            switch (i) {
                case 0:
                    placement = ones(placeOfValue);

                    break;
                case 1:
                    placement = tens(placeOfValue);

                    break;
                case 2:
                    placement = hundreds(placeOfValue);

                    break;
                case 3:
                    placement = thousands(placeOfValue);

                    break;
                default:
                    break;
            }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
                stack.push(placement);

            }
            integer = integer / 10;

            //System.out.print(placement);
            // System.out.println(placement.size());
            //}
//              for(int j = 0; j < placement.size(); j++){
//                                    double tenthPower = Math.floor(Math.log10(placement.get(j)));
//                                    double place = Math.pow(10, tenthPower);
//                                    System.out.println(place);
//
//              }
            // }

            while (!stack.isEmpty()) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
                    result += stack.pop();
                    //System.out.print(stack.pop());
                }

            }

//        } else {
//            System.out.println("Please enter an integer between 0 and 4,999.");
//        }

        }
        return result;
    }

`

翻译器方法的工作方式是遍历for循环,循环遍历整数的大小,然后将数字指定给定的位置。然后,从放置中,它将查看放置方法并返回数字。由于对于LinkedList反向添加整数,我已经推送堆栈并根据此帖子How to get the separate digits of an int number?弹出它以获得正确的顺序。然而,程序继续以相反的顺序打印出数字,这不是我想要的。我已经调试了程序,但没有运气,感觉就像使用pop方法关闭并将其添加到输出但是我无法弄清楚是什么。

1 个答案:

答案 0 :(得分:0)

我最终通过取消注释将stack.push()与stack.pop()分开的括号来解决这个问题。之后,其余的代码就可以了!

相关问题