学得时间太短了,就只会用一点简单方法,用了抽象的多态,if,for,switch,import 。感觉思路还是比较清楚。先父类 再子类最后是操作Initail.(先写了分开的 后面合起来的时候出了点问题 加了static 就好了(public class Jl extends Auto 改成public static class Jl extends Auto ))
package 综合作业合;
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="马自达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("请输入您要租车的数量:");
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("请输入租车的天数:");
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("马自达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);
}
}
}
为什么要加static?新手同问。