所以这里有一个简单的代码,用输入的数字来调整正确的“st”,“nd”,“rd”,“th”。 由于某种原因,它被放置在一个循环中。不要管那个。有条件的打印语句不打印其余部分。 Java
System.out.println("How many?");
int num = x.nextInt();
for(int i=1;i<=num;i++){
System.out.print("Enter the " + i);
System.out.println(i==1? ("st"):(i==2? "nd":i==3? "rd":"th") + " number!");
}
当num是输入作为5
这里的输出: “数”
Enter the 1st
Enter the 2nd number!
Enter the 3rd number!
Enter the 4th number!
Enter the 5th number!
的问题是在哪里与案例“第一”?
好感谢大家,我得到它。我认为把“(”st“):(i == 2?”nd“:i == 3?”rd“:”th“)'而不是'”st“:i == 2? “ND”:我== 3? “rd”:“th”会限制条件的边界。但显然它不这样工作......所以把整个条件放在括号内就可以了。谢谢大家:) –