gpt4 book ai didi

java - 存储未定义数量的整数

转载 作者:行者123 更新时间:2023-12-01 23:12:08 25 4
gpt4 key购买 nike

我的任务是检查一个数字是否是质数,如果是,则显示它除以什么(例如 12 除以 1、2、3、4、6 和 12。)

import java.util.Scanner;
public class kt_4_1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number:");
num = scanner.nextInt();
int array[] = new int[num];
for (int count; count <= num; count++) {
if (num % count == 0) {
// need to store the numbers somehow, possibly in an array.
}
// Or, if the amount of numbers it can be divided by
//is 2 ( meaning 1 and itself ), then say the number is a prime.
}
}
}

最佳答案

存储未定义数量的数字的最简单方法是使用 ArrayList。 ArrayList 只是一个动态列表,可以增长和缩小以满足程序员的需要。

ArrayList<Integer> factors = new ArrayList<>();

Java Documentation ArrayList

关于java - 存储未定义数量的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21783857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com