父类:定义车
package com.imooc;
public class Car {
private String name;//车的名字
private int carNum;//车的编号
private int price;//车的价格
private int carrying;//载货量
private int seatload;//载客量
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCarNum() {
return carNum;
}
public void setCarNum(int carNum) {
this.carNum = carNum;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getCarrying() {
return carrying;
}
public void setCarrying(int carrying) {
this.carrying = carrying;
}
public int getSeatload() {
return seatload;
}
public void setSeatload(int seatload) {
this.seatload = seatload;
}
}
子类:定义客车
package com.imooc;
public class keche extends Car {
public keche(int carNum,String name,int price,int seatload){
super.setCarNum(carNum);
super.setName(name);
super.setPrice(price);
super.setSeatload(seatload);
}
}
子类:定义卡车
package com.imooc;
public class kache extends Car {
public kache(int carNum,String name,int price,int carrying){
super.setCarNum(carNum);
super.setName(name);
super.setPrice(price);
super.setSeatload(carrying);
}
}
子类:定义皮卡
package com.imooc;
public class pcar extends Car {
public pcar(int carNum,String name,int price,int carrying,int seatload){
super.setCarNum(carNum);
super.setName(name);
super.setPrice(price);
super.setCarrying(carrying);
super.setSeatload(seatload);
}
}
调用方法:
package com.imooc;
import java.util.Scanner;
public class dadazuche {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int s0=0;
Car[] carFormat = {new keche(1,"奥迪A4",500,4),new keche(2,"马自达6",400,4),
new pcar(3,"皮卡雪6",450,4,4),new keche(4,"金龙",800,20),
new kache(5,"松花江",400,4),new kache(6,"依维柯",1000,20)};//车辆信息
int sum;//车辆总辆
System.out.println("欢迎使用哒哒租车系统:");
System.out.println("您是否要租车:1是 0否");
Scanner s = new Scanner(System.in);
s0 = s.nextInt();
int i,j;
if (s0 == 0)
{
System.out.println("感谢使用");
}
if (s0 == 1)
{
System.out.printf("序号\t汽车名称\t租金\t容量\t车型总量\n");
for(i=0;i<6;i++)
{
if(i<2){
sum=500;
System.out.println(carFormat[i].getCarNum() + ".\t" + carFormat[i].getName() + "\t" + carFormat[i].getPrice() + "元/天\t" + "载人:" + carFormat[i].getSeatload() + "人\t" + sum );
}
if(i==2){
sum =500;
//System.out.println(carFormat[1].getCarNum() + ".\t" + carFormat[1].getName() + "\t" + carFormat[1].getPrice() + "元/天\t" + "载人:" + carFormat[1].getSeatload() + "人\t" + 500);
//System.out.println(carFormat[2].getCarNum() + ".\t" + carFormat[2].getName() + "\t" + carFormat[2].getPrice() + "元/天\t" + "载人:" + carFormat[2].getSeatload() + "人\t" + 500);
System.out.println(carFormat[i].getCarNum() + ".\t" + carFormat[i].getName() + "\t" + carFormat[i].getPrice() + "元/天\t" + "载人:" + carFormat[i].getSeatload() + "人\t " + "载货:" + carFormat[i].getCarrying() + "吨\t" + 500);
}
if(i==3){
sum=500;
System.out.println(carFormat[i].getCarNum() + ".\t" + carFormat[i].getName() + "\t" + carFormat[i].getPrice() + "元/天\t" + "载人:" + carFormat[i].getSeatload() + "人\t" + sum );
}
if(i>3){
sum =1000;
System.out.println(carFormat[i].getCarNum() + ".\t" + carFormat[i].getName() + "\t" + carFormat[i].getPrice() + "元/天\t" + "载货:" + carFormat[i].getCarrying() + "吨\t" + sum);
//System.out.println(carFormat[5].getCarNum() + ".\t" + carFormat[5].getName() + "\t" + carFormat[5].getPrice() + "元/天\t" + "载货:" + carFormat[5].getCarrying() + "吨\t" + sum);
}
}
System.out.println("请输入您要租汽车的数量:");
int sn = s.nextInt();
int a[] = new int[sn];//选择汽车的信息保存数组
if(sn>6){
System.out.println("输入的数量大于已有数量。");
}
if(sn<=6){
for (j=0;j<sn;j++){
System.out.println("请输入第"+(j+1)+"辆车的序号");
Scanner s1 = new Scanner(System.in);
a[j] = s1.nextInt();
}
System.out.println("请输入租车天数:");
int d = s.nextInt();
int cusNum =0;//载人数
int bagNum =0;//载货数
int sumPrice =0; //总价格
System.out.println("您的账单:");
//***********************************************************************
System.out.println("****可载人的车有:");
for(int aj=0;aj<sn;aj++){
if(a[aj]== 1|| a[aj] == 2|| a[aj] == 4 ){
cusNum += carFormat[a[aj]-1].getSeatload();
System.out.print(carFormat[a[aj]-1].getName() + "\t");
}
}
System.out.println("共载人:"+ cusNum + "人");
//**************************************************************************
System.out.println("****可载货的车有:");
for(int lj=0;lj<sn;lj++){
if(a[lj]== 3|| a[lj] == 5|| a[lj] == 6 ){
bagNum += carFormat[a[lj]-1].getCarrying();
System.out.print(carFormat[a[lj]-1].getName() + "\t");
}
}
System.out.println("共载货:"+ bagNum + "吨");
System.out.print("****租车总价格:");
for(int bj=0;bj<sn;bj++){
sumPrice += carFormat[bj].getPrice()*d;
}
System.out.print(sumPrice);
}
}
}
}
热门评论
System.out.println("欢迎使用哒哒租车系统:");
System.out.println("您是否要租车:1是 0否");
这两句可以直接写在类里吗?
我在写这两句时出现报错,
报错语法错误,错误放置了构造
哥们,你这个是怎么提交上去的呢