qq_宫湦_03802224
2017-12-29 10:38
//这是父类货车的代码
public class Truck {
static Float cargoCa; //创建货车的载货量
static int capacity=4; //创建货车的载客量
static int rent; //创建货车的日租金
int smallType; //用来接收局部变量smalltype的值
Scanner input=new Scanner(System.in);
void getType() {
System.out.println("请输入您要选择的货车类型:");
System.out.println("1、轻型货车(可核载5吨) 2、重型货车(可核载20吨)");
int smalltype=input.nextInt();
smallType=smalltype;
while(smalltype*0==0) {
if(smalltype==1) { //这部分重型货车的继承代码没有贴上
Heavy heavy=new Heavy();
heavy.getBrand();
break;
}else if(smalltype==2) {
Light light=new Light();
light.getBrand();
break;
}else {
System.out.println("您的输入有误!请输入您要选择的货车类型:");
System.out.println("1、轻型货车(可核载5吨) 2、重型货车(可核载20吨)");
smalltype=input.nextInt();
}
}
}
//show()方法调用两个轻货和重货子类的showFinal()方法展示租车清单
void show() {
if(smallType==1) {
Light light=new Light();
light.showFinal();
}else if(smallType==2) {
Heavy heavy=new Heavy();
heavy.showFinal();
}
}
}
//这是子类Light继承父类Truck的代码
public class Light extends Truck {
static String name="轻型货车";
//【注意,创建light字符串,用来接收局部变量中的品牌名称】
static String light;
Scanner input=new Scanner(System.in);
void getBrand() {
String light1="日产";
String light2="雪铁龙";
String light3="标致";
Light.cargoCa=5f;
System.out.println("请输入您要选择的轻型货车品牌:");
System.out.println("1、"+light1+"(1200元/天) "+"2、"+light2+"(1500元/天) "+"3、"+light3+"(1800元/天)");
int lBrand=input.nextInt();
while(lBrand*0==0) {
if(lBrand==1) {
Light.rent=1200;
light=light1;
break;
}else if(lBrand==2) {
Sedan.rent=1500;
light=light2;
break;
}else if(lBrand==3) {
Sedan.rent=1800;
light=light3;
break;
}else {
System.out.println("您的输入有误!请输入您要选择的轻型货车品牌:");
System.out.println(light1+"(1200元/天) "+light2+"(1500元/天) "+light3+"(1800元/天)");
lBrand=input.nextInt();
}
}
}
//子类Light用来展示租车清单的showFinal()方法
void showFinal() {
System.out.println("请输入您要租车的数量:");
int amt=input.nextInt();
System.out.println("请输入您要租车的天数:");
int days=input.nextInt();
int price=Light.rent*amt*days;
//【注意,就是这里的变量light,在InitialCRS类里面的最后输出结果为null】
System.out.println("车型:"+light+name);
System.out.println("总载人量:"+Truck.capacity*amt+" 总载货量:"+Light.cargoCa*amt+"吨");
System.out.println("总计:"+price+"元");
}
}
//这是执行类InitialCRS的代码
public class InitialCRS {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("请问您是否要租车?");
System.out.println("1、是 2、否");
int confirm=input.nextInt();
while(confirm*0==0) {
if(confirm==2) {
System.out.println("答答租车系统已退出,感谢您的使用!");
break;
}else if(confirm==1) {
System.out.println("请选择您要租赁的车辆类型:");
System.out.println("1、汽车 2、货车 3、皮卡");
int type=input.nextInt();
while(type*0==0) {
if(type==1) { //Car父类部分的代码没有贴出
Car car=new Car();
car.getType();
car.show();
break;
}else if(type==2) { //本次问题中的Truck类的方法调用
Truck truck=new Truck();
truck.getType();
truck.show();
break;
}else if(type==3) { //pickup部分的代码没有贴出
Pickup pickup=new Pickup();
pickup.getBrand();
pickup.show();
break;
}else {
System.out.println("您的输入有误!请选择您要租赁的车辆类型:");
System.out.println("1、汽车 2、货车 3、皮卡");
type=input.nextInt();
}
}
break;
}else {
System.out.println("您的输入有误!请问您是否要租车?");
System.out.println("1、是 2、否");
confirm=input.nextInt();
}
}
input.close();
}
}
//在这里多说一句,我在其它子类里边,以一模一样的方式创建了类似于light的用来接收品牌的字符串,其最后的输出结果都是正常的,只有这个字符串light不知道怎么回事输出结果竟然是null
好恐怖 那么长的一串代码
。。。
找到问题了,是Truck类中的Heavy与Light实例化的位置调换了。
其运行结果如右图,light的输出值为null
Java入门第二季
531368 学习 · 6327 问题
相似问题