Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        2年前关闭。
                                                                                            
                
        
当我在程序中添加新行程并选择打印所有行程时,它会输出一个空信息,但索引号正确

当我添加两次旅行时:

我的期望是:

Name: customer name
mobile numer : the number

Name: customer name2
mobile numer : the number2


它输出什么:

Name:
mobile numer :

Name:
mobile numer :


那是我的代码

import java.util.*;
class Trip {

    private static String customerName;
    private static String mobileNo;
    private static String reservationCode;
    public static int totalReservations;
    public static Scanner input=new Scanner (System.in);

    public Trip(){//The constructer
        customerName = " ";
        mobileNo = " ";
    }

    public Trip(String Name, String MobileNo) {
        customerName=Name;
        mobileNo=MobileNo;
    }

    public void setcustomerName(String Name){
        customerName = Name;
    }

    public void setmobileNo(String MobileNo){
        mobileNo = MobileNo;
    }

    public String getcustomerName() {
        return customerName;
    }

    public String getmobileNo() {
        return mobileNo;
    }

    public static void printTripInfo(){

        Trip M=new Trip();
        M.getcustomerName();
        M.getmobileNo();

        System.out.println("Customer Name:"+customerName);
        System.out.println("Phone Number:"+mobileNo);

    }

}//end class trip


public class CabCompany{
    private static int reservations_size=20;
    private static Trip[] trip;
    Trip NewTrip;
    public static int user;

    //the scanners
    public static Scanner textinput=new Scanner (System.in);
    public static Scanner input=new Scanner (System.in);

    public static void main(String[]args){
        trip=new Trip[reservations_size];

        do {
            System.out.println("1. Add a new trip\n2. Find a trip by reservation code\n3. List all trips\n");
            user=input.nextInt();

            if ( user == 1) {
                System.out.println("Name:");
                String Name = textinput.nextLine();

                System.out.println("MobileNo:");
                String MobileNo = input.next();

                boolean flag;
                CabCompany Y = new CabCompany();
                flag = Y.addTrip(Name,MobileNo);

                if( flag == true ) {
                    System.out.println("the trip has been added successfully");

                } else if ( flag == false ){
                    System.out.println("the trip hasn't been added");
                }
            }

            if( user == 2) {
                printAll();
            }

            if( user == 3 ){
                System.out.println("The total Reservations is:"+Trip.totalReservations);
            }

        } while (user != 7);
    }


    public boolean addTrip(String customerName,String mobileNo){

        boolean flag = true;

        if ((mobileNo.length() == 10))
        {


            NewTrip=new Trip(customerName, mobileNo);
            Trip.totalReservations++;
            int i = Trip.totalReservations - 1;
            trip[i] = NewTrip;


            flag = true;
        } else {
            flag = false;
        }

        return flag;
    }

    public static void printAll() {

        for(int t=0; t<Trip.totalReservations; t++){
            trip[t].printTripInfo();

        }
    }
}

最佳答案

public static void printAll() {
    for(int t=0; t<Trip.totalReservations; t++){
        trip[t].printTripInfo();
    }
}


在这里,您要在数组的每个实例上调用printTripInfo()方法,但是您没有使用该实例。而是每次调用Trip方法时都在创建printTripInfo()的新实例。
所以你需要改变你的方法

public static void printTripInfo(){
    Trip M=new Trip();
    M.getcustomerName();
    M.getmobileNo();
    System.out.println("Customer Name:"+customerName);
    System.out.println("Phone Number:"+mobileNo);
}




public void printTripInfo(){
    System.out.println("Customer Name:"+ this.getcustomerName());
    System.out.println("Phone Number:"+ this.getmobileNo());
}


但是,当您尝试通过上述更改运行代码时,它将根本无法编译。因为您试图从非静态方法访问静态字段customerNamemobileNo。因此,您还需要将customerNamemobileNo字段更改为非静态字段。

10-06 13:36
查看更多