我正在创建一个程序,该程序读取0-999之间的数字,然后以字为单位返回该数字。我已经使用了所有其他数字,但是我不知道如何获取111-119、211-219、311-319等之间的数字,一直到911-919。有人可以帮忙吗?谢谢!
我尝试将numones = number%10;
更改为numones = number%100;
,但这无法读取所有其他数字,并导致array outofbounds
错误。
public static void main(String[] args) {
int numones;
int numtens;
int numhundreds;
String [] ones = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fiveteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty"};
String [] tens = {"","", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
System.out.print("Number: ");
int number = In.nextInt();
while(number !=- 1) {
if (number >= 100 && number <= 999) {
numhundreds = number / 100;
numtens = number/10%10;
numones = number%10;
if (number == 100 || number == 200 || number == 300 || number == 400 || number == 500
|| number == 600 || number == 700 || number == 800 || number == 900){
System.out.println("" + ones[numhundreds] + " " + "hundred");
System.out.print("Number: ");
number = In.nextInt();
}else{
System.out.println("" + ones[numhundreds] + " " + "hundred and " + tens[numtens] + " " + ones[numones]);
System.out.print("Number: ");
number = In.nextInt();
}
}
}
}
}
输入111时,输出为110,而应为111。
答案 0 :(得分:1)
首先,不要使用这样长的“ if”语句:
if (number == 100 || number == 200 || number == 300 || number == 400 || number == 500
|| number == 600 || number == 700 || number == 800 || number == 900)
您可以检查数字除以100是否没有余数,就像这样:
if(number%100==0)
因为100%100 = 0、200%100 = 0、300%100 = 0 ...
现在就像Rishaan said一样,您需要检查“ numtens”值是否为1,也可以使用conditional operator在print语句中执行该操作:
else{
System.out.println("" + ones[numhundreds] + " " +
"hundred and "+ (numtens == 1 ? ones[numones+10] : tens[numtens] + " " + ones[numones]));
System.out.print("Number: ");
number = In.nextInt();
}
答案 1 :(得分:0)
您需要检查十位数是否等于1,并且可以执行以下操作:
if (numtens == 1)
{
System.out.println(... + ones[numones + 10]);
} else {
System.out.println(... + tens[numtens] + ones[numones]);
}
我会采用这种方法,可以轻松地对其进行修改以扩展为更大的数字,并且 imo 看起来更干净。
StringBuilder str = new StringBuilder();
if (numhundreds > 0)
{
str.append(ones[numhundreds] + " hundred");
if (numtens > 0 || numones > 0) str.append(" and ");
}
if (numtens > 0)
{
if (numones == 1)
{
str.append(ones[numtens + numones]);
} else {
str.append(tens[numtens]);
if (numones > 0) str.append(" ");
}
}
if (numones > 1)
{
str.append(ones[numones]);
}
答案 2 :(得分:0)
遗漏了两个案例,即“ ... and [tens]”和“ [tens]”:
public static void main(String[] args) {
int numones;
int numtens;
int numhundreds;
int twenties;
String [] ones = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fiveteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty"};
String [] tens = {"","", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
Scanner In = new Scanner (System.in);
System.out.print("Number: ");
int number = In.nextInt();
while(number !=- 1) {
if (number >= 100 && number <= 999) {
numhundreds = number / 100;
numtens = number/10%10;
numones = number%10;
twenties = number%100;
System.out.println("Nbr"+number+" "+numtens);
if (number == 100 || number == 200 || number == 300 || number == 400 || number == 500
|| number == 600 || number == 700 || number == 800 || number == 900){
System.out.println("" + ones[numhundreds] + " " + "hundred");
System.out.print("Number: ");
number = In.nextInt();
}else if(number%100<20 && numtens==1){
System.out.println("" + ones[numhundreds] + " " + "hundred and " + ones[twenties]);
System.out.print("Number: ");
number = In.nextInt();
}else{
System.out.println("" + ones[numhundreds] + " " + "hundred and " + tens[numtens] + " " + ones[numones]);
System.out.print("Number: ");
number = In.nextInt();
}
}else{
twenties = number%100;
System.out.println("" +ones[twenties]);
System.out.print("Number: ");
number = In.nextInt();
}
}
}