我必须创建一个程序,该程序将用户int
输入并以单词输出int
。示例:如果101是输入,则程序输出" ONE HUNDRED AND ONE"。我也不知道如何打印2位数字,如54,34,69等。
import java.util.Scanner;
public class rough{
public static void main(String args[]){
int number = 0;
Scanner scanner = new Scanner(System.in);
System.out.print("Please type a number between 0 and 999 OR type -1 to exit: ");
number = scanner.nextInt();
while(number!=-1){
if(number>=0 && number<=999){
if(number==0){
System.out.print("ZERO");
} else if(number==100||number==200||number==300||number==400||number==500||number==600||number==700||number==800||number==900){
hundred(number);
}else if(number >=1 && number <=19){
from1To19(number);
}else if(number==20||number==30||number==40||number==50||number==60||number==70||number==80||number==90){
tens(number);
}else if(number>=101){
From101(number);
}
} else{
System.out.print("NUMBER OUT OF RANGE");
}
System.out.print("\nPlease type a number between 0 and 999 OR type -1 to exit: ");
number = scanner.nextInt();
}
}
public static void hundred(int num){
String HUNDREDS[] = {" ", "ONE HUNDRED", "TWO HUNDRED", "THREE HUNDRED", "FOUR HUNDRED", "FIVE HUNDRED", "SIX HUNDRED", "SEVEN HUNDRED", "EIGHT HUNDRED", "NINE HUNDRED"};
if(num>=100){
int ArrayOrder = num/100;
System.out.println(HUNDREDS[ArrayOrder]);
}else{
System.out.println(HUNDREDS[num]);
}
}
public static void tens(int num){
String TENS[] = {" ", " ", " TWENTY", " THIRTY", " FOURTY", " FIFTY", " SIXTY", " SEVENTY", " EIGHTY", " NINETY"};
if(num >=20){
int ArrayOrder = num/10;
System.out.println(TENS[ArrayOrder]);
}else{
System.out.println(TENS[num]);
}
}
public static void from1To19(int num){
String ONES[] = {" ", " ONE", " TWO", " THREE", " FOUR", " FIVE", " SIX", " SEVEN", " EIGHT", " NINE", " TEN", " ELEVEN", " TWELVE", " THIRTEEN", " FOURTEEN", " FIFTEEN", " SIXTEEN", " SEVENTEEN", " EIGHTEEN", " NINETEEN"};
System.out.println(ONES[num]);
}
public static void From101(int num){
int first = num%10;//last number of ###
num = num;//##
int second = num%10;//last number of ##
num = num;//#
hundred(num); tens(second); from1To19(first);
}
}
所以我的问题是:1。我想让From101中的3种方法在同一行上打印。
如果您输入678或445.我会得到:
六百 八十 EIGHT
而不是六十万和七十八。
答案 0 :(得分:1)
我稍微修改了您的代码,添加了towDigitNumber()
的新方法,您必须删除if(number >=1 && number <=19)
上的if条件并添加新条件来管理两位数字,即if(number >=1 && number<=99)
修改后的代码:我添加了评论,其中包含我所做的更改。
package com.org.test;
import java.util.Scanner;
public class rough{
public static void main(String args[]){
int number = 0;
Scanner scanner = new Scanner(System.in);
System.out.print("Please type a number between 0 and 999 OR type -1 to exit: ");
number = scanner.nextInt();
while(number!=-1){
if(number>=0 && number<=999){
if(number==0){
System.out.print("ZERO");
} else if(number==100||number==200||number==300||number==400||number==500||number==600||number==700||number==800||number==900){
hundred(number);
}else if(number==20||number==30||number==40||number==50||number==60||number==70||number==80||number==90){
tens(number);
}else if(number>=101){
From101(number);
}else if(number >=1 && number<=99){ // checking for numbers which are less then 99.
if(number >=1 && number <=19){
from1To19(number);
}else{
twoDigitNumber(number); // added new method that call two digit number
}
}
} else{
System.out.print("NUMBER OUT OF RANGE");
}
System.out.print("\nPlease type a number between 0 and 999 OR type -1 to exit: ");
number = scanner.nextInt();
}
}
public static void hundred(int num){
String HUNDREDS[] = {" ", "ONE HUNDRED", "TWO HUNDRED", "THREE HUNDRED", "FOUR HUNDRED", "FIVE HUNDRED", "SIX HUNDRED", "SEVEN HUNDRED", "EIGHT HUNDRED", "NINE HUNDRED"};
if(num>=100){
int ArrayOrder = num/100;
System.out.print(HUNDREDS[ArrayOrder]); // add print statement not println
}else{
System.out.print(HUNDREDS[num]); // add print statement not println
}
}
public static void tens(int num){
String TENS[] = {" ", " ", " TWENTY", " THIRTY", " FOURTY", " FIFTY", " SIXTY", " SEVENTY", " EIGHTY", " NINETY"};
if(num >=20){
int ArrayOrder = num/10;
System.out.print(TENS[ArrayOrder]); // add print statement not println
}else{
System.out.print(TENS[num]); // add print statement not println
}
}
public static void from1To19(int num){
String ONES[] = {" ", " ONE", " TWO", " THREE", " FOUR", " FIVE", " SIX", " SEVEN", " EIGHT", " NINE", " TEN", " ELEVEN", " TWELVE", " THIRTEEN", " FOURTEEN", " FIFTEEN", " SIXTEEN", " SEVENTEEN", " EIGHTEEN", " NINETEEN"};
System.out.println(ONES[num]);
}
public static void twoDigitNumber(int num){
String ONES[] = {" ", " ONE", " TWO", " THREE", " FOUR", " FIVE", " SIX", " SEVEN", " EIGHT", " NINE", " TEN"};
String PRO[] = {"",""," TWENTY", "THRTY", "FOURTY", " FIFTY", " SIXTY", " SEVENTY", " EIGHTY", " NINETY"};
System.out.print(PRO[(num/10)]);
System.out.print(ONES[(num%10)]);
}
public static void From101(int num){
int first = num%10;// find the first number
num = num;//##
int second = (num/10)%10;// find second number
num = num;//#
hundred(num); tens(second); from1To19(first);
}
}