在 Java 中分解一个数字 - 作业

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19507530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:51:10  来源:igfitidea点击:

Factoring a number in Java - Homework

java

提问by Jordan.McBride

As several other questions have asked on this forum, how would you go about finding the factors of a number.

正如在此论坛上提出的其他几个问题一样,您将如何找到一个数字的因数。

Note: I have tried to understand what other people have written, please do not rehash already given answers

注意:我已经尝试了解其他人写的内容,请不要重复已经给出的答案

My code works as of right now but, for example the number 6 returns: 6.0 factored by 1.0 is 6.000 6.0 factored by 2.0 is 3.000 6.0 factored by 3.0 is 2.000 6.0 factored by 4.0 is 1.500 6.0 factored by 5.0 is 1.200 6.0 factored by 6.0 is 1.000

我的代码现在有效,但是,例如数字 6 返回: 6.0 因数 1.0 为 6.000 6.0 因数为 2.0 为 3.000 6.0 因数为 3.0 为 2.000 6.0 因数为 4.0 为 1.500 6.0 因数为 10 .0 因数为 .6 .0 因数为 .0。是 1.000

Is there a way to remove all the decimal numbers? I thought somehting about a "for" statement, but I don't really know how to use them.

有没有办法删除所有的十进制数?我想过一些“for”语句,但我真的不知道如何使用它们。

Current Code:

当前代码:

public class Factoring {

public static void main(String[] args) {
    double input;
    double factor = 1;
    double factorNum;

    System.out.println("This application calculates every factor of a given number!"
            + "\nPlease enter a number: ");
    input = TextIO.getlnDouble();

    System.out.println("Original Number: " + input);

    while (input >= factor){
        factorNum = input / factor;

        System.out.print("\n" + input + " factored by " + factor + " is ");
        System.out.printf("%.3f", factorNum);
        factor++;
    }
}

}

采纳答案by Little Child

use an intin place of double. When you use doubleor floatyou get the decimals.

使用int代替double。当你使用doubleorfloat你得到小数。

So your this:

所以你的这个:

    double input;
    double factor = 1;
    double factorNum;  

Should be:

应该:

    int input;
    int factor = 1;
    int factorNum;  

SSCCE

南昌

public class Factoring {
    public static void main(String[] args) {
        int number = 15;
        int factorNumber = 1;

        while(factorNumber <= number){
            if(number % factorNumber == 0){
                System.out.println(factorNumber + " is a factor of " + number);
            }
            factorNumber ++;
        }
    }
}  

Result:

结果:

1 is a factor of 15
3 is a factor of 15
5 is a factor of 15
15 is a factor of 15  

Explanation

解释

Consider dividing the number 15 by 2 just like you would in Math class.

考虑将数字 15 除以 2,就像在数学课上一样。

15 = 2 * 7.5 + 0. Your quotient is: 7.5
In programming 7.5 and 7 are different. One is a floating pointnumber and the other is an integeror whole numberas we call them.

15 = 2 * 7.5 + 0。你的商是: 7.5
在编程中 7.5 和 7 是不同的。一个是浮点数字,另一个是整数整数,我们给他们打电话。

To store floating point numbers, floator doubledatatypes are used. They preserve the numbers after the decimal places.
intand longon the other hand are for whole numbers and hence they do not keep the numbers after the decimal places. They do not round them up; they truncate it.

为了存储浮点数,使用floatdouble数据类型。他们保留小数点后的数字。另一方面,
intlong用于整数,因此它们不保留小数位后的数字。他们没有把他们围起来;他们截断它。

Try this:

尝试这个:

double withDecimals = 7.5d;
int withoutDecimals = 7.5d;

and print them.
See the difference ?

并打印它们。
看到不同 ?

回答by Schickmeister

If the input number % by the factor is not equal to zero, then remove it from the list of factors. I'm assuming here that you're talking about the answers like "1.5", the ones that technically aren't factors of the input number.

如果因子的输入数 % 不等于零,则将其从因子列表中删除。我在这里假设您正在谈论诸如“1.5”之类的答案,这些答案在技术上不是输入数字的因数。

回答by Isuru Gunawardana

Use int and check for the remainder using %

使用 int 并使用 % 检查余数

int factor = 6;
int input = 1;

   while(input <= factor) {

       if(f % i == 0){

           System.out.println(i + " is a factor");

       }

   i++;

   }

回答by Putra

rather you use double for your number its better to change to integer..integer is for single number and double is for decimal number..

相反,您使用 double 作为您的数字,最好更改为整数..integer 用于单个数字,double 用于十进制数字..

int input;
    int factor = 1;
    int factorNum;

and change from

并从

System.out.printf("%.3f", factorNum);

to

System.out.printf("%.0f", factorNum);

回答by Zaid

I stored the factors in ArrayList

我将因子存储在 ArrayList 中

          int s = /* Your number or get from user */;
    ArrayList<Integer>integers = new ArrayList<>();
    for (int i = 1 ; i <= s ; i++){
        if(s%i == 0){
         integers.add(i);
           /* just for printing the factors */ System.out.println(i);
        }
    }