# 作業卡很久了= =急...晚上12點前要上傳給老師

class car{

String num; //車牌

double oil; //剩下油量

double oil2; //耗油量

double ie; //能跑公里數

}

public class test{

public static void main (String [] argv){

car car1,car2,car3,car4,car5;

car1=new car();

car2=new car();

car3=new car();

car4=new car();

car5=new car();

car1.num="123-AB";

car1.oil=500.0;

car1.oil2=9.0;

car1.ie=car1.oil/car1.oil2;

car2.num="456-CD";

car2.oil=750.0;

car2.oil2=8.0;

car2.ie=car2.oil/car2.oil2;

car3.num="789-EF";

car3.oil=850.0;

car3.oil2=10.0;

car3.ie=car3.oil/car3.oil2;

car4.num="012-GH";

car4.oil=30.0;

car4.oil2=12.0;

car4.ie=car4.oil/car4.oil2;

car5.num="345-AB";

car5.oil=30.0;

car5.oil2=11.0;

car5.ie=car5.oil/car5.oil2;

}

}

### 2 個解答

• 1 0 年前
最佳解答

我只做car1 其他的請自己copy喔!!

請問你是哪間學校的呢?

class car {

String num; //車牌

double oil; //剩下油量

double oil2; //耗油量

double ie; //能跑公里數

car(String num,double oil,double oil2){

this.num=num;

this.oil=oil;

this.oil2=oil2;

}

public String getCarData(){

return "車牌號碼:"+num+"\n油耗:"+oil+"L/km"+"\n耗油量:"+oil2+"L/km";

}

public double calculateOil(){

ie=oil/oil2;

return ie;

}

}

public class cartest{

public static void main(String args[]){

car car1=new car("123-AB",500,9);

System.out.println(""+car1.getCarData());

System.out.println("能跑公里數:"+car1.calculateOil()+"km\n");

}

}

參考資料： java新手...ives.
• 1 0 年前

請在 car5 那段後面加上以下程式碼。

Car[] cars = {car1, car2, car3, car4, car5};

java.util.Scanner scan = new java.util.Scanner(System.in);

System.out.print("請輸入車牌: ");

String s = scan.next();

for (int i = 0; i < cars.length; i++) {

if (s.equalsIgnoreCase(cars[i].num)) {

System.out.println("這輛車還能跑 " + cars[i].ie + " 公里");

}

}