Topic: “意外的类型” 错误--Java |
Print this page |
1.“意外的类型” 错误--Java | Copy to clipboard |
Posted by: Googlefeifei Posted on: 2007-04-18 00:59 public class c_sum { static int fac(int n) { int sum=0; if(n>=0) { fac=n; } sum=n+fac(n-1); return sum; } public static void main(String[] args) { int n=50; int f1=fac(50); System.out.println("50+49+....+2+1"+f1); } } 出现这样的错误!! -------------------Configuration: <Default>-------------------- E:\Java\Java作业2\c_sum.java:9: 意外的类型 需要: 变量 找到: 值 fac=n; ^ 1 错误 Process completed. 请各位给我看看这里有什么问题,小弟真想不明白!◎! |
2.Re:“意外的类型” 错误--Java [Re: Googlefeifei] | Copy to clipboard |
Posted by: xuxiaolei Posted on: 2007-04-18 07:38 static int sum(int n) { if(n == 1) { return 1; } else { return (n + sum(n - 1)); } } |
3.Re:“意外的类型” 错误--Java [Re: Googlefeifei] | Copy to clipboard |
Posted by: Googlefeifei Posted on: 2007-04-18 22:36 嗯嗯,小弟明白了,谢谢喔!! |
4.Re:“意外的类型” 错误--Java [Re: Googlefeifei] | Copy to clipboard |
Posted by: JiafanZhou Posted on: 2007-04-19 17:44 Recursive method...uhr?? |
5.Re:“意外的类型” 错误--Java [Re: Googlefeifei] | Copy to clipboard |
Posted by: xuxiaolei Posted on: 2007-04-20 07:37 是递归方法,这样效率是有点低,但是按照楼主写的方法,应该是用递归求的,不过最好用循环,这样效率会高些 |
6.Re:“意外的类型” 错误--Java [Re: Googlefeifei] | Copy to clipboard |
Posted by: lililixiong Posted on: 2007-04-20 08:30 public class c_sum { static int fac(int n) { int sum=0; if(n>=0) { fac=n; } sum=n+fac(n-1); 把括号移动这个后面就行了 |
7.Re:“意外的类型” 错误--Java [Re: Googlefeifei] | Copy to clipboard |
Posted by: lililixiong Posted on: 2007-04-20 08:32 public class c_sum { static int fac(int n) { int sum=0; if(n>=0) { fac=n; sum=n+fac(n-1);} 这样就能运行了 |
8.Re:“意外的类型” 错误--Java [Re: Googlefeifei] | Copy to clipboard |
Posted by: aaarong Posted on: 2007-04-20 08:52 public class c_sum { static int fac(int n) { int sum=0; if(n>=0) { sum=n+fac(n-1); } return sum; } public static void main(String[] args) { int n=50;int sum=0; int f1=fac(50); System.out.println("50+49+....+2+1="+f1); for(int i=1;i<=50;i++) //循环效率会快点 sum+=i; System.out.println(sum); } } |
9.Re:“意外的类型” 错误--Java [Re: Googlefeifei] | Copy to clipboard |
Posted by: JiafanZhou Posted on: 2007-04-22 06:27 still not good enough.... thinking about this (50 + 1) * 25 only takes 2 operations instead of 50 times of addition, cool?? Regards, Jiafan |
10.Re:“意外的类型” 错误--Java [Re: Googlefeifei] | Copy to clipboard |
Posted by: Googlefeifei Posted on: 2007-04-27 00:04 cool!!! (50+1)*25 哈哈。 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |