不可傳回直接顯示Sum
------------------------------------------
class test13
{
public static void main(String[] args)
{
Sum(1,100);
Sum(5,99);
}
static void Sum(int vStart,int vEnd)
{
int total=0;
for(int i=vStart;i
total+=i;
System.out.println(vStart+"加到"+vEnd+"的總和為"+total+"\n");
}
}
------------------------------------------
可傳回
------------------------------------------
class test14
{
public static void main(String[] args)
{
int tol1,tol2;
tol1=Sum(1,100);
System.out.println("1加到100總和為"+tol1+"\n");
tol2=Sum(5,99);
System.out.println("5加到99總和為"+tol2+"\n");
}
static int Sum(int vStart,int vEnd)
{
int total=0;
for(int i=vStart;i
total+=i;
return total;
}
}
------------------------------------------
- Apr 20 Fri 2007 07:30
<Java-基礎篇>方法01
close
全站熱搜
留言列表
發表留言