初学者,问题或许很傻1 public class wanshu
2 {public static void main(String args[])
3 {int a,b,c;
4 for(a=2;a<=10000;a++)
5 {c=0;
6 for(b=1;b<a;b++)
7 {if(a%b==0)
8 c+=b;
9 }
10 if(c==a)
11 {System.out.println(a);
12 }
13 }
14 }
15 }
如上:是一个求完数的程序,可以正常编译输出结果。
完数:一个数如果恰好等于除它本身外的因子之和,这个数就称为"完数"。例如6=1+2+3.(6的因子是1,2,3)
下面的为什么编译通过,而无法输出结果?
1 public class wanshu
2 {public static void main(String args[])
3 {int a,b,c=0;
4 for(a=2;a<=10000;a++)
5 {
6 for(b=1;b<a;b++)
7 {if(a%b==0)
8 c+=b;
9 }
10 if(c==a)
11 {System.out.println(a);
12 }
13 }
14 }
15 }
2 {public static void main(String args[])
3 {int a,b,c;
4 for(a=2;a<=10000;a++)
5 {c=0;
6 for(b=1;b<a;b++)
7 {if(a%b==0)
8 c+=b;
9 }
10 if(c==a)
11 {System.out.println(a);
12 }
13 }
14 }
15 }
如上:是一个求完数的程序,可以正常编译输出结果。
完数:一个数如果恰好等于除它本身外的因子之和,这个数就称为"完数"。例如6=1+2+3.(6的因子是1,2,3)
下面的为什么编译通过,而无法输出结果?
1 public class wanshu
2 {public static void main(String args[])
3 {int a,b,c=0;
4 for(a=2;a<=10000;a++)
5 {
6 for(b=1;b<a;b++)
7 {if(a%b==0)
8 c+=b;
9 }
10 if(c==a)
11 {System.out.println(a);
12 }
13 }
14 }
15 }
The most reason is that "c" is a global variable.
3 {int a,b,c;
4 for(a=2;a <=10000;a++)
5 {c=0;
和
1 public class wanshu
2 {public static void main(String args[])
3 {int a,b,c=0;
是不同的作用域吧 LZ你在仔細想想。