為什么我最后沒(méi)有累加呀,每次都是只能輸出最后一次的車輛的錢?求大佬指點(diǎn)?。?/h1>
package callcarapp;
import java.util.Scanner;
public class runapp {
?public static void main(String[] args) {
??int totalprice=0;
??int totalvolum=0;
??int totalweight=0;
?System.out.println("歡迎使用答答租車系統(tǒng):");
?System.out.println("您是否要租車:1是 0否");
?var input=new Scanner(System.in);
?int x=input.nextInt();
?zairen c1=new zairen("奧迪A4",500,4);
?zairen c2=new zairen("馬自達(dá)6",400,4);
?zairhh c3=new zairhh("皮卡6",450,40,2);
?zairen c4=new zairen("金龍",800,20);
?zaihuo c5=new zaihuo("松花江",400,4);
?if(x==1)
?{
??System.out.println("您可租車的類型及其價(jià)目表如下:");
??c1.show();
??c2.show();
??c3.show();
??c4.show();
??c5.show();
??System.out.println("您租車的數(shù)量為:");
??Scanner input1=new Scanner(System.in);
??int num=input1.nextInt();
??for(int i=1;i<=num;i++)
??{
???System.out.println("請(qǐng)輸入第"+i+"輛車的序號(hào):");
???Scanner input2=new Scanner(System.in);
???int cnum=input2.nextInt();
???switch(cnum)
???{
???case 1:{totalprice=+c1.getPrice();
??totalvolum=+c1.getVolume();
???break;}
???
???case 2:{totalprice=+c2.getPrice();
???totalvolum=+c2.getVolume();
???break;}
???
???case 3:{totalprice=+c3.getPrice();
???totalvolum=+c3.getVolume();
???totalweight=+c3.getWeight();break;}
???
???case 4:{totalprice=+c4.getPrice();
???totalvolum=+c4.getVolume();break;}
???
???case 5:{totalprice=+c5.getPrice();
???totalweight=+c5.getWeight();break;}
???default:break;
???}
??}
??System.out.println("您的賬單如下:");
??System.out.println("總價(jià)為:"+totalprice+"總?cè)藬?shù)為:"+totalvolum);
?}
}
}
package callcarapp;
import java.util.Scanner;
public class runapp {
?public static void main(String[] args) {
??int totalprice=0;
??int totalvolum=0;
??int totalweight=0;
?System.out.println("歡迎使用答答租車系統(tǒng):");
?System.out.println("您是否要租車:1是 0否");
?var input=new Scanner(System.in);
?int x=input.nextInt();
?zairen c1=new zairen("奧迪A4",500,4);
?zairen c2=new zairen("馬自達(dá)6",400,4);
?zairhh c3=new zairhh("皮卡6",450,40,2);
?zairen c4=new zairen("金龍",800,20);
?zaihuo c5=new zaihuo("松花江",400,4);
?if(x==1)
?{
??System.out.println("您可租車的類型及其價(jià)目表如下:");
??c1.show();
??c2.show();
??c3.show();
??c4.show();
??c5.show();
??System.out.println("您租車的數(shù)量為:");
??Scanner input1=new Scanner(System.in);
??int num=input1.nextInt();
??for(int i=1;i<=num;i++)
??{
???System.out.println("請(qǐng)輸入第"+i+"輛車的序號(hào):");
???Scanner input2=new Scanner(System.in);
???int cnum=input2.nextInt();
???switch(cnum)
???{
???case 1:{totalprice=+c1.getPrice();
??totalvolum=+c1.getVolume();
???break;}
???
???case 2:{totalprice=+c2.getPrice();
???totalvolum=+c2.getVolume();
???break;}
???
???case 3:{totalprice=+c3.getPrice();
???totalvolum=+c3.getVolume();
???totalweight=+c3.getWeight();break;}
???
???case 4:{totalprice=+c4.getPrice();
???totalvolum=+c4.getVolume();break;}
???
???case 5:{totalprice=+c5.getPrice();
???totalweight=+c5.getWeight();break;}
???default:break;
???}
??}
??System.out.println("您的賬單如下:");
??System.out.println("總價(jià)為:"+totalprice+"總?cè)藬?shù)為:"+totalvolum);
?}
}
}
2019-07-17
=+ 是賦值一個(gè)正數(shù);
+= 是相加后賦值;
代碼語(yǔ)法再看看。