package exercise03; public class Car { public int manned; public int cargo; public int rental; public void introduce(){ System.out.println("1. 奥迪A4 500元/天 载人:4人"); } } package exercise03; public class AodiA4 extends Car { String name="奥迪A4"; int rental=500; int manned=4; int cargo=0; public void introduce(){ System.out.println("1. 奥迪A4 500元/天 载人:4人"); } } package exercise03; public class Jinlong extends Car { String name="金龙"; int rental=800; int manned=20; int cargo=0; public void introduce(){ System.out.println("4. 金龙 800元/天 载人:20人"); } } package exercise03; public class Mazida6 extends Car { String name="马自达6"; int rental=400; int manned=4; int cargo=0; public void introduce(){ System.out.println("2. 马自达6 400元/天 载人:4人"); } } package exercise03; public class Pikaxue6 extends Car { String name="皮卡雪6"; int rental=450; int manned=4; int cargo=2; public void introduce(){ System.out.println("3. 皮卡雪6 450元/天 载人:4人 载货:两吨"); } } package exercise03; public class Songhuajiang extends Car { String name="松花江"; int rental=400; int manned=4; int cargo=4; public void introduce(){ System.out.println("5. 松花江 400元/天 载货:4吨"); } } package exercise03; public class Yiweike extends Car { String name="依维柯"; int rental=1000; int cargo=20; int manned=0; public void introduce(){ System.out.println("6. 依维柯 1000元/天 载货:20吨"); } } package exercise03; import java.util.Scanner; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); Car p1=new AodiA4(); Car p2=new Jinlong(); Car p3=new Mazida6(); Car p4=new Pikaxue6(); Car p5=new Songhuajiang(); Car p6=new Yiweike(); AodiA4 p12=new AodiA4(); Jinlong p22=new Jinlong(); Mazida6 p32=new Mazida6(); Pikaxue6 p42=new Pikaxue6(); Songhuajiang p52=new Songhuajiang(); Yiweike p62=new Yiweike(); System.out.println("欢迎使用答答租车系统:"); System.out.println("您是否要租车:1是 0否"); int k=input.nextInt(); if(k==0){ System.out.println("欢迎您未来租车时使用本软件"); }else{ System.out.println("您可租车的类型及其价目表:"); System.out.println("序号 汽车名称 租金 容量"); p1.introduce(); p2.introduce(); p3.introduce(); p4.introduce(); p5.introduce(); p6.introduce(); System.out.println("请输入您要租用的车辆数"); int i=input.nextInt(); int[] nums=new int[i]; for(int j=1,h=0;j<=i;h++,j++){ System.out.println("请输入您要租用的第"+j+"辆车的序号"); nums[h]=input.nextInt(); } System.out.println("请输入要租用的天数"); int d=input.nextInt(); int allmanned=0; int allcargo=0; int money=0; System.out.println("您要租用的车辆分别为:"); for(i=0;i<nums.length;i++){ if(nums[i]==1){ p1.introduce(); allmanned=allmanned+p12.manned; allcargo=allcargo+p12.cargo; money=money+p12.rental; }else if(nums[i]==2){ p2.introduce(); allmanned=allmanned+p22.manned; allcargo=allcargo+p22.cargo; money=money+p22.rental; }else if(nums[i]==3){ p3.introduce(); allmanned=allmanned+p32.manned; allcargo=allcargo+p32.cargo; money=money+p32.rental; }else if(nums[i]==4){ p4.introduce(); allmanned=allmanned+p42.manned; allcargo=allcargo+p42.cargo; money=money+p42.rental; }else if(nums[i]==5){ p5.introduce(); allmanned=allmanned+p52.manned; allcargo=allcargo+p52.cargo; money=money+p52.rental; }else if(nums[i]==6){ p6.introduce(); allmanned=allmanned+p62.manned; allcargo=allcargo+p62.cargo; money=money+p62.rental; } } int allmoney=money*d; System.out.println("您租用的天数为"+d+"天"); System.out.println("合计载人量:"+allmanned+"人"); System.out.println("合计载货量"+allcargo+"吨"); System.out.println("租金总计:"+allmoney+"元"); } } }
在父类方法里写一个初始化参数的成员方法 然后通过main函数初始化 可以减少重复代码
能不能注意一下格式。。。
再有,慕课提供的富文本编辑器也提供代码块的编辑啊,为毛就不用呢?
看都不想看=。= 格式受不了