내 프로그램이 실행되지만 논리적 인 실수가있어서 찾을 수 없습니다. 나는 이름과 모든 물건을 입력하지만 여행을 인쇄하도록 선택할 때 출력을 단지 전화 번호와 여기프로그램이 아무것도 인쇄하지 않습니다 (객체 및 클래스)
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();
}
}
}
힌트 : 우리가 ** 당신을 돕기 위해 우리 시간을 보내길 원합니다 **. 따라서 모든 코드를 적절하게 포맷/인 들기위한 시간을 보내십시오. 코드 품질에 대한 참고 사항 : 모든 종류의 블록을 중심으로 {중괄호}를 사용하십시오. 단 한 줄이라도 if/then if/else 일을 사용하는 것이 좋습니다. 그리고 iterating하고 elses에서 휴식을 ... 좀 추한. "깨끗한 코드"에 대해 조금 읽으 려합니다. 귀하의 코드는 그로부터 이익을 얻을 수 있습니다 ... – GhostCat
좋은 조언, 나는 당신을 감사 할 것입니다 – Dipie
그건 프로그래밍 문제에 관한 질문이 아니에요, 프로그래밍 문제 ("내가 뭘하는지 모르겠어, 어떻게 자바 프로그램에? "). 제발, 자바가 작동하는 방법을 배우십시오, 자바에서 프로그래밍에 대한 책이나 튜토리얼을 읽어서 이것을하십시오. SO에서 프로그램하는 방법을 배우려고 시간을 낭비하지 마십시오. – zubergu