亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

我寫到這不知道咋辦,如何才能輸出載人載貨的車名 求助大佬

package?com.答答租車系統;
import?java.util.*;
public?class?Car?{???//父類
	public?int?id;
	public?String?name;
	public?String?price;
	public?Car(int?id,String?name,String?price)?{
		this.id=id;
		this.name=name;
		this.price=price;
		System.out.print(id+"\t"+name+"\t"+price+"\t");
		}
	
	}
	
	
public?class?Huocar?extends?Car??{//?子類貨車
????public?String?zaihuo;
????public?Huocar(int?id,String?name,String?price,String?zaihuo)?{
		super(id,name,price);
		this.zaihuo=zaihuo;
		System.out.println(zaihuo+"\t");
	}
}

public?class?Kecar?extends?Car??{//子類客車
	public?String?zairen;
????public?Kecar(int?id,String?name,String?price,String?zairen)?{
	super(id,name,price);
	this.zairen=zairen;
	System.out.println(zairen+"\t");
}

}


public?class?Picar?extends?Car??{//子類皮卡
????public?String?zaihuo;	
	public?String?zairen;	
	public?Picar(int?id,String?name,String?price,String?zairen,?String?zaihuo	)?{
		super(id,name,price);
		this.zaihuo=zaihuo;
		this.zairen=zairen;
		System.out.print(zairen+"\t");
		System.out.println(zaihuo+"\t");
	}
}

public?class?Main?{
????????public?static?void?main(String[]?args)?{
		System.out.println("歡迎使用答答租車系統");
		System.out.println("您是否需要租車?:??1是???0否");
		Scanner?input=new?Scanner(System.in);
		int?judge1=input.nextInt();//獲取輸入值
		if(judge1==1)
		{
			System.out.println("序號\t車輛名稱\t?租車價格\t?容量\t");
			Car?car[]=?{new?Kecar(1,"奧迪A4","500元/天","載人:4人"),
			new?Kecar(2,"馬自達6","400元/天","載人:4人"),
			new?Picar(3,"皮卡雪","450元/天","載人:4人","載重:2噸"),
			new?Kecar(4,"金龍","800元/天","載人:20人"),
			new?Huocar(5,"松花江","400元/天","載重:4噸"),
			new?Huocar(6,"依維柯","1000元/天","載重:20噸")};//構造方法輸出信息
			/*System.out.println("序號"+"\t"+"車輛名稱"+"\t"+"租車價格"+"\t"+"?容量"+"\t");
			Car?car1=new?Kecar(1,"奧迪A4","500元/天","載人:4人");
			Car?car2=new?Kecar(2,"馬自達6","400元/天","載人:4人");
			Car?car3=new?Picar(3,"皮卡雪","450元/天","載人:4人","載重:2噸");
			Car?car4=new?Kecar(4,"金龍","800元/天","載人:20人");
			Car?car5=new?Huocar(5,"松花江","400元/天","載重:4噸");
			Car?car6=new?Huocar(6,"依維柯","1000元/天","載重:20噸");*/
		}else
		{
			System.out.println("如有需要歡迎您的使用,再見");
			System.exit(0);
		}
		System.out.println("請輸入您要租汽車的數量");
		int?sum=input.nextInt();
		int?s;
		String?zr[]=new?String[sum];
		int?weight=0;//計算總載重
		int?peosum=0;//計算總人數
		int?monsum=0;//計算總錢數
		for(int?i=0;i<sum;i++)?{
			System.out.println("請輸入第"+(i+1)+"輛租車的序號");
			?s=input.nextInt();??//保存各輛車
			?switch?(s)?{
			?case?1:?peosum=peosum+4;monsum=monsum+500;zr[i]="奧迪A4";continue;
			?case?2:?peosum=peosum+4;monsum=monsum+400;zr[i]="馬自達6";continue;
			?case?3:?peosum=peosum+4;monsum=monsum+450;zr[i]="皮卡雪6";weight=weight+2;continue;
			?case?4:?peosum=peosum+20;monsum=monsum+800;zr[i]="金龍";continue;
			?case?5:?monsum=monsum+400;weight=weight+4;zr[i]="松花江";continue;
			?case?6:?monsum=monsum+1000;weight=weight+20;zr[i]="依維柯";continue;
			?
			?}
					}
		System.out.println("請輸入您要租車的天數");
			int?daysum=input.nextInt();
			int?monsums=daysum*monsum;//總金額
		????
			System.out.println("您的賬單");
			System.out.println("****可載人的車有"+"\n"+"??共載人?:"+peosum);
			System.out.println("****可載貨的車有"+"\n"+"??共載貨??:"+weight+"噸");
			System.out.println("****租車總價格?:?"?+??monsums);
			System.out.println("歡迎再次使用");
			}

}


