Java .. 给定公共(public)类并且没有 Main 方法作为小组项目的一部分.. 我该如何编译?

标签 java eclipse class methods program-entry-point

参加在线介绍 Java 类(class)时,我 react 迟钝的教授为我们提供了“公共(public)类(class)航空公司”来纠正错误、提高效率、添加功能等。仅提供了 .txt 文件。到目前为止,在类里面我所有的代码都是在 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

'''

最佳答案

这是一个有效的 java 源文件,因此只需将其重命名为 Airplane.java (公共(public)类必须始终位于以其自身命名的源文件中;假设它包含 public class Airplane,因此源文件必须准确命名为 Airplane.java,并注意大小写!

它没有 main 方法,但这没问题,你仍然可以编译它。您根本无法运行它(尝试执行 java Airline 会产生错误:找不到 main 方法)。您可以创建另一个使用 Airline 类的类,或者,您可以向 Airline 类添加一个 main 方法。

关于Java .. 给定公共(public)类并且没有 Main 方法作为小组项目的一部分.. 我该如何编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58985984/

相关文章:

java - 杜克快速重复数据删除 : java. lang.UnsupportedOperationException : Operation not yet supported?

android - 在 Eclipse ADT 中生成 Gradle 时出错

eclipse - 如何使 JUnit4 + Hamcrest 1.3 + Mockito 从 Eclipse 和 Tycho 工作

java - 如果类是类型,那么它们可以保存什么类型的数据?

c# - 是否可以在运行时获取类摘要?

java - paraminterceptor如何自行从String转换为integer?

java - ArrayList 的索引

C#,结构与类,更快?

java - 使用 Java 访问 Google Directory API

java - 如何在三个不同的 Java IDE 上检查已安装的 JAR、外部库等?