標題:
[隨堂練習] 利用物件導向的概念 實作兩數計算
[打印本頁]
作者:
周政輝
時間:
2016-12-3 10:59
標題:
[隨堂練習] 利用物件導向的概念 實作兩數計算
運用先前講的建構元概念以及上禮拜交的函數
先在建構元當中將類別內的變數進行初始化
在自訂一個函數去實作兩數計算
Main:
package tw.kuas.mis;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Calc calc = new Calc(1,2,"+");
System.out.println(calc.Calcnum());
}
}
複製代碼
Calc:
package tw.kuas.mis;
public class Calc {
// 類別是由欄位以及方法所組成的
private static float num1;
private static float num2;
private static String oper;
private static float result;
//建構元主要在做初始化欄位的變數
public Calc(float num1,float num2,String oper)
{
this.num1 = num1;
this.num2 = num2;
this.oper = oper;
}
//使用函數去執行程式
public static float Calcnum()
{
switch(oper)
{
case "+":
result = num1+num2;
break;
case "-":
result = num1-num2;
break;
case "*":
result = num1*num2;
break;
case "/":
result = num1/num2;
break;
}
return result;
}
}
複製代碼
作者:
張健勳
時間:
2016-12-3 11:05
package site.istak.org.tw;
public class Main {
public static void main(String[] args) {
Calc calc = new Calc(1,2,"+");
System.out.println(Calc.Calcnum());
}
}
複製代碼
package site.istak.org.tw;
public class Calc {
private static float num1;
private static float num2;
private static String oper;
private static float total;
public Calc(float num1,float num2,String oper)
{
this.num1 = num1;
this.num2 = num2;
this.oper = oper;
}
public static float Calcnum ()
{
switch(oper)
{
case "+":
total = num1+num2;
break;
case "-":
total = num1-num2;
break;
case "*":
total = num1*num2;
break;
case "/":
total = num1/num2;
break;
}
return total;
}
}
複製代碼
作者:
蔡庭豪
時間:
2016-12-3 11:09
package site.istak.org.tw;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner scanner =new Scanner(System .in);
int x=scanner.nextInt();
int y=scanner.nextInt();
String c=scanner.next();
Calc calc = new Calc(x,y,c);
Calc.calc(x, y, c);
}
}
複製代碼
package site.istak.org.tw;
public class Calc {
private float num1;
private float num2;
private String oper;
public Calc(float x,float y,String c){
this.num1=x;
this.num2=y;
this.oper=c;
}
public static void calc(float x,float y,String c){
switch(c)
{
case"+":
System.out.println(x+y);
break;
case"-":
System.out.println(x-y);
break;
case"*":
System.out.println(x*y);
break;
case"/":
System.out.println(x/y);
break;
}
}
}
複製代碼
作者:
王彥甯
時間:
2016-12-3 11:11
package site.istak.org.tw;
import java.util.Scanner;
public class main {
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
String oper = scanner.next();
calc calc = new calc(num1 , num2 , oper);
System.out.println(calc.calcnum());
}
}
複製代碼
package site.istak.org.tw;
public class calc {
private static float num1;
private static float num2;
private static String oper;
private static float result;
public calc(int num1, int num2, String oper){
this.num1 = num1;
this.num2 = num2;
this.oper = oper;
}
public static float calcnum(){
switch(oper)
{
case"+":
result = num1+num2;
break;
case"-":
result = num1-num2;
break;
case"*":
result = num1*num2;
break;
case"/":
result = num1/num2;
break;
}
return result ;
}
}
複製代碼
作者:
蔡季樺
時間:
2016-12-11 07:15
package site.istak.orrg.tw;
public class Main {
public static void main(String[] args) {
Calc calc = new Calc(1,1,"+");
System.out.println(calc.Calcnum());
}
}
複製代碼
package site.istak.orrg.tw;
public class Calc {
private static float num1;
private static float num2;
private static String oper;
private static float total;
public Calc(float num1,float num2,String oper)
{
this.num1 = num1;
this.num2 = num2;
this.oper = oper;
}
public static float Calcnum()
{
switch(oper)
{
case"+":
total = num1+num2;
break;
case"-":
total = num1-num2;
break;
case"*":
total = num1*num2;
break;
case"/":
total = num1/num2;
break;
}
return total;
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2