|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
错误代码:编译错误
Main.java:24: illegal start of type
List<Integer> factors = new ArrayList<>();
^
1 error
题目要求:编写程序,读入一个整数,然后以升序显示它的所有最小因子,并对这些因子进行求和输出。(10分)
题目内容:
编写程序,读入一个整数,然后以升序显示它的所有最小因子,并对这些因子进行求和输出。
输入格式:
整数,number为输入的整数, 数组factor存储因子
输出格式:
System.out.print(number + "'s factor series:");
System.out.print(factor[i] + ", ");
System.out.println("\nthe sum of factors is " + sum);
输入样例:
100
输出样例:
100's factor series:2, 2, 5, 5,
the sum of factors is 14
程序代码:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
List<Integer> factors = findFactors(number);
System.out.print(number + "'s factor series:");
for (int i = 0; i < factors.size(); i++) {
System.out.print(factors.get(i));
if (i != factors.size() - 1) {
System.out.print(", ");
}
}
int sum = calculateSum(factors);
System.out.println("\nthe sum of factors is " + sum);
}
private static List<Integer> findFactors(int number) {
List<Integer> factors = new ArrayList<>();
for (int i = 2; i <= number; i++) {
while (number % i == 0) {
factors.add(i);
number /= i;
}
}
return factors;
}
private static int calculateSum(List<Integer> factors) {
int sum = 0;
for (int factor : factors) {
sum += factor;
}
return sum;
}
}
程序没有什么问题,看样子是不是在那一行输入了中文字符什么的,把那一行删除了,重新输入
|
|