public static void main(String[] args)
{
Scanner x=new Scanner(System.in);
int n=0;int g=0; int term=0;int temp=0;
int sum=0; int factor=1;
System.out.print("Input N:");
n=x.nextInt();
g=n;
if (n<=0)
{
System.out.println("Please enter a positive integer");
System.exit(0);
}
if (n>0)
{
System.out.print("The factors are:");
while (factor<n)
{
if (n%factor==0)
{
System.out.print(factor+",");
}
factor++;
}
}
}
如果我输入数字8,则因子是1,2和4.我想要实现的是添加因子8,即1,2和4,这将导致7。
答案 0 :(得分:0)
import java.util.Scanner;
public class Demo {
public static void main(String[] args)
{
Scanner x=new Scanner(System.in);
int n=0;int g=0; int term=0;int temp=0;
int sum=0; int factor=1;
System.out.print("Input N:");
n=x.nextInt();
g=n;
int number = 0;
if (n<=0)
{
System.out.println("Please enter a positive integer");
System.exit(0);
}
if (n>0)
{
System.out.print("The factors are:");
while (factor<n)
{
if (n%factor==0)
{
System.out.println(factor+",");
number+=factor;
}
factor++;
}
}
System.out.println("Sum = "+number);
}
}
答案 1 :(得分:0)
import static java.lang.System.*;
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args)
{
Scanner kb = new Scanner(in);
out.print("Enter a number: ");
int num = kb.nextInt();
int sum = 0;
int x = 1;
for(x = 1; x <= num; x++){
if (num % x == 0){
sum = sum + x;
}
}
out.print(sum);
}
}