gpt4 book ai didi

java程序将数字转换为字符串

转载 作者:行者123 更新时间:2023-11-29 09:30:04 26 4
gpt4 key购买 nike

我如何为输入 100 到 999 之间的数字创建返回值。

 public class numbers {
static String[] ones={" ","one","two","three","four","five","six","seven","eight","nine","ten",
"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
static String [] tens={" ","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
static String [] hundreds={" ","one-hundred","two-hundred","three-hundred","four-hundred",
"fife-hundred","six-hundred","sevent-hundred","eight-hundred","nine-hundred"};
static String[] thousand= {"Thousand"};

public static String UpTONineteen(int i){
Scanner t=new Scanner(System.in);
System.out.print("please enter a number\n");
i=t.nextInt();
while(i>0 && i<20){
return ones[i];
}
while(i>=20 && i<100){
return tens[i/10]+ones[i%10];
}while(i>=100 && i<1000){

}

return "";

}

public static void main(String []args){
numbers b=new numbers();
System.out.print(b.UpTONineteen(10));

}

}

最佳答案

首先:你的 tens 数组有问题,它需要像这样因为你使用了索引

static String[] tens = {" ", " ", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};

第二个:百范围

return hundreds[i / 100] +" "+ tens[(i % 100) / 10] +" "+ ones[i % 10];

关于java程序将数字转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21577377/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com