我尝试打印输入号码的反面而没有任何for循环。但是我在打印Arraylist时遇到了一些问题。如何将Arraylist [3,5,4,1]打印为3541-不带括号,逗号和空格?
如果不可能,如何将ArrayList元素添加到字符串列表然后打印?
public static void main(String[] args) {
int yil, bolum = 0, kalan;
Scanner klavye = new Scanner(System.in);
ArrayList liste = new ArrayList();
//String listeStr = new String();
System.out.println("Yıl Girin: "); // enter the 1453
yil = klavye.nextInt();
do{ // process makes 1453 separate then write in the arraylist like that [3, 5, 4,1]
kalan = yil % 10;
liste.add(kalan);
bolum = yil / 10;
yil = bolum;
}while( bolum != 0 );
System.out.println("Sayının Tersi: " + ....); //reverse of the 1453
klavye.close();
}
最佳答案
public static void main(String[] args) {
int yil, bolum = 0, kalan;
ArrayList liste = new ArrayList();
System.out.println("Yıl Girin: "); // enter the 1453
yil = 1453;
String s="";
do { // process makes 1453 separate then write in the arraylist like that [3, 5, 4,1]
kalan = yil % 10;
liste.add(kalan);
s= s + kalan; // <------- THE SOLUTION AT HERE -------
bolum = yil / 10;
yil = bolum;
} while (bolum != 0);
System.out.println("Sayının Tersi: " + s ); //reverse of the 1453
}