如何编写一个程序,用户输入一个数字作为输入(数字最多可以是六位数字),然后程序将其转换为文本?
例如,用户输入 100 并打印输出“一百”。
代码必须是Java。用户最多可随机输入六位数字
我写了一个代码如下,但是是两位数的,多位数的我就留着写了
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("enter the number");
int n=sc.nextInt();
int n1=n,n2=n;
int b=n1%10,a=n2/10; // n1/10 means last digit is removed and n2%10 means last digit by modulus
String[] single_digits = new String[]{"zero","one","two","three","four","five", "six","seven","eight","nine"};
String[] two_digits = new String[]{"","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen", "nineteen"};
String[] tens_multiple = new String[]{"","","twenty","thirty","forty","fifty","sixty", "seventy","eighty","ninety"};
if(a==1)
{
System.out.println(two_digits[b+1]);
}
else if(b==0)
System.out.println(tens_multiple[a]);
else
System.out.println(tens_multiple[a]+"-"+single_digits[b]);
答案 0 :(得分:0)
您所需要的只是可重用的方法。例如,打印 1
位数字、2
位数字等的方法...然后在打印第一个后在 printOne
中调用 printTwo
,如图所示...< /p>
import java.util.Scanner;
public class x {
// Instance fields are below....
// Placed below for easy readability of code...
public static void main (String args[]) {
// You know what this does...
System.out.print (" Input number\n > ");
Scanner sc = new Scanner (System.in);
// Create int array to collect all digits...
String number = Integer.toString (sc.nextInt ());
int[] digits = new int[number.length ()];
for (int i = 0; i < number.length (); i++)
digits [i] = Integer.parseInt (number.charAt (i) + "");
// All you need is a simple method to print a given amount..even up to ten million
switch (digits.length) {
case 1:
printOne (digits);
break;
case 2:
printTwo (digits);
break;
case 3:
printThree (digits);
break;
case 4:
printFour (digits);
break;
case 5:
printFive (digits);
break;
case 6:
printSix (digits);
break;
}
}
static void printOne (int[] arr) {
// Just print coresponding value, easy since i converted to int[]
System.out.print (below_ten [arr [0]]);
}
static void printTwo (int[] arr) {
// check if it is a ~teen/not (you know what i mean)
if (arr [0] == 1)
System.out.print (below_twenty [arr [1] + 1]);
else {
System.out.print (below_hundered [arr [0]] + " ");
if (arr [1] != 0) // added this to prevent 30 = thirty zero
printOne (new int[] {arr [1]}); // this wont be needed for the ~teens
}
}
static void printThree (int[] arr) {
// now everything is siple, just print first digits and call previous, to call previous...
// you get where that goes...
System.out.print (below_thousand [arr [0]] + " ");
printTwo (new int[] {arr [1], arr [2]});
}
// Now you have the algorithm, Challenge=print up to ten million... :-)
static void printFour (int[] arr) {
// challenge
}
static void printFive (int[] arr) {
//exercise
}
static void printSix (int[] arr) {
//bounty :-b)
}
// Placed below for easy readability of code...
static String hundred = " hundred";
static String[] below_ten = new String[] {
"zero","one","two","three","four","five", "six","seven","eight","nine"
};
static String[] below_twenty = new String[] {
"","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen", "nineteen"
};
static String[] below_hundered = new String[] {
"","","twenty","thirty","forty","fifty","sixty", "seventy","eighty","ninety"
};
static String[] below_thousand = new String[] {
"", below_ten [1] + hundred, below_ten [2] + hundred, below_ten [3] + hundred, below_ten [4] + hundred, below_ten [5] + hundred, below_ten [6] + hundred, below_ten [7] + hundred, below_ten [8] + hundred, below_ten [9] + hundred,
};
}
编辑 如果你在使用其他方法时遇到问题......比魅力更有效......
static void printFour (int[] arr) {
System.out.print (below_million [arr [0]]);
printThree (new int[] {arr [1], arr [2], arr [3]});
}
static void printFive (int[] arr) {
printTwo (new int[] {arr [0], arr [1]});
System.out.print (thousand);
printThree (new int[] {arr [2], arr [3], arr [4]});
}
static void printSix (int[] arr) {
printThree (new int[] {arr [0], arr [1], arr [2]});
System.out.print (thousand);
printThree (new int[] {arr [3], arr [4], arr [5]});
}
static String thousand = " thousand ";
static String[] below_million = new String[] {
"", below_ten [1] + thousand, below_ten [2] + thousand, below_ten [3] + thousand, below_ten [4] + thousand, below_ten [5] + thousand, below_ten [6] + thousand, below_ten [7] + thousand, below_ten [8] + thousand, below_ten [9] + thousand,
};