正在回答

4 回答

你所租的車的車名放在了String zr[sum]這個數組中,可以輸出zr[]這個數組中的值從而輸出車名。由于你把車分成了載人和載貨的所以要在輸出車名的同時進行劃分,這個可以使用String中間涉及判斷語句,所以要改的話需要把最終的倒數三,四syso都改一下。

代碼部分:System.out.println("您的賬單:");

? ? ? ? ? ? ? ? ? System.out.println("****可載人的車有:");

? ? ? ? ? ? ? ? ? for(int i=0;i<sum;i++)

? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ?if(zr[i]==Car[0].name||zr[i]==Car[1].name||zr[i]==Car[2].name||zr[i]==Car[3].name)

? ? ? ? ? ? ? ? ? ? ? ?System.out.println(zr[i]);

? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ?System.out.println("\n共載人:"+peosum);

? ? ? ? ? ? ? ? ?System.out.println("****可載貨的車有:");

? ? ? ? ? ? ? ? ? for(int i=0;i<sum;i++)

? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ??if(zr[i]==Car[2].name||zr[i]==Car[4].name||zr[i]==Car[5].name)

? ? ? ? ? ? ? ? ? ??System.out.println(zr[i]);

? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ?System.out.println("\n共載貨:"+weight+"噸");

直接寫的,沒有用編譯器,可能存在一些細節問題,思路是這樣的。這是最直接的方法,但是個人不推薦這樣做,比較String變量不如比較int變量。

比較int變量的思路:zr數組改為int數組,switch循環中將車子的序號存進zr數組中,或者刪除這一步在輸入租車序號后將序號存進zr數組中,之后使用if語句和Car數組中id屬性比較,相等的話輸出Car數組中的name屬性(也就是車名)。

0 回復 有任何疑惑可以回復我~
#1

qq_枯笑_0 提問者

謝謝大神,我也看過一些慕友的代碼,每次看到 if ( * instanceof *)這個用判斷類型這里我就看不懂了
2017-08-06 回復 有任何疑惑可以回復我~
#2

qq_枯笑_0 提問者

再次感謝大神,我在這個問題都糾結一天了,感覺瞬間豁然開朗
2017-08-06 回復 有任何疑惑可以回復我~
#3

qq_枯笑_0 提問者

我還有個問題請教下,就是按照這樣寫輸出來就可能包含兩個相同的名字,如果我租的車都是一樣的,那最后結果就會出現重復
2017-08-07 回復 有任何疑惑可以回復我~

應用List

CarProperty.java//父類

package zuchexitong;


public class CarProperty {

public String carName;//車輛名稱

public String carType;//車輛類型

public int carZaiKe;//載客量

public int carZaiHuo;//載貨量

public int carZuJin;//每天租金

public CarProperty(){

}

//構造方法

public CarProperty(String carN,String carT,int carZk,int carZh,int carZj){

carName=carN;

carType=carT;

carZaiKe=carZk;

carZaiHuo=carZh;

carZuJin=carZj;

}

}

HuoChe.java//子類

package zuchexitong;


public class HuoChe extends CarProperty{


}

KeChe.java//子類

package zuchexitong;


