[英]For the following java demo, how would I make it so the methods would not be static?
当我尝试简单地删除static
它给了我错误消息,并且我不知道如何创建演示类的实例。 这是代码:
import java.util.Scanner;
public class FilmDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
Film[] filmArray = new Film[10];
int numberOfFilms = 0;
int option;
do {
System.out.println("Press 1 to create a Film");
System.out.println("Press 2 to create a Foreign Film");
System.out.println("Press 3 to create a Bollywood Film");
System.out.println("Press 4 to view all the films");
System.out.println("Press 5 to exit");
option = keyboard.nextInt();
if(option == 1) {
Film currentFilm = new Film();
enterInformation(currentFilm);
boolean alreadyInTheArray = checkForDuplicate(filmArray,numberOfFilms,currentFilm);
if(!alreadyInTheArray) {
numberOfFilms = insertIntoArray(filmArray,numberOfFilms,currentFilm);
}
else {
System.out.println("This film already exists in the system");
}
}
else if(option == 2) {
ForeignFilm currentForeignFilm = new ForeignFilm();
enterInformation(currentForeignFilm);
boolean alreadyInTheArray = checkForDuplicate(filmArray,numberOfFilms, currentForeignFilm);
if(!alreadyInTheArray) {
numberOfFilms = insertIntoArray(filmArray,numberOfFilms,currentForeignFilm);
}
else {
System.out.println("This film already exists in the system.");
}
}
else if(option == 3) {
BollywoodFilm currentBollywoodFilm = new BollywoodFilm();
enterInformation(currentBollywoodFilm);
boolean alreadyInTheArray = checkForDuplicate(filmArray,numberOfFilms, currentBollywoodFilm);
if(!alreadyInTheArray) {
numberOfFilms = insertIntoArray(filmArray,numberOfFilms,currentBollywoodFilm);
}
else {
System.out.println("This film already exists in the system");
}
}
else if(option == 4) {
print(filmArray,numberOfFilms);
}
else if(option == 5) {
System.out.println("Thank you for using the Film Library prototype.");
}
else {
System.out.println("Error. Invalid entry");
}
}while(option != 5);
}
public static boolean checkForDuplicate(Film [] array, int numberOfFilms, Film newFilm) {
boolean alreadyInTheArray = false;
for(int i=0; i < numberOfFilms; i++) {
if(array[i].equals(newFilm)) {
alreadyInTheArray = true;
}
}
return alreadyInTheArray;
}
public static int insertIntoArray(Film [] array, int numberOfFilms, Film newFilm) {
array[numberOfFilms] = newFilm;
numberOfFilms++;
return numberOfFilms;
}
public static void print(Film [] array, int numberOfFilms) {
for(int i=0; i < numberOfFilms; i++) {
System.out.println(array[i]);
}
}
public static void enterInformation(Film newFilm) {
Scanner keyboard = new Scanner(System.in);
if(newFilm != null) {
System.out.println("Enter the name of the film");
String name = keyboard.nextLine();
System.out.println("Enter the director of the film");
String director = keyboard.nextLine();
System.out.println("Enter the year of the film");
int year = keyboard.nextInt();
keyboard.nextLine();
newFilm.setName(name);
newFilm.setDirector(director);
newFilm.setYear(year);
if(newFilm instanceof ForeignFilm) {
System.out.println("Enter the native language the film is: ");
String nativeLanguage = keyboard.nextLine();
System.out.println("Enter translation of the name in English");
String translationOfNameInEnglish = keyboard.nextLine();
System.out.println("How many subtitles does this film have?");
int number = keyboard.nextInt();
keyboard.nextLine();
ForeignFilm newForeignFilm = (ForeignFilm)newFilm;
newForeignFilm.setNativeLanguage(nativeLanguage);
newForeignFilm.setTranslationOfNameInEnglish(translationOfNameInEnglish);
for(int i =0; i < number; i++) {
System.out.println("Enter the next subtitle");
String nextSubtitle = keyboard.nextLine();
if(newForeignFilm.getNumberOfSubtitles() < ForeignFilm.getMaxSubtitles()) {
newForeignFilm.addSubtitle(nextSubtitle);
}
}
if(newFilm instanceof BollywoodFilm) {
BollywoodFilm newBollywoodFilm = (BollywoodFilm)newFilm;
System.out.println("How many secondary languages does this film have?");
int secondary = keyboard.nextInt();
keyboard.nextLine();
for(int i=0; i < secondary; i++) {
System.out.println("Enter the next secondary language");
String nextSecondaryLanguage = keyboard.nextLine();
if(newBollywoodFilm.getNumberOfSecondaryLanguages() < BollywoodFilm.getMaxSecondaryLanguages()) {
newBollywoodFilm.addSecondaryLanguage(nextSecondaryLanguage);
}
}
System.out.println("How many songs does this film have?");
int songs = keyboard.nextInt();
keyboard.nextLine();
for(int i=0; i < songs; i++) {
System.out.println("Enter the next song");
String nextSong = keyboard.nextLine();
if(newBollywoodFilm.getNumberOfSongs() < BollywoodFilm.getMaxSongs()) {
newBollywoodFilm.addSong(nextSong);
}
}
}
}
}
}
}
public static void main(String[] args) {
是从命令行启动应用程序时的入口点; 不能删除它 ,它将出现在每个可启动的 Java应用程序中。
相反,您可以在此方法中创建当前类的实例,然后将所有代码移至构造函数和方法调用中。
public static void main(String[] args) {
FilmDemo filmDemo = new FilmDemo(args);
filmDemo.doSomething();
}
public FilmDemo(String[] args) {
// Init anything here
}
public void doSomething() {
// Do something here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.