如何正確傳值呢 求指點 謝謝啦~~
//父類
package ThirdEx;
public abstract class One{
?? ?
?? ?public static int number;
?? ?public static int carMoney;
?? ?public int no;
?? ?public String carName;
?? ?public static int weight;
?? ?public void Aodi() {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?System.out.println(no+"."+"??? "+carName+"??? "+carMoney+"/day"+"??? "+"can carry person:"+number);
?? ?}
?? ?public void Mazida() {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?System.out.println(no+"."+"??? "+carName+"??? "+carMoney+"/day"+"??? "+"can carry person:"+number);
?? ?}
?? ?public void PiKaxue() {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?System.out.println(no+"."+"??? "+carName+"??? "+carMoney+"/day"+"??? "+"can carry person:"+number+"??? "+"can carry weight:"+weight+"t");
?? ?}
?? ?public void JinLong() {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?System.out.println(no+"."+"??? "+carName+"??? "+carMoney+"/day"+"??? "+"can carry person:"+number);
?? ?}
?? ?public void SongHuaJiang() {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?System.out.println(no+"."+"??? "+carName+"??? "+carMoney+"/day"+"??? "+"can carry weight:"+weight+"t");
?? ?}
?? ?public void YiWeiHe() {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?System.out.println(no+"."+"??? "+carName+"??? "+carMoney+"/day"+"??? "+"can carry weight:"+weight+"t");
?? ?}
}
//Aodi類
package ThirdEx;
public class Aodi extends One {
?? ?public Aodi(){
???????? no = 1;
?? ??? ?carName = "Aodi";
?? ??? ?carMoney = 500;
?? ??? ?number = 4;
?? ?}
}
//Mazida類
package ThirdEx;
public class Mazida extends One {
?? ?public Mazida(){
?? ???? no=2;
?? ??? ?carName="Mazida";
?? ??? ?carMoney=400;
?? ??? ?number=4;
?? ?}
}
//PiKaxue類
package ThirdEx;
public class PiKaxue extends One {
?? ?public PiKaxue(){
?? ??? ?no=3;
?? ??? ?carName="PiKaxue";
?? ??? ?carMoney=450;
?? ??? ?number=4;
?? ??? ?weight=2;
?? ?}
}
還有其他車的類都一樣的寫法 就不貼上來了
//測試類
package ThirdEx;
import java.util.Scanner;
public class Initial {
?? ?private static int b;
?? ?/**
?? ? * @param args
?? ? */
?? ?public static void main(String[] args) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?System.out.println("Welcome to DADA Rent Car!If you want to rent a car?" );
?? ??? ?System.out.println("1.Yes??? 0.No");
?? ??? ?
?? ??? ?Scanner input =new Scanner(System.in);
?? ??? ?int a=input.nextInt();
?? ??? ?if(a==0){
?? ??? ??? ?System.out.println("Thank you!Good bye!");
?? ??? ?}else if(a==1){
?? ??? ??? ?System.out.println("You can use these cars:");
?? ??? ??? ?
?? ??? ??? ?Aodi carA=new Aodi();
?? ??? ??? ?carA.Aodi();
?? ??? ??? ?
?? ??? ??? ?Mazida carM=new Mazida();
?? ??? ??? ?carM.Mazida();
?? ??? ??? ?
?? ??? ??? ?PiKaxue carP=new PiKaxue();
?? ??? ??? ?carP.PiKaxue();
?? ??? ??? ?
?? ??? ??? ?JinLong carJ=new JinLong();
?? ??? ??? ?carJ.JinLong();
?? ??? ??? ?
?? ??? ??? ?SongHuaJiang carS=new SongHuaJiang();
?? ??? ??? ?carS.SongHuaJiang();
?? ??? ??? ?
?? ??? ??? ?YiWeiHe carY=new YiWeiHe();
?? ??? ??? ?carY.YiWeiHe();
?? ??? ??? ?
?? ??? ??? ?System.out.println("How much cars do you want?");
?? ??? ??? ?
?? ??? ??? ?Scanner input11=new Scanner(System.in);
?? ??? ??? ?b = input11.nextInt();
?? ??? ??? ?int sum=0;
?? ??? ??? ?int people=0;
?? ??? ??? ?int weight=0;
?? ??? ??? ?for(int i=1;i<=b;i++){
?? ??? ??? ??? ?
?? ??? ??? ?System.out.println("Please enter the "+i+" car's No.");
?? ??? ??? ?Scanner inputi=new Scanner(System.in);
?? ??? ??? ?int id=input.nextInt();
?? ?
?? ??? ??? ?if(id==1){
?? ??? ??? ??? ? sum=sum+Aodi.carMoney;
?? ??? ??? ??? ? people=people+Aodi.number;
?? ??? ??? ? }
?? ??? ??? ? if(id==2){
?? ??? ??? ??? ? sum=sum+Mazida.carMoney;
?? ??? ??? ??? ? people=people+Mazida.number;
?? ??? ??? ? }
?? ??? ??? ? if(id==3){
?? ??? ??? ??? ? sum=sum+PiKaxue.carMoney;
?? ??? ??? ??? ? people=people+PiKaxue.number;
?? ??? ??? ??? ? weight=weight+PiKaxue.weight;
?? ??? ??? ? }
?? ??? ??? ? if(id==4){?? ??? ??? ??? ?
?? ??? ??? ??? ? sum=sum+JinLong.carMoney;
?? ??? ??? ??? ? people=people+JinLong.number;
?? ??? ??? ? }
?? ??? ??? ? if(id==5){?? ??? ??? ??? ?
?? ??? ??? ??? ? sum=sum+SongHuaJiang.carMoney;
?? ??? ??? ??? ? weight=weight+SongHuaJiang.weight;;
?? ??? ??? ? }
?? ??? ??? ? if(id==6){?? ??? ??? ??? ?
?? ??? ??? ??? ? sum=sum+YiWeiHe.carMoney;
?? ??? ??? ??? ? weight=weight+YiWeiHe.weight;
?? ??? ??? ? }? ??? ????
?? ??? ??? ?}
?? ??? ??? ?? ??? ??? ?
?? ??? ??? ?
?? ??? ??? ?System.out.println("Plaese enter the days do you want to rent?");
?? ??? ??? ?Scanner input12=new Scanner(System.in);
?? ??? ??? ?int c1=input12.nextInt();
?? ??? ??? ?
?? ??? ??? ?System.out.println("Your pill is:");
?? ??? ??? ?System.out.println("can carry "+ people +"people");
?? ??? ??? ?System.out.println("can carry "+ weight +"weight");
?? ??? ??? ?int sumTotil = sum*c1;
?? ??? ??? ?System.out.println("All money:"+sumTotil);
?? ??? ?}????
?? ?}
}
我測試的結(jié)果就是錯的
為什么傳人數(shù)、車錢的值都是似乎是地址值而不是我賦的值呢 求指點
2017-10-25
cars 用 many。。。
2017-08-25
你最后的輸出輸出的是啥!