public class KeChe extends CarProperty{



}

PiKar.java//子類

package zuchexitong;


public class PiKar extends CarProperty{

}

Test//測試類


package zuchexitong;


import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

import java.util.Scanner;


import com.CarProperty;


import test.Course;

/**

?* 應用了list的方法調用

?* 需要添加的有List的變量

?* 定義泛型

?* 構造類的構造器

?* 添加testAdd方法

?* 添加testForEach方法

?* @author Administrator

?*

?*/

public class Test2 {

public List<CarProperty> carProperties;

public Test2(){//構造器

carProperties=new ArrayList<CarProperty>();

}

public void testAdd(){

CarProperty[] cars={new CarProperty("1,奧迪", "貨車", 2, 3, 100),new CarProperty("2,大眾", "貨車", 2, 2, 80),

new CarProperty("3,寶馬", "客車", 4, 0, 150),new CarProperty("4,一汽", "客車", 4, 0, 100),

new CarProperty("5,五菱", "兩用", 8, 2, 250),new CarProperty("6,奔馳", "兩用", 2, 3, 160)};

carProperties.addAll(Arrays.asList(cars));

CarProperty temp=(CarProperty)carProperties.get(0);

CarProperty temp2=(CarProperty)carProperties.get(0);

CarProperty temp3=(CarProperty)carProperties.get(2);

CarProperty temp4=(CarProperty)carProperties.get(3);

CarProperty temp5=(CarProperty)carProperties.get(2);

CarProperty temp6=(CarProperty)carProperties.get(3);

}

public void testForEach(){

for(Object obj:carProperties){

CarProperty cr=(CarProperty)obj;

System.out.println("課程:"+cr.carName+"\t"+cr.carType+"\t"+

cr.carZaiHuo+"\t"+cr.carZaiKe+"\t"+cr.carZuJin);

}

}

public static void main(String[] args) {

Test2 t2=new Test2();


int sum=0;

System.out.println("請選擇是否租車?(1,是 ? 2,否):");

Scanner input=new Scanner(System.in);

int zchy=input.nextInt();

if (zchy==1) {

System.out.println("車輛名稱\t車輛類型\t載客量/人\t載貨量/噸\t租金/每天");

t2.testAdd();

t2.testForEach();

System.out.println("請選擇租車數量:");

Scanner input2=new Scanner(System.in);

int zcsl=input2.nextInt();

for (int i = 1; i < zcsl+1; i++) {

System.out.println("請輸入要租用的第幾輛車的編號:");

Scanner input3=new Scanner(System.in);

int zcbh=input3.nextInt();

switch (zcbh) {

case 1:

sum+=100;

break;

case 2:

sum+=80;

break;

case 3:

sum+=150;

break;

case 4:

sum+=100;

break;

case 5:

sum+=250;

break;

case 6:

sum+=160;

break;


default:

System.out.println("輸入錯誤,請重新輸入!");

break;

}

System.out.println("請輸入租用天數:");

Scanner input4=new Scanner(System.in);

int zcts=input4.nextInt();

sum=sum*zcts;

System.out.println("總租車輛數:"+zcsl+"輛\t租用天數:"+zcts+"天\t總金額:¥"+sum);

}

}

}

}



0 回復 有任何疑惑可以回復我~

在比較String變量的過程中最好使用equals(); 函數

0 回復 有任何疑惑可以回復我~

你想輸出哪種形式的呢?如果想單純的想輸出租的車的名字可以的話可以將zr[]數值中的值先進行處理,剔除相同的值。然后再進入for循環輸出。

剔除方法:將zr[]數組中的值依次與之后的值相比較,相同的時候將zr[]當前值賦值為空,然后之后的數組序號前移。

0 回復 有任何疑惑可以回復我~
#1

qq_枯笑_0 提問者

我這樣寫出來,有一個問題運行會出現一堆哈希碼
2017-08-09 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

我寫到這不知道咋辦,如何才能輸出載人載貨的車名 求助大佬

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號