如何检查一个数字是否乘以某物,但会增加某物
因此,我一直在尝试制作某种计算器工具。功能之一是检查一个因子是否乘以某物,但同时又增加了其他因素。这有助于考虑三项官方。我找到了这些因素,但我不知道该如何进行。到目前为止,这就是
public static void MA() {
int sum = 0;
int product = 0;
ArrayList<Integer> Factors = new ArrayList<Integer>();
System.out.println("Enter the sum of the number");
sum = sc.nextInt();
System.out.println("Enter the number it should multiply to");
product = sc.nextInt();
if(product < 0){
for(int i = product; i <= Math.abs(product); ++i) {
// skips the iteration for i = 0
if(i == 0) {
continue;
}
else {
if (product % i == 0) {
Factors.add(i);
}
}
}
}
else{
for (int i = 1; i <= product; ++i) {
// if number is divided by i
// i is the factor
if (product % i == 0) {
Factors.add(i);
}
}
}
System.out.println(Factors);
}
我的程序架构是一个主要类别,它呼吁所有其他功能。
示例:x^2 + 9x + 20 =(x + 5)(x + 4)
4*5 = 20 4+5 = 9
So I've been trying to make some sort of a calculator tool. One of the function is to check if a factor of a number multiplies to something but at the same time adds to something else. This is helpful for factoring a trinomial. I found the factors but I don't know how to proceed. This is what I have so far
public static void MA() {
int sum = 0;
int product = 0;
ArrayList<Integer> Factors = new ArrayList<Integer>();
System.out.println("Enter the sum of the number");
sum = sc.nextInt();
System.out.println("Enter the number it should multiply to");
product = sc.nextInt();
if(product < 0){
for(int i = product; i <= Math.abs(product); ++i) {
// skips the iteration for i = 0
if(i == 0) {
continue;
}
else {
if (product % i == 0) {
Factors.add(i);
}
}
}
}
else{
for (int i = 1; i <= product; ++i) {
// if number is divided by i
// i is the factor
if (product % i == 0) {
Factors.add(i);
}
}
}
System.out.println(Factors);
}
Keep in mind the architecture of my program is a main class that calls on all other functions.
EXAMPLE: X^2 + 9x + 20 = (x+5)(x+4)
4*5 = 20
4+5 = 9
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题可以简洁地表示如下:
一个简单的解决方案是:
如果a和b不是null,则找到了
sum
的一对。Your problem can be succinctly expressed as follows:
A simple solution to which is:
If a and b are not null, a pair that summed to
sum
was found.