这个程序有什么问题。如果一个数字是完美的,例如 num 是 6,它应该打印 1*2*3。使用大整数
What is wrong with this program. if a number is perfect for example num is 6 it should print 1*2*3. used BigInteger
package perfect;
import java.math.BigInteger;
import java.util.Scanner;
public class Perfect {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the number");
BigInteger n = input.nextBigInteger();
BigInteger sum = BigInteger.valueOf(0);
BigInteger i = BigInteger.valueOf(1);
BigInteger mul = BigInteger.valueOf(1);
for(; i.compareTo(n)< 0; i.add(BigInteger.ONE))
{
if(n.mod(i).equals(BigInteger.ZERO))
{
sum = sum.add(i);
mul = mul.multiply(i) ;
}
}
if(sum == n)
{
System.out.println(n+ "=" +mul) ;
}
else
{
System.out.println("the given number " +n+ " is not a perfect
number");
}
}
}
因为它必须打印 6 = 1*2*3 我使用了 BigInteger。但它没有显示任何错误,但是程序在控制台中从用户那里获取一个数字后我没有得到任何输出。
三个问题:
BigInteger
是不可变的,所以你应该 i = i.add(BigInteger.ONE)
而不是
- 比较
sum
和n
时,你应该sum.equals(n)
而不是
- 将因子存储到列表中,而不是将它们累加返回输入
- 如果格式化代码会更好看
import java.math.BigInteger;
import java.util.Scanner;
import java.util.ArrayList;
public class Perfect {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the number");
BigInteger n = input.nextBigInteger();
BigInteger sum = BigInteger.valueOf(0);
BigInteger i = BigInteger.valueOf(1);
ArrayList<BigInteger> factors = new ArrayList<BigInteger>();
for (; i.compareTo(n) < 0; i = i.add(BigInteger.ONE)) {
if (n.mod(i).equals(BigInteger.ZERO)) {
sum = sum.add(i);
factors.add(i);
}
}
if (sum.equals(n)) {
System.out.print(n + "=" + factors.get(0));
for (int idx = 1; idx < factors.size(); idx++) {
System.out.print("*" + factors.get(idx));
}
System.out.println();
} else {
System.out.println("the given number " + n + " is not a perfect number");
}
}
}
package perfect;
import java.math.BigInteger;
import java.util.Scanner;
public class Perfect {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the number");
BigInteger n = input.nextBigInteger();
BigInteger sum = BigInteger.valueOf(0);
BigInteger i = BigInteger.valueOf(1);
BigInteger mul = BigInteger.valueOf(1);
for(; i.compareTo(n)< 0; i.add(BigInteger.ONE))
{
if(n.mod(i).equals(BigInteger.ZERO))
{
sum = sum.add(i);
mul = mul.multiply(i) ;
}
}
if(sum == n)
{
System.out.println(n+ "=" +mul) ;
}
else
{
System.out.println("the given number " +n+ " is not a perfect
number");
}
}
}
因为它必须打印 6 = 1*2*3 我使用了 BigInteger。但它没有显示任何错误,但是程序在控制台中从用户那里获取一个数字后我没有得到任何输出。
三个问题:
BigInteger
是不可变的,所以你应该i = i.add(BigInteger.ONE)
而不是- 比较
sum
和n
时,你应该sum.equals(n)
而不是 - 将因子存储到列表中,而不是将它们累加返回输入
- 如果格式化代码会更好看
import java.math.BigInteger;
import java.util.Scanner;
import java.util.ArrayList;
public class Perfect {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the number");
BigInteger n = input.nextBigInteger();
BigInteger sum = BigInteger.valueOf(0);
BigInteger i = BigInteger.valueOf(1);
ArrayList<BigInteger> factors = new ArrayList<BigInteger>();
for (; i.compareTo(n) < 0; i = i.add(BigInteger.ONE)) {
if (n.mod(i).equals(BigInteger.ZERO)) {
sum = sum.add(i);
factors.add(i);
}
}
if (sum.equals(n)) {
System.out.print(n + "=" + factors.get(0));
for (int idx = 1; idx < factors.size(); idx++) {
System.out.print("*" + factors.get(idx));
}
System.out.println();
} else {
System.out.println("the given number " + n + " is not a perfect number");
}
}
}