应该做什么的returnBooking()方法呢?

时间:2014-04-06 08:00:11

标签: java class return

我从其他课程调用后,我尝试返回上一课但是出现了这个错误,下面是我的代码

public class Train extends Booking {
  public Train returnBooking(){
     int Tid,dest;
     Date now = new Date();
     Scanner sc= new Scanner(System.in);
     Scanner sc2=new Scanner(System.in);
 try{     
     System.out.println("Enter train ID");
     System.out.println("(**Example '1001'**)");
     Tid=sc.nextInt();
     System.out.println("1. Malacca Zoo to Night Safari");
     System.out.println("2. Malacca Zoo to River Safari");
     System.out.println("3. River Safari to Malacca Zoo");
     System.out.println("Enter a destination for the train");
     dest = sc.nextInt();
     System.out.println("Enter date: ");
     String date = sc2.nextLine();

     SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");  
     Date testDate = null;  


     try{

        testDate = df.parse(date);    
    }catch(ParseException e){ System.out.println("invalid format");} 
    if (!df.format(testDate).equals(date)){  
        System.out.println("invalid date!!");  
    } else {  
        System.out.println("valid date");  
    } 
}catch(InputMismatchException e){System.out.print("Input must be numbers only");}
return Booking.class;**//imcompatible type**
 }
}

我的退货方法错了吗?或者只是一些我想念的简单错误?

这是我的预订课程代码

public class Booking{
public static void main(String[]agrs){
   Scanner sc = new Scanner(System.in);

   int sel,book_ticket, reschedule, cancel, add, remove, display, total, loop;

   System.out.println("\t\t\t\t\t****************************************************\t\t\t\t\t");
   System.out.println("\t\t\t\t\t*              Welcome to Malacca Zoo              *\t\t\t\t\t");
   System.out.println("\t\t\t\t\t*  This is our new online train reservstion system *\t\t\t\t\t");
   System.out.println("\t\t\t\t\t*  follow the instruction at below to perform your *\t\t\t\t\t");
   System.out.println("\t\t\t\t\t* reservation, hope you will enjoy our new services*\t\t\t\t\t");
   System.out.println("\t\t\t\t\t****************************************************\t\t\t\t\t");
   System.out.println("\n\n1.Select and book a ticket ");
   System.out.println("2.Reschedule a train");
   System.out.println("3.Cancel a reservation");
   System.out.println("4.Add new train service");
   System.out.println("5.Remove a train from service");
   System.out.println("6.Display total number of passengers");
   System.out.println("7.Total profit from a service");
   System.out.println("0.Exit");
   System.out.println("\nEnter the number to select the function that you want to perform");

//try {
   sel=sc.nextInt();
   for(loop=0;loop<2;loop++)   {
      if (sel==0)
      {
          System.exit(0);  
      }
      else if(sel == 4)
      {
         new Train();
      }
      else
      {
           System.out.println("You enter an invalid number. Please try again");
           sel=sc.nextInt();
           loop=0;
       }
    }
 // } catch (InputMismatchException e){
 // } finally {
 // }
 // System.out.print("Input must be a number between 1 and 0: ");
}
}

2 个答案:

答案 0 :(得分:0)

您在返回Train时定义了返回class类型的方法。

尝试将代码更改为以下内容:

public Train returnBooking(){
    //code
    Train trainObject = new Train();
    // code to fill the Train object.
    return new trainObject();
}

或者只是不返回任何内容,因为您不对任何Train对象进行任何处理。

return null;

或者我建议您将方法声明为void,因为您似乎没有对Train对象做任何事情。您甚至没有声明TrainBooking类中的任何对象。

public void returnBooking(){
    //code
}

答案 1 :(得分:0)

在提出问题之前,请确保您对java有正确的了解!!

如何返回java.lang.Class类型而不是类型为Train ???的java.lang.Object

Definetly编译器会用你的代码哭泣。

相关问题