所以這裏有一個簡單的代碼,用輸入的數字來調整正確的「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」會限制條件的邊界。但顯然它不這樣工作......所以把整個條件放在括號內就可以了。謝謝大家:) –