|
#1
| |||
| |||
| [求助] [Java]輸入通話時間計算費用 最近Java教到OO這個章節,我想要將他用物件的方式給跑出來 可是卻遲遲無法成功 各位大大可以指點一下迷津嘛?? 我想用以下的方法做出來 ================= compute Charge() =>費用 compute Largest() =>取最大值 main() { 產生charge類別的物件 for() { 讀入通話時間=>到 ; 計算通話費(呼叫compute Charge)存入array } 呼叫compute Largest取最大值print出來 } ==================== 代碼: import java.io.*;
public class Charge{
public static void main(String[] args) throws IOException {
int[] userTime = new int[3];
double[] userMoney = new double[3];
double temp;
for(int people = 0 ; people<3 ; people++)
{
BufferedReader br=new
BufferedReader(new InputStreamReader(System.in));
System.out.println("====================================");
System.out.print("請輸入通話時間:");
String str = br.readLine();
userTime[people]=Integer.parseInt(str);
switch(userTime[people]/100){
case 0:case 1:case 2:case 3:case 4:case 5:case 6:
case 7:
System.out.println("第"+(people+1)+"用戶通話費用為:"+userTime[people]*0.9+"元");
userMoney[people] = userTime[people]*(0.9);
break;
case 8:case 9:case 10:case 11:case 12:case 13:case 14:
System.out.println("第"+(people+1)+"用戶通話費用為:"+userTime[people]*0.9*0.9+"元");
userMoney[people] = userTime[people]*(0.81);
break;
default:
System.out.println("第"+(people+1)+"用戶通話費用為:"+userTime[people]*0.9*0.9*0.79+"元");
userMoney[people] = userTime[people]*(0.7399);
break;
}
}
System.out.println("====================================");
double max = 0 ;
for(double z :userMoney)
{
if(z>max)
{
max = z;
}
}
System.out.println("最大值:"+max);
System.out.println("====================================");
}
}
__________________ 盜版無罪!原創真理! |
|
#2
| ||||
| ||||
| 如果是想用 new object 的方式 我簡單的改了一下…你可以參考看看… 和寫method(方法),的意思差不多 只是這裡寫成class(類別)而已。 代碼: package shiuan.secure;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class test2{
public static void main(String arg[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("請輸入編號:");
String u_id = br.readLine();
System.out.println("請輸入通話時間:");
int u_time = Integer.parseInt(br.readLine());
compute Chare = new compute();//建立compute物件
double money = Chare.total(u_time);
System.out.println("使用者:" + u_id);
System.out.println("通話時間:" + u_time);
System.out.println("=====================");
System.out.println("通話費用:" + money);
}
}
class compute {
protected double total(double u_time) {
return u_time * 0.9;//傳回運算後的結果
}
}
__________________ 旅行不一定要有目的…遇然發現也許是一種驚奇… 法國人常說:「經常向右轉的路,你把它向左轉,這也算是旅行。 此文章於 2005-12-03 02:30 AM 被 dershiuan 編輯. |
|
#3
| |||
| |||
| 看了大大的程式碼用class展現,十分感激!! 不過我用方法卻還是行不通耶 嗯嗯!! 小弟對於很多事情都還不懂~~ 看來小弟還有很多要學的!! 今後請大大多多指教囉!!!^_^a
__________________ 盜版無罪!原創真理! |
|
#4
| ||||
| ||||
| 如果你是寫成『方法』的話,就不能用 new『物件』了 方法的話直接打 [檔名].[方法](); 雖然得到的結果是一樣的,但什麼時候用方法or類別的寫法 就要看自己的想法與用途了。 範例如下: 代碼: package shiuan.secure;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class test2{
public static void main(String arg[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("請輸入編號:");
String u_id = br.readLine();
System.out.println("請輸入通話時間:");
int u_time = Integer.parseInt(br.readLine());
//----用產生物件的方式----
compute Chare = new compute();//建立compute物件
double money = Chare.total(u_time);
//---------END---------
System.out.println("使用者:" + u_id);
System.out.println("通話時間:" + u_time);
System.out.println("=====================");
System.out.println("通話費用:" + money);
System.out.println("---------------------");
//----用方法來取得結果---
double mmoney = test2.mtotal(u_time);
//--------END---------
System.out.println("使用者:" + u_id);
System.out.println("通話時間:" + u_time);
System.out.println("=====================");
System.out.println("通話費用:" + mmoney);
}
//----這裡就是方法了---
public static double mtotal(double utime){
return utime*0.9;
}
}
class compute {
protected double total(double u_time) {
return u_time * 0.9;//傳回運算後的結果
}
}
__________________ 旅行不一定要有目的…遇然發現也許是一種驚奇… 法國人常說:「經常向右轉的路,你把它向左轉,這也算是旅行。 此文章於 2005-12-04 10:05 AM 被 dershiuan 編輯. |
|
#5
| |||
| |||
| 我try了一下結果沒辦法比大小ㄟ=_=+ 遇到的瓶頸還蠻多的!! 敗在陣列不知如何是好!!! 看來還有很長的一段路要走 代碼: import java.io.*;
class Computer{
public static void main(String[] args) throws IOException
{
int[] Time = new int[3];
double[] Money = new double[3];
Computer.Charge();
Computer.largest(Time);
}
public static void Charge() throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] Time = new int[3];
double[] Money = new double[3];
String str;
for(int i = 0;i<3;i++){
System.out.println("========================");
System.out.print("請輸入通話時間:");
str = br.readLine();
Time[i] = Integer.parseInt(str);
switch(Time[i]/100)
{
case 0:case 1:case 2:case 3:case 4:case 5:case 6:
case 7:
Money[i] = Time[i]*(0.9);
System.out.println("第"+(i+1)+"用戶通話費用為:"+Money[i]+"元");
break;
case 8:case 9:case 10:case 11:case 12:case 13:case 14:
Money[i] = Time[i]*(0.81);
System.out.println("第"+(i+1)+"用戶通話費用為:"+Money[i]+"元");
break;
default:
Money[i] = Time[i]*(0.7399);
System.out.println("第"+(i+1)+"用戶通話費用為:"+Money[i]+"元");
break;
}
}
}
public static void largest(int arr[]) throws IOException
{
int max = arr[0];
for(int i = 0 ; i <arr.length;i++)
if(max < arr[i])
max = arr[i];
System.out.println("========================");
System.out.println("The Max Value:" + max);
}
}
__________________ 盜版無罪!原創真理! 此文章於 2005-12-04 01:41 PM 被 emulator 編輯. |
|
#6
| ||||
| ||||
| 可以比大小阿…是你的變數宣告方式錯了 Time 應該要是全域的,而不區域的 不然程式只會抓到自己區域裡的Time值 你可以看看書,關於變數宣告的部分… 代碼: class test {
static int[] Time = new int[3];//整個程都會用的變數最好宣告在最開頭的地方…
public static void main(String[] args) throws IOException
{
double[] Money = new double[3];
Computer.Charge();
Computer.largest(Time);//這裡只要直接調用就可以了…不必再次宣告
}
__________________ 旅行不一定要有目的…遇然發現也許是一種驚奇… 法國人常說:「經常向右轉的路,你把它向左轉,這也算是旅行。 |
|
#7
| |||
| |||
| 提供一個直接做排序的功能,可以參考API java.util.Arrays 代碼: import java.util.* ;
public class Test {
public static void main(String[] args) {
double[] d = {3213,38,1,-958,31};
Arrays.sort(d); //把陣列按照小到大排列
for(int i=0 ; i<d.length ; i++){
System.out.println(i+":"+d[i]);
}
}
|