我有一个大学的任务
到目前为止,这就是我所拥有的
public class Film {
//Data members
static int idNumber;
String title;
String classification;
//constructor
public Film() {
idNumber = 0;
title = "Not Given";
classification = "G";
classification = "PG";
classification = "12A";
classification = "15A";
classification = "16";
classification = "18";
}
public Film(int idNumber, String title, String classification) {
idNumber = 0;
title = "Not Given";
classification = "Not Given";
}
//Getters
public int getIdNumber( ) {
return idNumber;
}
public String getTitle( ) {
return title;
}
public String getClassification( ) {
return classification;
}
//Setters
public void setIdNumber(int IdNumber) {
IdNumber = idNumber;
}
public void setTitle(String Title) {
Title = title;
}
public void setClassification(String Classification) {
Classification = classification;
}
//calculateLateFee method
double calculateLateFee(int numDaysLate){
return numDaysLate;
}
//toString method
public String toString(){
return idNumber + "" + title + "" + classification;
}
}//end class
和
public class Drama extends Film {
int numDaysLate = 3;
//overridden calculateLateFee method
double calculateLateFee(int numDaysLate){
return numDaysLate;
}
}//end class
和
public class Action extends Film {
int numDaysLate = 5;
//overridden calculateLateFee method
double calculateLateFee(int numDaysLate){
return numDaysLate;
}
}
和
public class Comedy extends Film {
int numDaysLate = 4;
//overridden calculateLateFee method
double calculateLateFee(int numDaysLate){
return numDaysLate;
}
}
import java.util.Scanner;
public class FilmTest {
static int array;
//create method createArray
public static Film[] createArray() {
Scanner int_input = new Scanner(System.in);
Film[] array = new Film[0];
//read user input as arraySize
return new Film[3];
}//end method
//create method populateArray
public static void populateArray(Film[] array) {
Scanner string_input = new Scanner(System.in);
Scanner double_input = new Scanner(System.in);
Scanner int_input = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
Film movies = new Film(); //drama
//set ID number
System.out.println("Enter Movie ID Number: ");
movies.setIdNumber(int_input.nextInt());
//put new student into array passed to the method
array[i] = movies;
}//end for loop
}//end method
//create method display Array
public static void displayArray(Film[] array){
for (Film s : array) {
System.out.println(String.format("%s %d", s.getTitle(),
s.getIdNumber(), s.getClassification()));
}//end for loop
}//end method
public static void main(String [] args) {
// create array of size specified by user
Film[] movies = createArray();
//populate this array with data from user
populateArray(movies);
//display array contents
displayArray(movies);
}//end main method
}//end class
好的,所以我只是学习如何做这些东西,所以有人可以帮助我
如何编写它以便当我输入3个Id编号时,电影的详细信息会出现,如下所示:
Enter Movie ID Number:
12345
Enter Movie ID Number:
34567
Enter Movie ID Number:
95432
12345 - Step Brothers - 15A - Comedy
34567 - Transformers - PG - Action
95432 - The Godfather -18 - Drama
答案 0 :(得分:0)
你需要这样的东西......
public Film GetMovieFromID(int id){
for(Film f: array){
if(f.getIdNumber == id){
return f;
}
}
//If it didn't find the movie return null
return null;
}
然后您需要扫描仪来询问用户输入。因此,在您显示ID和标题以及分类和评级之后。你需要打这样的话......
public void MovieGoerChoosening(){
String s = "What movie would you like to see? (Type in movie ID.)";
Film chosenMovie;
System.out.println(s);
Scanner sc = new Scanner(System.in);
try{
/**
* Surrounded with a try catch block because users are stoopid
* and given the chance they will break your program at any
* opportunity given.
*/
s = sc.Next();
chosenMovie = GetMovieFromID(Integer.parseInt(s));
}catch(NumberFormatException e){
s = "Type in a number with digits only. Try again."
System.out.println(s);
//Recursive call to itself to give the movie goer a second chance
MovieGoerChoosening();
}
//Again users make mistakes and even if they typed only digits it
//could have returned nothing.
if(chosenMovie != null){
System.out.println(String.format("%s %d", chosenMovie.getTitle(),
chosenMovie.getIdNumber(), chosenMovie.getClassification()));
}else{
s = "Not a valid movie... Try again.";
System.out.println(s);
//Recursive call to itself to give the movie goer a second chance
MovieGoerChoosening();
}
}