我的程序运行,但这是一个逻辑错误,我无法找到它。我输入了名称和所有内容,但我认为他们没有保存,因为当我选择打印行程时,它只保留输出电话号码,并且名称打印为空
import java.util.*;
class Trip{
private static String customerName;
private static String mobileNo;
private static String reservationCode;
public static int totalReservations=0;
public static Scanner input=new Scanner (System.in);
public Trip(){//The constructer
customerName = " ";
mobileNo = " ";
}
public Trip(String Name, String MobileNo) {
customerName=Name;
mobileNo=MobileNo;
totalReservations++; //increase the total reservations
}
public void setcustomerName(String Name){
customerName = Name;
}
public void setmobileNo(String MobileNo){
mobileNo = MobileNo;
}
//Getters
public String getcustomerName() {
return customerName;
}
public String getmobileNo() {
return mobileNo;
}
public String getreservationCode() {
return reservationCode;
}
public static void generateReservationCode(){
}
public static void printTripInfo(){
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 int totalNumber=0;
public static int user;
public static Scanner input=new Scanner (System.in);
public static void main(String[]args){//MAIN
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 = input.nextLine();
input.nextLine();
System.out.println("MobileNo:");
String MobileNo = input.next();
CabCompany Y = new CabCompany();
boolean flag;
flag = Y.addTrip(Name,MobileNo);
if( flag == true) {
System.out.println("the trip has been added succssfully");
} else if (flag == false){
System.out.println("hasn't been added");
}
}
if( user == 2) {
printAll();
}
if( user == 3 ){
System.out.println("The total Reservations is:"+Trip.totalReservations);
}
} while (user != 7);//BREAK
}
public boolean addTrip(String customerName,String mobileNo){
boolean flag = true;
if ((mobileNo.length() == 10)) {
newTrip=new Trip(customerName, mobileNo);
int i = Trip.totalReservations - 1;
newTrip.generateReservationCode();
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();
}
}
}
答案 0 :(得分:0)
好的,这里有好几件事。要回答你的问题,我想你是在谈论方法
public static void printTripInfo(){
Trip rr=new Trip();
CabCompany K=new CabCompany();
for(int y=0;y<2;y++)
System.out.println("Customer Name:"+rr.getcustomerName());
System.out.println("Phone Number:"+rr.getmobileNo());
}
在这里,在第一行,您创建一个新的空行程(rr),您打印。这个方法应该是:
public static void printTripInfo(){
System.out.println("Customer Name:"+getcustomerName());
System.out.println("Phone Number:"+getmobileNo());
}
此外,还有一个可用于此的内置方法,称为toString()。其他注意事项:变量名称不应该以大写字母开头,并且应该具有含义(TYY,Y,...,不是好名字),并且可以删除和替换您的第一个构造函数,如下所示:
class Trip{
private String customerName="";
private String mobileNo="";
private String day="";
private String date="";
private String time="";
...
...
但是,您的代码中还有一些其他问题,我建议您阅读更多关于java dev或者关注教程的内容;)
答案 1 :(得分:0)
在printTripInfo()方法中,您正在创建一个新的Trip对象 将永远是空的: -
public static void printTripInfo(){
//Creating a new Trip object, which is empty
Trip rr=new Trip();
//Printing the values from above object, which will be blank
System.out.println("Customer Name:"+rr.getcustomerName());
System.out.println("Phone Number:"+rr.getmobileNo());
}