如何单独打印除数?

时间:2014-08-25 15:15:33

标签: java

我想找出给定实数有多少除数并想要打印这些除数。但是我得到错误: 输入正整数:25

25的除数是1 除数是1

25的除数是1 除数是1

25的除数是1 除数是1

25的除数是1 除数是1

25的除数是2 除数是2

25的除数是2 除数是2

25的除数是2 除数是2

25的除数是2 除数是2

25的除数是2 除数是2

25的除数是2 除数是2

25的除数是2 除数是2

25的除数是2 除数是2

25的除数是2 除数是2

25的除数是2 除数是2

25的除数是2 除数是2

25的除数是2 除数是2

25的除数是2 除数是2

25的除数是2 除数是2

25的除数是2 除数是2

25的除数是2 除数是2

25的除数是2 除数是2

25的除数是2 除数是2

25的除数是2 除数是2

25的除数是2 除数是2

25的除数是3 除数是3

一遍又一遍地打印。 程序工作正常,并告诉该数字有多少除数。但是当我试图打印出这些数字并显示它们时,问题。是否需要一个数组?有一个伟大的头脑那里帮助我解决这个问题? 不是家庭作业

这是我的源代码

public class CountDivisors {
public static void main(String[] args) {
int N; // A positive integer entered by the user.
// Divisors of this number will be counted.
int testDivisor; // A number between 1 and N that is a
// possible divisor of N.
int divisorCount; // Number of divisors of N that have been found.
int numberTested; // Used to count how many possible divisors
int positiveDivisor;
// of N have been tested. When the number
// reaches 1000000, a period is output and
// the value of numberTested is reset to zero.
/* Get a positive integer from the user. */
while (true) {
System.out.print("Enter a positive integer: ");
N = TextIO.getlnInt();
if (N > 0)
break;
System.out.println("That number is not positive. Please try again.");
}
/* Count the divisors, printing a "." after every 1000000 tests. */
divisorCount = 0;
numberTested = 0;
positiveDivisor=0;
for (testDivisor = 1; testDivisor <= N; testDivisor++) {

if ( N % testDivisor == 0 )
{
positiveDivisor++;
divisorCount++;
numberTested++;
if (numberTested == 1000000) {
System.out.println("Whoof!! Just tested a Million Numbers!!");
System.out.println("No probs.LETS GO AGAIN!!");
numberTested = 0;
}
}
/* Display the result. */
System.out.println();
System.out.println("The number of divisors of " + N
+ " is " + divisorCount);
System.out.println("And the divisors are" + positiveDivisor);
} // end main()
} // end class CountDivisors
}

2 个答案:

答案 0 :(得分:0)

我在您的代码中发现了一些问题,首先我添加了Scanner因为我没有TextIO课程。接下来,我添加了一个List来存储除数(因为你只保留了一个计数)。所以,它应该看起来像 -

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int testDivisor;
    int numberTested; // Used to count how many possible divisors
    int N = -1;
    while (true) {
        System.out.print("Enter a positive integer: ");
        N = scanner.nextInt();
        if (N > 0) {
            break;
        }
        System.out.println("That number is not positive. Please try again.");
    }
    /* Count the divisors, printing a "." after every 1000000 tests. */
    numberTested = 0;
    List<Integer> divisors = new ArrayList<Integer>();
    for (testDivisor = 1; testDivisor <= N; testDivisor++) {
        if (N % testDivisor == 0) {
            divisors.add(testDivisor);
            numberTested++;
            if (numberTested == 1000000) {
                System.out.println("Whoof!! Just tested a Million Numbers!!");
                System.out.println("No probs.LETS GO AGAIN!!");
                numberTested = 0;
            }
        }
        /* Display the result. */
        System.out.println();
        System.out.println("The number of divisors of " + N + " is "
                + divisors.size());
        System.out.println("And the divisors are" + divisors);
    }
} // end main()

当我用25测试时,我确实得到了(不包括中间步骤) -

The number of divisors of 25 is 3
And the divisors are[1, 5, 25]

答案 1 :(得分:0)

你的&#39; for&#39;循环是打印语句&#34; N的除数是&#34;多次。 尝试将所有除数存储在ArrayList中,并在for循环之后放置另一个除数&#39; for&#39;循环打印那些。

.
.
.
ArrayList<int> divisor_array = new ArrayList<int>();
for (testDivisor = 1; testDivisor <= N; testDivisor++) {

  if ( N % testDivisor == 0 ) {
    positiveDivisor++;
    divisorCount++;
    numberTested++;
    divisor_array.add(testDivisor);

    if (numberTested == 1000000) {
      System.out.println("Whoof!! Just tested a Million Numbers!!");
      System.out.println("No probs.LETS GO AGAIN!!");
      numberTested = 0;
    }
  }
} //end the for loop here

/* Display the result. */
System.out.println();
System.out.println("The number of divisors of " + N + " is " + divisorCount);

//print all divisors
System.out.println("And the divisors are:");
for(int k : divisor_array) {
   System.out.println(k);
}