素因子JAVA程序未打印出“的写入编号”;“设置系数”;
在代码的最底层,我有一个System.out.print打印错误的数字集。在输出端,它说它是13个数字的分解,但正如你所看到的,它比13个数字多得多 这是我的密码:素因子JAVA程序未打印出“的写入编号”;“设置系数”;,java,computer-science,Java,Computer Science,在代码的最底层,我有一个System.out.print打印错误的数字集。在输出端,它说它是13个数字的分解,但正如你所看到的,它比13个数字多得多 这是我的密码: import java.io.*; import java.util.Scanner; public class Project4 { public static void main(String[] args) throws FileNotFoundException { Scanner i
import java.io.*;
import java.util.Scanner;
public class Project4
{
public static void main(String[] args)
throws FileNotFoundException
{
Scanner in = new Scanner(new File("data4.txt"));
System.out.println("Tanner Triggs CM111 Lecture Project #4 Due Friday Oct 18");
int ctr= 0;
int x = in.nextInt();
while (x != -999)
{
int y = x;
int z = 2;
System.out.printf("\r\n For the number %9d the prime factors are",x);
ctr++;
while (z< y)
{
int remainder = x%z;
if (remainder == 0)
{
System.out.print(","+z);
x= x/z;
}
else
{
z++;
}
}
if (x==y)
{
System.out.print(" "+x);
}
x = in.nextInt();
}
System.out.printf("\n\n %9d Sets of numbers factored",ctr);
}
}
对于从文件中读取的每一行,您都会增加
ctr
,因此最终得到正确的13。如果要计算处理了多少素数因子,则应将递增移动到内部while
循环中。数字正确。txt文件包含14个数字,计数器从0开始。
459
120
340
379217
56
457
315
548153764
1001
240652872
123456789
32159
456
-999