Java:如何在arraylist中写入计算数据?

标签 java arrays arraylist

大家好,我是 Java 的初学者,我在数组和数组列表方面遇到了一些问题。我的主要问题是如何将计算、动态数据写入数组,然后如何读取它?这是我的奇怪代码:

    public static void main(String[] args) {

    int yil, bolum = 0, kalan;
    Scanner klavye = new Scanner(System.in);
    ArrayList liste = new ArrayList();

    //or shall i use this? >> int[] liste = new int[10];

    System.out.println("Yıl Girin: "); // enter the 1453
    yil = klavye.nextInt();

    do{ // process makes 1453 separate then write in the array or arraylist. [1, 4, 5, 3]

    kalan = yil % 10;
    liste.add(kalan); //my problem starts in here. How can i add "kalan" into the "liste".
    bolum = yil / 10;
    yil = bolum;

    }while( bolum == 0 );

    System.out.println("Sayının Basamak Sayısı: " + liste.size()); //in here read the number of elements of the "liste" 
    klavye.close();
}

编辑:

    //needs to be like that
    while( bolum != 0 ); 
    System.out.println("Sayının Basamak Sayısı: " + liste);

最佳答案

我认为您最有可能希望循环停止条件是:

while( bolum != 0)

因为 bolum 只有在您的号码中没有更多数字需要处理时才为 0。此外,正如 Amit 上面提到的,当系统提示输入数字时,用户可能输入了 0,因此您应该考虑到这一点。

关于Java:如何在arraylist中写入计算数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13002289/

相关文章:

java - 二维迷宫的递归算法?

c - 将二维数组传递给c中的函数

c - 如何正确初始化/访问具有多个源的结构数组?

java - ArrayList 和文件传输的问题

java - OpenCL 脚本给出意想不到的结果

java - FTP 使用服务器中的文件和目录填充 jtree

Java 和 SQLite [SQLITE_ERROR] SQL 错误或缺少数据库 ()

c++ - 将 vector 放在数组元素中

java - 为什么 LinkedList 在添加到列表末尾时比 ArrayList 慢?

java - 使用 toString 和增强型 for 循环显示 ArrayList 信息的更好方法