作業(yè)代碼,傻瓜版
學(xué)得時間太短了,就只會用一點(diǎn)簡單方法,用了抽象的多態(tài),if,for,switch,import 。感覺思路還是比較清楚。先父類 再子類最后是操作Initail.(先寫了分開的 后面合起來的時候出了點(diǎn)問題 加了static 就好了(public class Jl extends Auto 改成public static class Jl extends Auto?))
package 綜合作業(yè)合;
import java.util.*;
public abstract class Auto {
? ?public ?String Name;
public float Price;
public int Nm;
public int Zkl;
public int Zhl;
public abstract void Run();
?//
public static class Ada4 extends Auto {
public String Name="奧迪A4";
public float Price=500;
public int Nm=1;
public int Zkl=4;
? ? ? ? public ?void Run() {
? ? ? ? ? ?System.out.println(Nm+" ? ? "+Name+" ? "+Price+"元/天"+" ?"+Zkl+"人");
}
? ? ? ? ?}
//
public static class Jl extends Auto {
public String Name="金龍";
public float Price=800;
public int Nm=4;
public int Zkl=20;
? ?public void Run() {
System.out.println(Nm+" ? ? "+Name+" ? ? ? ? "+Price+"元/天"+" ?"+Zkl+"人");
? ? ? ? }
? ? ? ? ? ?}
? ?//
public static class Mzd6 extends Auto {
public String Name="馬自達(dá)6";
public float Price=400;
public int Nm=2;
public int Zkl=4;
? ?public void Run() {
System.out.println(Nm+" ? ? "+Name+" ?"+Price+"元/天"+" ?"+Zkl+"人");
}
? ? ? ?}
//
public static class Pkx extends Auto {
public String Name="皮卡雪6";
public float Price=450;
public int Nm=3;
public int Zkl=4;
public int Zhl=2;
public void Run() {
System.out.println(Nm+" ? ? "+Name+" ?"+Price+"元/天"+" ? "+Zkl+"人"+Zhl+"噸");
}
}
//
public static class Shj extends Auto {
public String Name="松花江";
public float Price=400;
public int Nm=5;
public int Zhl=4;
public void Run() {
System.out.println(Nm+" ? ? "+Name+" ? ? "+Price+"元/天"+" ? "+Zhl+"噸");
}?
}
//
public static class Ywh extends Auto {
public String name="依維河";
public float price=1000;
public int Nm=6;
? ? ? ? public int Zhl=20;
public void Run() {
System.out.println(Nm+" ? ? "+name+" ? ? "+price+"元/天"+" ?"+Zhl+"噸");
}
}
public static void main(String[] args) {
System.out.println("您是否租車? ?1是 0否");
Scanner input = new Scanner(System.in);
? int r = input.nextInt(); ?
? if(r==1){
System.out.println("您可租車的類型及其價目表:");
System.out.println("序號"+" ?"+"汽車名稱"+" ? ? ?"+"租金"+" ? ? ? ? "+"容量");
Auto i1=new Ada4();
i1.Run();
Auto i2=new Mzd6();
i2.Run();
Auto i3=new Pkx();
i3.Run();
Auto i4=new Jl();
i4.Run();
Auto i5=new Shj();
i5.Run();
Auto i6=new Ywh();
i6.Run();
System.out.println("請輸入您要租車的數(shù)量:");
Scanner input1 = new Scanner(System.in);
? int r1 = input1.nextInt();?
? float ?num=0;
? int a1=0,a2=0,a3=0,a4=0,a5=0,a6=0;
? ? for(int n=1;n<=r1;n++){
? ? System.out.println("請輸入第"+n+"輛車的序號");
? Scanner input2 = new Scanner(System.in);
? ? int z = input2.nextInt();?
? ? switch(z){
? ? case 1:
? ? num=num+500;
? ? a1++;
? ? break;
? ? case 2:
? ? num=num+400;
? ?a2++;
? ? break;
? ? case 3:
? ? num=num+450;
? ? a3++;
? ? break;
? ? case 4:
? ? num=num+800;
? ? a4++;
? ? break;
? ? case 5:
? ? num=num+400;
? ? a5++;
? ? break;
? ? case 6:
? ? num=num+1000;
? ? a6++;
? ? break; ??
? ? }
? ? ? ? ?}
? ? ? ? ? ? ?System.out.println("請輸入租車的天數(shù):");
? ? ? ? ? ? Scanner input3 = new Scanner(System.in);
? ? ? ? ? ? ? int x = input3.nextInt();?
? ? ?int sumpeople=a1*4+a2*4+a3*4+a4*20+a5*0+a6*0;?
? ? ?int sumzaihuo=a3*2+a5*4+a6*20;
? ? ?float sumzongjia=num*x;
? ? ? ? ? ? ? System.out.println("您的賬單:");
? ? ? ? ? ? ??
? ? ? ? ? ? ? System.out.println("***可載人的車有:");
? ? ? ? ? ? ? if(a1>0){System.out.print("奧迪A4,");}
? ? ? ? ? ? ? if(a2>0){System.out.print("馬自達(dá)6,");}
? ? ? ? ? ? ? if(a3>0){System.out.print("皮卡雪6,");}
? ? ? ? ? ? ? if(a4>0){System.out.print("金龍,");}
? ? ? ? ? ? ? System.out.println("共載人:"+sumpeople);
? ? ? ? ? ? ? System.out.println("***載貨的車有:");
? ? ? ? ? ? ? if(a3>0){System.out.print("皮卡雪6,");}
? ? ? ? ? ? ? if(a5>0){System.out.print("松花江,");}
? ? ? ? ? ? ? if(a6>0){System.out.print("依維河,");}
? ? ? ? ? ? ? System.out.println("共載貨:"+sumzaihuo);
? ? ? ? ? ? ? System.out.println("租車總價:"+sumzongjia);
}
}
}
2016-12-01
為什么要加static?新手同問。