java - Java - 主要因素 - 提示修复?
问题描述
我想用主要因素填充列表。这里有什么问题?
public class factorClass {
private ArrayList<Integer> factors = new ArrayList<>();
// constructor populates array of factors for a given number
public factorClass(int factorThis) {
int test = 2;
while (factorThis > 1 ) {
if (factorThis % test == 0) { //factor found
factors.add(test); //add factor to array
factorThis = factorThis / test; //quotient is new number to factor
} else {
testFactor++;// try next integer
}
}
}
解决方案
有两点不对:
您首先测试因子 3,这意味着您永远不会找到 2 个因子。
您的循环条件 -
testFactor > 1
- 是错误的。testFactor
不断增长,所以循环只会在testFactor
溢出到负值时终止。您应该在变为 1 时终止NumberToFactor
。即将条件更改为NumberToFactor > 1
.
哦,还有另一个问题 -Factors
似乎是一个构造函数,但它出现在一个不同名称的类中 - factorClass
。构造函数必须与类同名。
推荐阅读
- html - 使用列换行扩大 flex div 的宽度以适应内容
- kubernetes - Knative 大使 Ingress
- php - 在 Linux 上从 PHP 连接到 MS Access 数据库
- javascript - 有没有办法为Oracle中的文件定义一个新目录
- sql - 完全外连接在右侧没有获得 NULL 值,而它们确实存在于左侧
- node.js - 如何在 npm gulp maven deploy 插件中传递用户名和密码
- php - 试图在laravel中获取非对象的属性“状态”
- powershell - 格式化来自 docker 事件的 json 输出
- javascript - 如何仅禁用上个月的日期
- android - 如何解决重复的 gradle 依赖项(jsr305)?