将整数转换为字符串表示 - 有没有办法缩短我的代码?

时间:2015-02-10 04:32:30

标签: java android

我想创建一个程序,允许用户在EditText中输入一个数字并显示其英文单词表示。使用1-1000的输入创建此程序的最快方法是什么?

示例:

Input: 1
Output: One

我使用的代码:

public class MainActivity extends Activity { 

Button submit;
EditText ET;
TextView output;

@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

submit = (Button) findViewById(R.id.SubmitBTN);
ET = (EditText) findViewById(R.id.editText1);
output = (TextView) findViewById(R.id.textView1);

Submit.setOnClickListener(new OnClickListener(){

int num = Integer.parseInt(ET.getText().toString());

Switch(num){

case: 1
output.setText("One");
break;

case: 2
output.setText("Two");
break;

case: 3
output.setText("Three");
break;

case: 4
output.setText("Four");
break;

case: 5
output.setText("Five");
break;

}

         });
     }
}

3 个答案:

答案 0 :(得分:0)

Java或Android API都不包含对拼写数值的任何支持,但您可以实现自己的算法或尝试使用these回答。

答案 1 :(得分:0)

您可以使用此class并通过main

进行调用
NumberToWord convertToWord = NumberToWord()

convertToWord.convert(num); //num <- your variable that holds the input

这会从one转换为quintillion

public class NumberToWord  

{
    private static final String[] specialNames = {
        "",
        " thousand",
        " million",
        " billion",
        " trillion",
        " quadrillion",
        " quintillion"
    };

    private static final String[] tensNames = {
        "",
        " ten",
        " twenty",
        " thirty",
        " fourty",
        " fifty",
        " sixty",
        " seventy",
        " eighty",
        " ninety"
    };

    private static final String[] numNames = {
        "",
        " one",
        " two",
        " three",
        " four",
        " five",
        " six",
        " seven",
        " eight",
        " nine",
        " ten",
        " eleven",
        " twelve",
        " thirteen",
        " fourteen",
        " fifteen",
        " sixteen",
        " seventeen",
        " eighteen",
        " nineteen"
    };

    private String convertLessThanOneThousand(int number) {
        String current;

        if (number % 100 < 20){
            current = numNames[number % 100];
            number /= 100;
        }
        else {
            current = numNames[number % 10];
            number /= 10;

            current = tensNames[number % 10] + current;
            number /= 10;
        }
        if (number == 0) return current;
        return numNames[number] + " hundred" + current;
    }

    public String convert(int number) {

        if (number == 0) { return "zero"; }

        String prefix = "";

        if (number < 0) {
            number = -number;
            prefix = "negative";
        }

        String current = "";
        int place = 0;

        do {
            int n = number % 1000;
            if (n != 0){
                String s = convertLessThanOneThousand(n);
                current = s + specialNames[place] + current;
            }
            place++;
            number /= 1000;
        } while (number > 0);

        return (prefix + current).trim();
    }

}

信用:http://javahungry.blogspot.com/2014/05/convert-math-number-to-equivalent-readable-word-in-java-code-with-example.html

答案 2 :(得分:-1)

            public class MainActivity extends Activity { 

            Button submit;
            EditText ET;
            TextView output;
            String words[]={"one","two","three","four","five"};

            @override 
            protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            submit = (Button) findViewById(R.id.SubmitBTN);
            ET = (EditText) findViewById(R.id.editText1);
            output = (TextView) findViewById(R.id.textView1);

            Submit.setOnClickListener(new OnClickListener(){ 

            int num = Integer.parseInt(ET.getText().toString());
            if(num<5)
            output.setText(words[num+1]); 


                     }); 
                 } 
            }