Java 如何单独打印除数??

Java 如何单独打印除数??,java,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

我想知道一个给定的实数有多少个除数,并想打印这些除数。但我得到一个错误: 输入一个正整数: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
}
公共类计数器除数{
公共静态void main(字符串[]args){
int N;//用户输入的正整数。
//将计算此数字的除数。
int testDivisor;//介于1和N之间的数字,它是一个
//N的可能除数。
int divisorCount;//已找到的N的除数的数目。
int numberTested;//用于计算可能的除数
int阳性对照;
//已经测试了N的数量。当数量
//达到1000000,则输出一个周期并
//numberTested的值被重置为零。
/*从用户处获取正整数*/
while(true){
System.out.print(“输入正整数:”);
N=TextIO.getlnInt();
如果(N>0)
打破
System.out.println(“该数字不是正数,请重试”);
}
/*计算除数,每1000000次测试后打印一个“.”*/
除数计数=0;
numberTested=0;
阳性率=0;

对于(testDivisor=1;testDivisor我在您的代码中发现了一些问题,首先我添加了一个
扫描器
,因为我没有您的
TextIO
类。接下来,我添加了一个
列表
来存储除数(因为您只保留了一个计数)。因此,它应该看起来像-

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()

“for”循环多次打印语句“N的除数为”。 尝试将所有除数存储在ArrayList中,然后在for循环之后放入另一个“for”循环以打印这些除数

.
.
.
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);
}
。
.
.
ArrayList除数_数组=新的ArrayList();

对于(testDivisor=1;testDivisor首先,你应该缩进你的代码。看起来还是个家庭作业……你的末尾有三个大括号——第一个是
end main
,第二个是
end class countDivisor
,第三个是-,那是什么结尾?这应该是你的大括号不匹配的线索正确,特别是如果你必须在最后添加一个额外的来编译它。仔细检查。缩进(正如Eran建议的)这真的很有帮助。如果你使用的IDE可以为你做缩进,那就让它来吧。这不是家庭作业。我正在学习java,对如何为自己做这件事很好奇。我使用的是eclipse。它是缩进的。程序运行良好,并说“x的除数是y”.但我想打印这些数字。显示它们。我需要一个数组还是更好的主意?我应该输入什么?一个数字?我如何知道边界?如果我使用你的行,eclipse会告诉我在这里“插入”维度”???我在问@truffleme,因为您已经绕过了TextIO类问题……请回答VM我刚刚使用了默认的eclipse修复程序,并从java.util导入了ArrayList包。工作非常完美,这正是我想要的。我正在继续学习,希望有一天我会像truffleme和在座的其他人一样聪明,并且非常精通在爪哇。感谢一个磨坊的伙计们。没想到这么快就解决了问题,学到了一些新东西。会回来获取更多。我如何解决这个问题,因为truffleme已经回答了这个问题??
.
.
.
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);
}