参加在线介绍 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/