標題:
多載函式
[打印本頁]
作者:
tonyh
時間:
2013-2-16 16:09
標題:
多載函式
本帖最後由 tonyh 於 2013-2-16 16:43 編輯
多載函式的定義:
相同的函式名稱,卻擁有不同功能運算。
條件是引入參數的數量不同或是型態不同。
練習:
輸入五個數字 ,分別利用四個相同名稱的函式來計算
1. 前兩個數相加
2. 前三個數相加
3. 前四個數相加
4. 五個數相加
import java.util.Scanner;
public class ch57
{
public static void main(String args[])
{
int a, b, c, d, e;
Scanner s=new Scanner(System.in);
System.out.print("請依序任意輸入5個數: ");
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();
d=s.nextInt();
e=s.nextInt();
System.out.println("前兩個數相加: "+total(a,b));
System.out.println("前三個數相加: "+total(a,b,c));
System.out.println("前四個數相加: "+total(a,b,c,d));
System.out.println("五個數相加: "+total(a,b,c,d,e));
}
public static int total(int a, int b)
{
return a+b;
}
public static int total(int a, int b, int c)
{
return a+b+c;
}
public static int total(int a, int b, int c, int d)
{
return a+b+c+d;
}
public static int total(int a, int b, int c, int d, int e)
{
return a+b+c+d+e;
}
}
複製代碼
作者:
t3742238
時間:
2013-2-16 16:38
import java.util.Scanner;
public class ch57
{
public static void main(String args[])
{
int a,b,c,e,d;
Scanner s=new Scanner(System.in);
System.out.print("請依序任意輸入5個數:");
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();
d=s.nextInt();
e=s.nextInt();
System.out.println("前兩個相加"+total(a,b));
System.out.println("前三個相加"+total(a,b,c));
System.out.println("前四個相加"+total(a,b,c,d));
System.out.println("五個相加"+total(a,b,c,d,e));
}
public static int total(int a,int b)
{
return a+b;
}
public static int total(int a,int b,int c)
{
return a+b+c;
}
public static int total(int a,int b, int c,int d)
{
return a+b+c+d;
}
public static int total(int a,int b,int c,int d,int e)
{
return a+b+c+d+e;
}
}
複製代碼
作者:
lon
時間:
2013-2-16 16:39
import java.util.Scanner;
public class ch57{
public static void main(String args[]){
int a,b,c,d,e;
Scanner s=new Scanner(System.in);
System.out.println("請依序輸入5個數字");
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();
d=s.nextInt();
e=s.nextInt();
System.out.println("前兩個數相加:"+total (a,b));
System.out.println("前三個數相加:"+total (a,b,c));
System.out.println("前四個數相加:"+total (a,b,c,d));
System.out.println("五個數相加:"+total (a,b,c,d,e));
}
public static int total(int a,int b){
return a+b;
}
public static int total(int a,int b,int c){
return a+b+c;
}
public static int total(int a,int b,int c,int d){
return a+b+c+d;
}
public static int total(int a,int b,int c,int d,int e){
return a+b+c+d+e;
}
}
複製代碼
作者:
劉漢文
時間:
2013-2-16 16:39
import java.util.Scanner;
public class ch57
{
public static void main(String args[])
{
int a,b,c,e,d;
Scanner s=new Scanner(System.in);
System.out.print("請依序任意輸入5個數:");
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();
d=s.nextInt();
e=s.nextInt();
System.out.println("前兩個相加"+total(a,b));
System.out.println("前三個相加"+total(a,b,c));
System.out.println("前四個相加"+total(a,b,c,d));
System.out.println("五個相加"+total(a,b,c,d,e));
}
public static int total(int a,int b)
{
return a+b;
}
public static int total(int a,int b,int c)
{
return a+b+c;
}
public static int total(int a,int b, int c,int d)
{
return a+b+c+d;
}
public static int total(int a,int b,int c,int d,int e)
{
return a+b+c+d+e;
}
}
複製代碼
作者:
t2364705
時間:
2013-2-16 16:39
import java.util.Scanner;
public class ch57
{
public static void main(String args[])
{
int a, b, c, d, e;
Scanner s=new Scanner(System.in);
System.out.print("請依序任意輸入五個數: ");
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();
d=s.nextInt();
e=s.nextInt();
System.out.println("前兩數相加: "+total(a,b));
System.out.println("前三數相加: "+total(a,b,c));
System.out.println("前四數相加: "+total(a,b,c,d));
System.out.println("五數相加: "+total(a,b,c,d,e));
}
public static int total(int a, int b)
{
return a+b;
}
public static int total(int a, int b, int c)
{
return a+b+c;
}
public static int total(int a, int b, int c, int d)
{
return a+b+c+d;
}
public static int total(int a, int b, int c, int d, int e)
{
return a+b+c+d+e;
}
}
複製代碼
作者:
蔡昀佑
時間:
2013-2-16 16:42
import java.util.Scanner;
public class ch57
{
public static void main(String args[])
{
int a,b,c,e,d;
Scanner s=new Scanner(System.in);
System.out.print("請依序任意輸入5個數:");
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();
d=s.nextInt();
e=s.nextInt();
System.out.println("前兩個相加"+total(a,b));
System.out.println("前三個相加"+total(a,b,c));
System.out.println("前四個相加"+total(a,b,c,d));
System.out.println("五個相加"+total(a,b,c,d,e));
}
public static int total(int a,int b)
{
return a+b;
}
public static int total(int a,int b,int c)
{
return a+b+c;
}
public static int total(int a,int b, int c,int d)
{
return a+b+c+d;
}
public static int total(int a,int b,int c,int d,int e)
{
return a+b+c+d+e;
}
}
複製代碼
作者:
黃博鴻
時間:
2013-2-16 16:43
import java.util.Scanner;
public class ch57
{
public static void main(String args[])
{
int a,b,c,d,e;
Scanner s=new Scanner(System.in);
System.out.println("請依序任意輸入5個數: ");
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();
d=s.nextInt();
e=s.nextInt();
System.out.println("前2個數相加:"+total(a,b));
System.out.println("前3個數相加:"+total(a,b,c));
System.out.println("前4個數相加:"+total(a,b,c,d));
System.out.println("5個數相加:"+total(a,b,c,d,e));
}
public static int total(int a, int b)
{
return a+b;
}
public static int total(int a, int b, int c)
{
return a+b+c;
}
public static int total(int a, int b, int c, int d)
{
return a+b+c+d;
}
public static int total(int a, int b, int c, int d, int e)
{
return a+b+c+d+e;
}
}
複製代碼
作者:
尤泓鈞
時間:
2013-2-16 16:44
import java.util.Scanner;
public class ch57
{
public static void main(String args[])
{
int a,b,c,e,d;
Scanner s=new Scanner(System.in);
System.out.print("請依序任意輸入5個數:");
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();
d=s.nextInt();
e=s.nextInt();
System.out.println("前兩個相加"+total(a,b));
System.out.println("前三個相加"+total(a,b,c));
System.out.println("前四個相加"+total(a,b,c,d));
System.out.println("五個相加"+total(a,b,c,d,e));
}
public static int total(int a,int b)
{
return a+b;
}
public static int total(int a,int b,int c)
{
return a+b+c;
}
public static int total(int a,int b, int c,int d)
{
return a+b+c+d;
}
public static int total(int a,int b,int c,int d,int e)
{
return a+b+c+d+e;
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2