public class PerfectNumber{
成都创新互联是一家企业级云计算解决方案提供商,超15年IDC数据中心运营经验。主营GPU显卡服务器,站群服务器,成都移动服务器托管,海外高防服务器,大带宽服务器,动态拨号VPS,海外云手机,海外云服务器,海外服务器租用托管等。
private static String[] isPrefectnumber(int n){
int sum=0;
StringBuffer sBuffer = new StringBuffer();;
for (int i = 1; i n; i++) {
if (n % i == 0){
sum += i;
sBuffer.append(","+i);
}
}
if (sum==n) {
return sBuffer.toString().replaceFirst(",", "").split(",");
}else {
return new String[0];
}
}
public static void main(String[] args){
String[] al= isPrefectnumber(t);
if (al.length!=0) {
StringBuffer sb = new StringBuffer();
sb.append(t+"是完数,其因子为");
for (int i = 0; i al.length ; i++) {
sb.append(","+al[i]);
}
System.out.println(sb.toString());
} else {
System.out.println(t+"不是完数");
}
}
}
代码如下,希望能帮到你~!
import java.util.Scanner;
public class PerfectNumber {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
String str;
long num;
System.out.println("请输入一个整数,输入end退出程序。");
while (true) {
str = in.nextLine();
if (str.equals("end"))
break;
try {
num = Long.parseLong(str);
} catch (Exception e) {
System.out.println("输入错误,请重新输入!");
continue;
}
System.out.println(num + (perfectNum(num) ? "是完全数" : "不是完全数"));
}
}
/**
* 判断一个数是否是完全数的方法
*
* @param num
* @return
*/
public static boolean perfectNum(long num) {
int sum = 0;
for (int i = 1; i = num / 2; i++) {
if (num % i == 0) {
sum += i;
// System.out.println(i);
}
}
return sum == num;
}
}
import java.util.Scanner;
public class Test1 {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
if(isFullNum(scanner.nextInt())) {
System.out.println("是完数");
}
else {
System.out.println("不是完数");
}
}
//判断一个数字是不是完数,是的话返回true
public static boolean isFullNum(int num) {
int sum = 0;
for(int i = 1;i = num/2;i++) {
if(num%i == 0)
sum+=i;
}
return sum == num;
}
}
这个就是正确的啊,注意你书写的习惯,因为以后工作了你的代码不是一个人看的,还有人要帮你改,所以早点养成好的习惯比较好。
改了,你在看看
import java.util.Scanner;
public class Test5 {
public static void main(String[] args) {
System.out.println("请输入数字:");
String s=new String();
Scanner c=new Scanner(System.in);
s=c.next();
int a = 0;
try {
a = Integer.parseInt(s);
int sum = 0;
for(int i=1;i=a/2;i++){
if(a%i == 0) {
sum += i;
}
}
if(a==sum) {
System.out.print("a+是完全数");
} else {
System.out.print("a+不是完全数");
}
} catch(NumberFormatException e) {
e.printStackTrace();
}
}
}
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int i = input.nextInt();
int k;
int j = 1, sum = 0; // 而应在该处为j赋初值1;
while (j = i / 2) {
k = i % j;
if (k == 0)
sum += j;
j++;
}
if (sum == i){
System.out.println(i+"是完全数");
}else {
System.out.println(i+"不是完全数");
}
}
}
import java.io.*;
public class Demo2
{
public static void main(String []args)
{
try{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.print("请输入一个数:");
String a=br.readLine();
int num=Integer.parseInt(a);
int sum=0;
for(int j=1;j=num/2;j++)
if(num%j==0)
sum=sum+j;
if(sum==num)
System.out.println(num+"是完全数");
else
System.out.println(num+"不是完全数");
}catch(Exception e)
{
e.printStackTrace();
}
}
};