課程
/后端開發(fā)
/Java
/Java入門第二季 升級(jí)版
有誰有6-1的代碼 ‘順便說下解題思路’ 多謝
2017-04-10
源自:Java入門第二季 升級(jí)版 12-1
正在回答
import java.util.Scanner; //導(dǎo)入Scanner類
class Car //定義一個(gè)父類{?public String name; //定義名字?public float rent; //租金?public int number; //載人量?public float weight; //載貨量??public float getRent() ?{??return rent;?}??public String getName()?{??return name;?}??public float getWeight()?{??return weight;?}??public int getNumber()?{??return number;?}}
interface IBus //定義一個(gè)接口,實(shí)現(xiàn)了該接口的類,表示該類具有載人的功能{}
interface ITruck //定義一個(gè)接口,實(shí)現(xiàn)了該接口的類,表示該類具有載貨的功能{}
class Bus extends Car implements IBus //創(chuàng)建一個(gè)子類,該類繼承了父類Car,實(shí)現(xiàn)了 IBus 接口{?public Bus(String name, int number, float rent) //創(chuàng)建構(gòu)造方法?{??this.name = name;??this.number = number;??this.rent = rent;?}?}
class Truck extends Car implements ITruck //創(chuàng)建一個(gè)子類,該類繼承了父類Car,實(shí)現(xiàn)了 ITruck 接口{?public Truck(String name, float weight, float rent) //創(chuàng)建構(gòu)造方法?{??this.name = name;??this.weight = weight;??this.rent = rent;?}}
class BusTruck extends Car implements IBus, ITruck //創(chuàng)建一個(gè)子類,該類繼承了父類Car,實(shí)現(xiàn)了 ITruck 接口和 IBus 接口{?public BusTruck(String name, int number, float weight, float rent) //創(chuàng)建構(gòu)造方法?{??this.name = name;??this.number = number;??this.weight = weight;??this.rent = rent;?}}
class CarRental {?private String bus = ""; //定義一個(gè)字符串,用來存放所有載人汽車的名字?private String truck = ""; //定義一個(gè)字符串,用來存放所有載貨汽車的名字?private int busNum; //定義一個(gè)整形變量,用來存放所有載人汽車的總載人量?private float truckWeight; //定義一個(gè)浮點(diǎn)型變量,用來存放把有載貨汽車的總載貨量?private float sumRent; //定義一個(gè)浮點(diǎn)型變量,用來存放所有汽車的一天租金的總和,注意是一天?private int date; //定義租用的天數(shù)?private Scanner sc = null; //定義一個(gè)Scanner工具?private Car[] c = null; //定義一個(gè)父類類型的數(shù)組,用來接受mian()方法中傳遞過來的參數(shù)??public CarRental(Car[] c) //通過構(gòu)造函數(shù) 使本類中的 c 跟mian()方法中的 c 相關(guān)連?{??this.c = c;?}??public void launch() //該方法由mian()方法調(diào)用?{??show(); ??menu();??count();??output();?}??public void show() //顯示的信息?{??System.out.println("歡迎使用答答租車系統(tǒng):");??System.out.println("您是否要租車:" + "1 是" + ", 0 否");????sc = new Scanner(System.in); //接收從鍵盤輸入的信息??while (true)??{???int s = sc.nextInt(); //將信息保存到變量s中??????if (s!=1 && s!=0) //如果輸入有誤,則重新輸入???{????System.out.println("您的輸入有誤!請(qǐng)重新輸入");????continue;???}???else if (s == 0) //當(dāng)輸入0時(shí),程序終止????System.exit(-1);???else? //輸入合法,則跳出循環(huán),執(zhí)行接下來的語句????break;??}?}??public void menu() //輸出菜單信息?{??System.out.println("\n" + "您可租車的類型及價(jià)目表:");??System.out.println("***************************");????System.out.println("序號(hào)? " + "汽車名稱? " + "租金?? " + "容量");??System.out.println("1.??? " + "奧迪A4?? " + "500元/天? " + "載人:4人");??System.out.println("2.??? " + "馬自達(dá)6? " + "400元/天? " + "載人:4人");??System.out.println("3.??? " + "皮卡雪6? " + "450元/天? " + "載人:4人" + ", 載貨:2噸");??System.out.println("4.??? " + "金龍???? " + "800元/天? " + "載人:20人");??System.out.println("5.??? " + "松花江?? " + "400元/天? " + "載貨:4噸");??System.out.println("6.??? " + "依維柯?? " + "1000元/天 " + "載貨:20噸");??System.out.println("***************************" + "\n");?}??public void count() //統(tǒng)計(jì)?{??System.out.println("請(qǐng)輸入您要租汽車的數(shù)量:");??sc = new Scanner(System.in);??int count = sc.nextInt(); //count 表示用戶要租的汽車的數(shù)量????for(int i=1; i<=count; i++) //使用循環(huán)進(jìn)行統(tǒng)計(jì)??{???System.out.println("請(qǐng)輸入第" + i + "輛車的序號(hào):");???int num = sc.nextInt();??????if (num<1 || num>6) //當(dāng)輸入有誤時(shí),程序終止???{????System.out.println("您的輸入有誤!程序終止!");????System.exit(-1);???}???else if (3 == num) //當(dāng)用戶輸入3時(shí),對(duì)應(yīng)上面菜單上的 "皮卡雪6",即能載人,也能載貨???{????bus = bus + " " + c[num-1].getName(); //將3對(duì)應(yīng)的車的名字存放到bus里面,因?yàn)閏是一個(gè)數(shù)組引用,用其下標(biāo)表示要用 c[num-1]????busNum = busNum + c[num-1].getNumber();????sumRent = sumRent + c[num-1].getRent();????truck = truck + " " + c[num-1].getName();????truckWeight = truckWeight + c[num-1].getWeight();???}???else if (num < 5) //當(dāng)num的值為1,2,4的時(shí)候???{????bus = bus + " " + c[num-1].getName();????busNum = busNum + c[num-1].getNumber();????sumRent = sumRent + c[num-1].getRent();???}???else? //當(dāng)num的值為5,6的時(shí)候???{????sumRent = sumRent + c[num-1].getRent();????truck = truck + " " + c[num-1].getName();????truckWeight = truckWeight + c[num-1].getWeight();???}??}????System.out.println("請(qǐng)輸入租車天數(shù):");??date = sc.nextInt();??if (date < 1) //當(dāng)輸入有誤時(shí),程序終止??{???System.out.println("您的輸入有誤!程序終止!");???System.exit(-1);??}
??sumRent = sumRent * date;? //計(jì)算出所有車的租金總和?}??public void output() //輸出?{??System.out.println("***************************");??System.out.println("您的賬單:");??System.out.println("***可載人的車有:" + "\n" + bus + " 共載人:" + busNum + "人");??System.out.println("***可載貨的車有:" + "\n" + truck + " 共載貨:" + truckWeight + "噸");??System.out.println("***租車總價(jià)格:" + sumRent + "元");?}}
public class TestCarRental ?//啟動(dòng)類{?public static void main(String[] args) ?{??Car[] c = {new Bus("奧迪A4", 4, 500),?????? new Bus("馬自達(dá)6", 4, 400),?????? new BusTruck("皮卡雪6", 4, 2, 450),?????? new Bus("金龍", 20, 800),?????? new Truck("松花江", 4, 400),?????? new Truck("依維柯", 20, 1000)?????? };????new CarRental(c).launch();???? ?}}
慕粉1471134825 提問者
package zuche.com;
public class Pickup extends Car{ ?int thingcount;?int peoplecount;?public Pickup(String name,String rent,int thingcount,int peoplecount){??this.name=name;??this.rent=rent;??this.peoplecount=peoplecount;??this.thingcount=thingcount;?}?public void setThingcount(int thingcount) {??this.thingcount = thingcount;?}?public int getThingcount() {??return thingcount;?}?public int getPeoplecount() {??return peoplecount;?}?public void setPeoplecount(int peoplecount) {??this.peoplecount = peoplecount;?}??}
public class Car {?String name;?String rent;?int peoplecount;?int thingcount;?public String getName() {??return name;?}?public String getRent() {??return rent;?}?public int getPeoplecount() {??return peoplecount;?}?public int getThingcount() {??return thingcount;?}
}
public class PassengerCar extends Car{?int peoplecount;?public PassengerCar(String name,String rent,int peoplecount){??this.name=name;??this.peoplecount=peoplecount;??this.rent=rent;?}?public int getPeoplecount() {??return peoplecount;?}?public void setPeoplecount(int peoplecount) {??this.peoplecount = peoplecount;?}?}
public class Trunk extends Car{?int thingcount;?public Trunk(String name,String rent,int thingcount){??this.thingcount=thingcount;??this.name=name;??this.rent=rent;???}?public int getThingcount() {??return thingcount;?}?public void setThingcount(int thingcount) {??this.thingcount = thingcount;?}?}
import java.util.Scanner;
public class Test {?public static void main(String[] args) {??Car[] carsForRent={new PassengerCar("勞斯萊斯","500元/天",4)??,new PassengerCar("拉沙馬蒂","600元、天",2)??,new Pickup("皮卡","300元/天",2,4)??,new Trunk("松花江","800元/天",5)??,new Trunk("依維柯","1000元/天",20)??};????System.out.println("歡迎使用嗒嗒租車行:");????System.out.println("你是否需要租車:1是 0否");??Scanner scanner=new Scanner(System.in);????String input=scanner.next();????if(input.equals("1")){???System.out.println("你可租用的車的類型及價(jià)格等");???System.out.println("序號(hào)\t 汽車名稱\t租金\t載客量\t載重量\t");???int i=1;???for(Car c:carsForRent){????????if(c instanceof PassengerCar){????System.out.println(""+i+"\t"+c.getName()+"\t"+c.getRent()??????+"\t"+c.getPeoplecount()+"人");????}else if(c instanceof Trunk){?????System.out.println(""+i+"\t"+c.getName()+"\t"+c.getRent()???????+"\t"+"\t"+c.getThingcount()+"噸");????}else{?????System.out.println(""+i+"\t"+c.getName()+"\t"+c.getRent()???????+"\t"+c.getPeoplecount()+"人"+"\t"+c.getThingcount()+"噸");????}????i++;???}??}?????System.out.println("請(qǐng)輸入你要租車的序號(hào)");???@SuppressWarnings("unused")???int e=scanner.nextInt();??????????}?}
舉報(bào)
課程升級(jí)!以終為始告別枯燥,在開發(fā)和重構(gòu)中體會(huì)Java面向?qū)ο缶幊痰膴W妙
1 回答有沒有大佬能講解一下代碼的執(zhí)行順序,有點(diǎn)亂
3 回答最后一節(jié)的 代碼 誰有
1 回答下面代碼中的equals()方法誰能解釋一下
2 回答沒有思路,,,
1 回答等號(hào)左邊的代碼不太理解,有無大神細(xì)說一下?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2017-04-25
import java.util.Scanner; //導(dǎo)入Scanner類
class Car //定義一個(gè)父類
{
?public String name; //定義名字
?public float rent; //租金
?public int number; //載人量
?public float weight; //載貨量
?
?public float getRent()
?{
??return rent;
?}
?
?public String getName()
?{
??return name;
?}
?
?public float getWeight()
?{
??return weight;
?}
?
?public int getNumber()
?{
??return number;
?}
}
interface IBus //定義一個(gè)接口,實(shí)現(xiàn)了該接口的類,表示該類具有載人的功能
{
}
interface ITruck //定義一個(gè)接口,實(shí)現(xiàn)了該接口的類,表示該類具有載貨的功能
{
}
class Bus extends Car implements IBus //創(chuàng)建一個(gè)子類,該類繼承了父類Car,實(shí)現(xiàn)了 IBus 接口
{
?public Bus(String name, int number, float rent) //創(chuàng)建構(gòu)造方法
?{
??this.name = name;
??this.number = number;
??this.rent = rent;
?}
?
}
class Truck extends Car implements ITruck //創(chuàng)建一個(gè)子類,該類繼承了父類Car,實(shí)現(xiàn)了 ITruck 接口
{
?public Truck(String name, float weight, float rent) //創(chuàng)建構(gòu)造方法
?{
??this.name = name;
??this.weight = weight;
??this.rent = rent;
?}
}
class BusTruck extends Car implements IBus, ITruck //創(chuàng)建一個(gè)子類,該類繼承了父類Car,實(shí)現(xiàn)了 ITruck 接口和 IBus 接口
{
?public BusTruck(String name, int number, float weight, float rent) //創(chuàng)建構(gòu)造方法
?{
??this.name = name;
??this.number = number;
??this.weight = weight;
??this.rent = rent;
?}
}
class CarRental
{
?private String bus = ""; //定義一個(gè)字符串,用來存放所有載人汽車的名字
?private String truck = ""; //定義一個(gè)字符串,用來存放所有載貨汽車的名字
?private int busNum; //定義一個(gè)整形變量,用來存放所有載人汽車的總載人量
?private float truckWeight; //定義一個(gè)浮點(diǎn)型變量,用來存放把有載貨汽車的總載貨量
?private float sumRent; //定義一個(gè)浮點(diǎn)型變量,用來存放所有汽車的一天租金的總和,注意是一天
?private int date; //定義租用的天數(shù)
?private Scanner sc = null; //定義一個(gè)Scanner工具
?private Car[] c = null; //定義一個(gè)父類類型的數(shù)組,用來接受mian()方法中傳遞過來的參數(shù)
?
?public CarRental(Car[] c) //通過構(gòu)造函數(shù) 使本類中的 c 跟mian()方法中的 c 相關(guān)連
?{
??this.c = c;
?}
?
?public void launch() //該方法由mian()方法調(diào)用
?{
??show();
??menu();
??count();
??output();
?}
?
?public void show() //顯示的信息
?{
??System.out.println("歡迎使用答答租車系統(tǒng):");
??System.out.println("您是否要租車:" + "1 是" + ", 0 否");
??
??sc = new Scanner(System.in); //接收從鍵盤輸入的信息
??while (true)
??{
???int s = sc.nextInt(); //將信息保存到變量s中
???
???if (s!=1 && s!=0) //如果輸入有誤,則重新輸入
???{
????System.out.println("您的輸入有誤!請(qǐng)重新輸入");
????continue;
???}
???else if (s == 0) //當(dāng)輸入0時(shí),程序終止
????System.exit(-1);
???else? //輸入合法,則跳出循環(huán),執(zhí)行接下來的語句
????break;
??}
?}
?
?public void menu() //輸出菜單信息
?{
??System.out.println("\n" + "您可租車的類型及價(jià)目表:");
??System.out.println("***************************");??
??System.out.println("序號(hào)? " + "汽車名稱? " + "租金?? " + "容量");
??System.out.println("1.??? " + "奧迪A4?? " + "500元/天? " + "載人:4人");
??System.out.println("2.??? " + "馬自達(dá)6? " + "400元/天? " + "載人:4人");
??System.out.println("3.??? " + "皮卡雪6? " + "450元/天? " + "載人:4人" + ", 載貨:2噸");
??System.out.println("4.??? " + "金龍???? " + "800元/天? " + "載人:20人");
??System.out.println("5.??? " + "松花江?? " + "400元/天? " + "載貨:4噸");
??System.out.println("6.??? " + "依維柯?? " + "1000元/天 " + "載貨:20噸");
??System.out.println("***************************" + "\n");
?}
?
?public void count() //統(tǒng)計(jì)
?{
??System.out.println("請(qǐng)輸入您要租汽車的數(shù)量:");
??sc = new Scanner(System.in);
??int count = sc.nextInt(); //count 表示用戶要租的汽車的數(shù)量
??
??for(int i=1; i<=count; i++) //使用循環(huán)進(jìn)行統(tǒng)計(jì)
??{
???System.out.println("請(qǐng)輸入第" + i + "輛車的序號(hào):");
???int num = sc.nextInt();
???
???if (num<1 || num>6) //當(dāng)輸入有誤時(shí),程序終止
???{
????System.out.println("您的輸入有誤!程序終止!");
????System.exit(-1);
???}
???else if (3 == num) //當(dāng)用戶輸入3時(shí),對(duì)應(yīng)上面菜單上的 "皮卡雪6",即能載人,也能載貨
???{
????bus = bus + " " + c[num-1].getName(); //將3對(duì)應(yīng)的車的名字存放到bus里面,因?yàn)閏是一個(gè)數(shù)組引用,用其下標(biāo)表示要用 c[num-1]
????busNum = busNum + c[num-1].getNumber();
????sumRent = sumRent + c[num-1].getRent();
????truck = truck + " " + c[num-1].getName();
????truckWeight = truckWeight + c[num-1].getWeight();
???}
???else if (num < 5) //當(dāng)num的值為1,2,4的時(shí)候
???{
????bus = bus + " " + c[num-1].getName();
????busNum = busNum + c[num-1].getNumber();
????sumRent = sumRent + c[num-1].getRent();
???}
???else? //當(dāng)num的值為5,6的時(shí)候
???{
????sumRent = sumRent + c[num-1].getRent();
????truck = truck + " " + c[num-1].getName();
????truckWeight = truckWeight + c[num-1].getWeight();
???}
??}
??
??System.out.println("請(qǐng)輸入租車天數(shù):");
??date = sc.nextInt();
??if (date < 1) //當(dāng)輸入有誤時(shí),程序終止
??{
???System.out.println("您的輸入有誤!程序終止!");
???System.exit(-1);
??}
??sumRent = sumRent * date;? //計(jì)算出所有車的租金總和
?}
?
?public void output() //輸出
?{
??System.out.println("***************************");
??System.out.println("您的賬單:");
??System.out.println("***可載人的車有:" + "\n" + bus + " 共載人:" + busNum + "人");
??System.out.println("***可載貨的車有:" + "\n" + truck + " 共載貨:" + truckWeight + "噸");
??System.out.println("***租車總價(jià)格:" + sumRent + "元");
?}
}
public class TestCarRental ?//啟動(dòng)類
{
?public static void main(String[] args)
?{
??Car[] c = {new Bus("奧迪A4", 4, 500),
?????? new Bus("馬自達(dá)6", 4, 400),
?????? new BusTruck("皮卡雪6", 4, 2, 450),
?????? new Bus("金龍", 20, 800),
?????? new Truck("松花江", 4, 400),
?????? new Truck("依維柯", 20, 1000)
?????? };
??
??new CarRental(c).launch();????
?}
}
2017-04-10
package zuche.com;
public class Pickup extends Car{
?int thingcount;
?int peoplecount;
?public Pickup(String name,String rent,int thingcount,int peoplecount){
??this.name=name;
??this.rent=rent;
??this.peoplecount=peoplecount;
??this.thingcount=thingcount;
?}
?public void setThingcount(int thingcount) {
??this.thingcount = thingcount;
?}
?public int getThingcount() {
??return thingcount;
?}
?public int getPeoplecount() {
??return peoplecount;
?}
?public void setPeoplecount(int peoplecount) {
??this.peoplecount = peoplecount;
?}
?
?
}
package zuche.com;
public class Car {
?String name;
?String rent;
?int peoplecount;
?int thingcount;
?public String getName() {
??return name;
?}
?public String getRent() {
??return rent;
?}
?public int getPeoplecount() {
??return peoplecount;
?}
?public int getThingcount() {
??return thingcount;
?}
}
package zuche.com;
public class PassengerCar extends Car{
?int peoplecount;
?public PassengerCar(String name,String rent,int peoplecount){
??this.name=name;
??this.peoplecount=peoplecount;
??this.rent=rent;
?}
?public int getPeoplecount() {
??return peoplecount;
?}
?public void setPeoplecount(int peoplecount) {
??this.peoplecount = peoplecount;
?}
?
}
package zuche.com;
public class Trunk extends Car{
?int thingcount;
?public Trunk(String name,String rent,int thingcount){
??this.thingcount=thingcount;
??this.name=name;
??this.rent=rent;
??
?}
?public int getThingcount() {
??return thingcount;
?}
?public void setThingcount(int thingcount) {
??this.thingcount = thingcount;
?}
?
}
package zuche.com;
import java.util.Scanner;
public class Test {
?public static void main(String[] args) {
??Car[] carsForRent={new PassengerCar("勞斯萊斯","500元/天",4)
??,new PassengerCar("拉沙馬蒂","600元、天",2)
??,new Pickup("皮卡","300元/天",2,4)
??,new Trunk("松花江","800元/天",5)
??,new Trunk("依維柯","1000元/天",20)
??};
??
??System.out.println("歡迎使用嗒嗒租車行:");
??
??System.out.println("你是否需要租車:1是 0否");
??Scanner scanner=new Scanner(System.in);
??
??String input=scanner.next();
??
??if(input.equals("1")){
???System.out.println("你可租用的車的類型及價(jià)格等");
???System.out.println("序號(hào)\t 汽車名稱\t租金\t載客量\t載重量\t");
???int i=1;
???for(Car c:carsForRent){
????
????if(c instanceof PassengerCar){
????System.out.println(""+i+"\t"+c.getName()+"\t"+c.getRent()
??????+"\t"+c.getPeoplecount()+"人");
????}else if(c instanceof Trunk){
?????System.out.println(""+i+"\t"+c.getName()+"\t"+c.getRent()
???????+"\t"+"\t"+c.getThingcount()+"噸");
????}else{
?????System.out.println(""+i+"\t"+c.getName()+"\t"+c.getRent()
???????+"\t"+c.getPeoplecount()+"人"+"\t"+c.getThingcount()+"噸");
????}
????i++;
???}
??}
??
???System.out.println("請(qǐng)輸入你要租車的序號(hào)");
???@SuppressWarnings("unused")
???int e=scanner.nextInt();
???
??
??
??
?}
?
}