【发布时间】:2015-02-02 23:59:34
【问题描述】:
我有一个对象列表,其中包含一个名为 Video 的类的电影名称:
Video v1 = new Video("The Shawshank Redemption", "Frank Darabont", "Drama", "9.2*", 49.99, true);
Video v2 = new Video("The Godfather", "Francis Ford Coppola", "Crime, Drama", "9.2*", 49.99, true);
Video v3 = new Video("The Godfather: Part II", "Francis Ford Coppola", "Crime, Drama", "9*", 48.99, true);
Video v4 = new Video("The Dark Knight", "Christopher Nolan", "Action, Crime, Drama", "9*", 48.99, false);
Video v5 = new Video("Pulp Fiction ", "Quentin Tarantino", "Crime, Thriller, Drama", "8.9*", 47.99, true);
我需要让用户输入名称或名称的一部分(至少包含 3 个字符),然后打印与用户输入匹配的电影,但我不确定如何以简单的方式完成.
有什么想法吗? 这是我的完整代码
class Video
{
String name, director, type, rating;
Double price;
boolean borrowed;
String borrowedTrue;
Video(String n, String d, String t, String r, Double p, boolean b)
{
name = n;
director = d;
type = t;
rating = r;
price = p;
borrowed = b;
if(b == true)
{
borrowedTrue = "Yes";
}
else if(b == false)
{
borrowedTrue = "No";
}
}
public void display()
{
System.out.println("");
System.out.println("******************************");
System.out.println("");
System.out.println("Name: "+name);
System.out.println("Director: "+director);
System.out.println("Type: "+type);
System.out.println("Rating: "+rating);
System.out.println("Prince: "+price);
System.out.println("Borrowed: " +borrowedTrue);
System.out.println("");
System.out.println("******************************");
}
}
类,我有这个对象
import java.util.*;
class TestVideo
{
static int counter = 0;
public static void main(String args[])
{
Video v1 = new Video("The Shawshank Redemption","Frank Darabont","Drama","9.2*",49.99,true);
Video v2 = new Video("The Godfather","Francis Ford Coppola","Crime,Drama","9.2*",49.99,true);
Video v3 = new Video("The Godfather: Part II","Francis Ford Coppola","Crime,Drama","9*",48.99,true);
Video v4 = new Video("The Dark Knight","Christopher Nolan","Action,Crime,Drama","9*",48.99,false);
Video v5 = new Video("Pulp Fiction ","Quentin Tarantino","Crime,Thriller,Drama","8.9*",47.99,true);
Video v6 = new Video("12 Angry Men","Sidney Lumet","Drama","8.9*",47.99,true);
Video v7 = new Video("The Good, the Bad and the Ugly","Sergio Leone","Western","8.9*",47.99,false);
Video v8 = new Video("Schindler's List","Steven Spielberg","Bography,History,Drama","8.9*",47.99,false);
Video v9 = new Video("The Lord of the Rings: The Return of the King","Peter Jackson"," Peter Jackson","8.9*",47.99,true);
Video v10 = new Video("Fight Club","David Fincher","Drama","8.9*",47.99,false);
Video v11 = new Video("The Lord of the Rings: The Fellowship of the Ring"," Peter Jackson"," Peter Jackson","8.8*",46.99,true);
Video v12 = new Video("Star Wars: Episode V - The Empire Strikes Back","Irvin Kershner","Action,Adventure,Fanstasy","8.8*",46.99,true);
Video v13 = new Video("Forrest Gump ","Robert Zemeckis","Romance,Drama","8.8*",46.99,false);
Video v14 = new Video("Inception","Christopher Nolan","Action,Mystery,Sci-Fi","8.8*",47.99,false);
Video v15 = new Video("The Lord of the Rings: The Two Towers","Peter Jackson","Adventure,Fantasy","8.8*",45.99,false);
Video v16 = new Video("Interstellar","Christopher Nolan","Adventure,Sci-Fi","8.8*",44.99,false);
Scanner user_input = new Scanner(System.in);
System.out.println("Please enter the name of the movie");
String matchName =user_input.next();
if (matchName.matchesIgnoreCase("[the]*"))
{
v1.display();
v2.display();
// i have to manually search for any videos which name contains 'the'
}
else
{
System.out.println("No match");
}
}
}
【问题讨论】:
-
如果你不知道有什么简单的方法,那就试试不简单的方法。无论如何,重要的是你首先尝试。然后,如果您仍然卡住,您可以在这里提出一个更好更具体的问题以及您的代码尝试。
-
请不要在cmets中写代码。编辑您的问题。