gpt4 book ai didi

java - java 序数 - Java I 类

转载 作者:行者123 更新时间:2023-12-02 04:36:45 33 4
gpt4 key购买 nike

一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。

public class Ordinals {

public static String Ordinals(int i) {

System.out.println("Enter an integer between 0 to 10: ");
Scanner input = new Scanner(System.in);


int hundred = value % 100;
int tens = value % 10;
if (hundred - tens == 10) {
return "th";
}
switch (tens) {
case 1:
return "st";
case 2:
return "nd";
case 3:
return "rd";
default:
return "th";

}


public static void main(String[] args) {
Ordinals number = new Ordinals();
for (int i = 1; i <= 10; i++) {
String st = number.Ordinals(i);
System.out.println(i + " = " + i + st);
}
}
}

最佳答案

在我看来,最好将 Scanner 移至 main:

public static void main(String[] args) {
Ordinals number = new Ordinals();
Scanner input = new Scanner(System.in);
while(true){
System.out.println("Enter an integer between 0 to 10: ");
int i = input.nextInt();
System.out.println(i+number.Ordinals(i));
}
}

然后添加到序数:

int value = i;

方法将是独立的表单输入类型。

关于java - java 序数 - Java I 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30655138/

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