複製內容到剪貼板
代碼:
import java.io.*; //輸入資料
public class input //公開類別 命名"輸入"
{
public static void main(String args[]) throws Exception
{
// 公開沒有回傳值的一維陣列 main為程式進入點 "throws Exception"丟入例外
computer calculate=new computer(); //設一各叫"計算"的物件
calculate.poweron(); //計算開始開啟計算機
java.io.BufferedReader keyin; //取得使用者輸入 "BufferedReader"讀入緩衝器
keyin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
// 從系統裡面取得輸入
System.out.println("請輸入第一個數 "); //提供使用者提示介面
int i = Integer.parseInt(keyin.readLine()); //強制將輸入字串轉換為整數
System.out.println("請輸入第二個數 "); //提供使用者提示介面
int j = Integer.parseInt(keyin.readLine()); //強制將輸入字串轉換為整數
System.out.println("兩數相加 "+calculate.add(i,j)); //呼叫方法並印出相加結果
System.out.println("兩數相減 "+calculate.sub(i,j)); //呼叫方法並印出相減結果
System.out.println("兩數相乘 "+calculate.mul(i,j)); //呼叫方法並印出相乘結果
System.out.println("兩數相除 "+calculate.div(i,j)); //呼叫方法並印出相除結果
calculate.poweroff(); //計算完成關閉計算機
}
}
class computer //設一各類別 名叫"計算機"
{
public boolean power=false; //計算機開始為關閉
void poweron()
{ power=true; //計算機開啟
System.out.println("ON "+power); //印出"ON"讓使用者知道計算機開啟
}
void poweroff()
{
power=false; //計算機關閉
System.out.println("OFF "+power);//印出"OFF"讓使用者知道計算機關閉
}
double add(double i,double j){return i+j;} //設變數類型並回傳初值方法(加法)
double sub(double i,double j){return i-j;} //設變數類型並回傳初值方法(減法)
double mul(double i,double j){return i*j;} //設變數類型並回傳初值方法(乘法)
double div(double i,double j){return i/j;} //設變數類型並回傳初值方法(除法)
}
利用呼叫做的累加計算機複製內容到剪貼板
代碼:
import java.io.*; //公開類別 命名"輸入"
public class input2
{
public static void main(String args[]) throws Exception
{
java.io.BufferedReader keyin; //取得使用者輸入 "BufferedReader"讀入緩衝器
keyin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
System.out.println("請輸入整數的n值 "); //提供使用者提示介面
int n = Integer.parseInt(keyin.readLine()); //強制將輸入字串轉換為整數
Totalizer tt=new Totalizer();
tt.add(n,0,1);
System.out.println("1加到"+n+"累加結果為"+tt.add(n,0,1));//印出結果
}
}
public class Totalizer //公開命名一各叫加法計算器的類別
{
public int add(int n,int sum,int i)
{
do //利用do/while先執行累加方法,再作一次流程判斷
{
sum+=i;
i++;
}
while(i<=n);
return sum;
}
}
[
本帖最後由 philxyz0316 於 2006-8-8 20:38 編輯 ]