java编程。完数,指的是所有的因子之和与其本身相等的数.现要求实现完整的程序
答案:2 悬赏:50 手机版
解决时间 2021-04-03 12:07
- 提问者网友:愿为果
- 2021-04-02 12:12
java编程。完数,指的是所有的因子之和与其本身相等的数.现要求实现完整的程序
最佳答案
- 五星知识达人网友:長槍戰八方
- 2021-04-02 12:17
public class PerfectNumber {
public static int factorsum(int num){
int sum=0;
for (int i = 1; i < num; i++) {
if (num % i == 0) {
sum = sum + i;
}
}
return sum;
}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("please input m = ");
int m=s.nextInt();
System.out.println("please input n = ");
int n=s.nextInt();
for (int a = m; a <= n; a++) {
if (a == factorsum(a)) {
System.out.println(a);
}
}
}
}
public static int factorsum(int num){
int sum=0;
for (int i = 1; i < num; i++) {
if (num % i == 0) {
sum = sum + i;
}
}
return sum;
}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("please input m = ");
int m=s.nextInt();
System.out.println("please input n = ");
int n=s.nextInt();
for (int a = m; a <= n; a++) {
if (a == factorsum(a)) {
System.out.println(a);
}
}
}
}
全部回答
- 1楼网友:轻雾山林
- 2021-04-02 12:54
import java.util.Scanner;
public class TestFullNumbers {
public static void main(String[] args) {
String[] numbers = getTwoNumbers();
int m = Integer.parseInt(numbers[0]);
int n = Integer.parseInt(numbers[1]);
for (int index = m; index < n; index++) {
if (index == factorSum(index)) {
System.out.println(index);
}
}
}
private static int factorSum(int num) {
int sum = 0;
for (int index = 1; index < num; index++) {
if (num % index == 0) {
sum += index;
}
}
return sum;
}
private static String[] getTwoNumbers() {
Scanner input = new Scanner(System.in);
return input.nextLine().split("\s+");
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