簡體   English   中英

Java.. 給定公共 class 並且沒有 Main 方法作為組項目的一部分.. 我該如何編譯?

[英]Java .. Given public class and no Main method as part of a group project .. how do I compile?

采取在線介紹 Java class 和我反應遲鈍的教授向我們提供了“公共 class 航空公司”來糾正錯誤,提高效率,添加功能等。只有 .txt 文件到目前為止,在 class 中,我已經在 Eclipse 中編寫了我自己的所有代碼。 如何獲取沒有 main 方法的提供的.txt 文件並嘗試編譯它以便我可以處理作業?

感謝您的任何意見。 麥克風

'''

import java.util.Scanner;

public class Airline
{

    boolean seats[]; // array of seats
    final int SIZE = 10;  // number of seats
    final int FIRST_CLASS = 1;
    final int ECONOMY_CLASS = 2;

   public Airline()
   {
      seats = new boolean[ SIZE ]; // array of seats
   }

   // prints boarding pass
   public void printBoardingPass( int seat )
   {
      String section = ( seat < 5 ) ? "First Class" : "Economy Class";
      System.out.printf( "%s. Seat #%d\n", section, seat );
   }

   // print seating menu
   public void printMenu()
   {
      System.out.println( "Please type 1 for First Class" );
      System.out.println( "Please type 2 for Economy" );
      System.out.print( "choice: " );
   }

   // checks customers in and assigns them a boarding pass
   public void checkIn()
   {      
      int firstClass = 0; // next available first class seat
      int economy = 5; // next available economy seat      
      int section = 0; // passenger's seating choice

      Scanner input = new Scanner( System.in );

      System.out.println( "Welcome to Java Airways" );

      while ( ( firstClass < 5 ) || ( economy < 10 ) )
      {
         printMenu();
         section = input.nextInt();

         if ( section == FIRST_CLASS ) // user chose first class
         {
            if ( firstClass < 5 )
            {
               seats[ firstClass++ ] = true;
               printBoardingPass( firstClass );
            } // end if 
            else if ( economy < 10 ) // first class is full
            {
               System.out.println(
                  "First Class is full, Economy Class?" );
               System.out.print( "1. Yes, 2. No. Your choice: " );
               int choice = input.nextInt();

               if ( choice == 1 )
               {
                  seats[ economy++ ] = true;
                  printBoardingPass( economy );
               }
               else
                  System.out.println( "Next flight leaves in 3 hours." );
            } // end else if
         } // end if
         else if ( section == ECONOMY_CLASS ) // user chose economy
         {
            if ( economy < 10 ) 
            {
               seats[ economy++ ] = true;
               printBoardingPass( economy );
            } // end if 
            else if ( firstClass < 5 ) // economy class is full
            {
               System.out.println(
                  "Economy Class is full, First Class?" );
               System.out.print( "1. Yes, 2. No. Your choice: " );
               int choice = input.nextInt();

               if ( choice == 1 )
               {
                  seats[ firstClass++ ] = true;
                  printBoardingPass( firstClass );
               } // end if
               else
                  System.out.println( "Next flight leaves in 3 hours." );
            } // end else if

         } // end else if

         System.out.println();

      } // end while

      System.out.println( "The plane is now full." );      

   } // end method checkIn

} // end class Airline

'''

It's a valid java source file, so just rename it to Airplane.java (public classes must always be in a source file named after itself; given that this contains public class Airplane , the source file must therefore be named Airplane.java precisely like that ,注意外殼!

它沒有 main 方法,但沒問題,你仍然可以編譯它。 您根本無法運行它(嘗試執行java Airline會產生錯誤: no main method found )。 您可以制作另一個使用Airline class 的 class,或者,您可以將主要方法添加到航空公司 class。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM