我的下面的代碼應該接受來自用戶的整數,然後打印他們輸入的任何整數以相反的順序。我沒有得到任何錯誤,但是當我運行這個程序時,整數不會被反向打印。有人能幫我弄清楚爲什麼?Collections.reverse方法不倒轉條目
import java.util.*;
public class ReverseDigits {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String response;
System.out.println("Please enter a whole number.");
response = sc.next();
reverseDigit(response);
}
public static void reverseDigit(String digit) {
ArrayList al = new ArrayList();
al.add(digit);
Collections.reverse(al);
System.out.println("Your number in reverse order is: " + al);
}
}
非常感謝!我將更多地瞭解ArrayLists,並且您的鏈接解決了我的問題。 – carchelhf