像一个搜索引擎
like a search engine
我有一个对象列表,其中包含名为 Video
的 class 的电影名称:
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("******************************");
}
}
用于 class 我有这个用于对象
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");
}
}
}
如果您将 Video 实例存储在 class 实现 java.util.List
中,那么实现此目的的一个简单算法是搜索列表并将用户传递的搜索字符串与您视频的名称字段。我会阅读有关 String 方法的 javadoc,例如 `startsWith().
例如,假设
// minimum functionality needed for example
public class Video{
private String name;
public String getName(){
return name;
}
public void printDetails(){
// some method that prints the movie details
}
}
并且您有自己的列表,可能作为封闭 class:
的一个字段
// Somewhere relevant in your code
List<Video> videos = new ArrayList<Video>();
// add your examples to the list
videos.add(v1);
videos.add(v2);
videos.add(v3);
videos.add(v4);
videos.add(v5);
此方法可以解决您的问题:
public void printNames(String query){
if(query.length() < 3){
// report an error, with an exception or print statement
System.out.println("Search query must be at least 3 characters.");
}
for(Video v : videos){
if(v.getName().startsWith(query)){
v.printDetails();
}
}
}
或者,我可能建议使用 StringBuilder
构建您的 return 字符串,然后打印它。或者,return 匹配视频列表并以其他方式处理它们。
我有一个对象列表,其中包含名为 Video
的 class 的电影名称:
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("******************************");
}
}
用于 class 我有这个用于对象
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");
}
}
}
如果您将 Video 实例存储在 class 实现 java.util.List
中,那么实现此目的的一个简单算法是搜索列表并将用户传递的搜索字符串与您视频的名称字段。我会阅读有关 String 方法的 javadoc,例如 `startsWith().
例如,假设
// minimum functionality needed for example
public class Video{
private String name;
public String getName(){
return name;
}
public void printDetails(){
// some method that prints the movie details
}
}
并且您有自己的列表,可能作为封闭 class:
的一个字段// Somewhere relevant in your code
List<Video> videos = new ArrayList<Video>();
// add your examples to the list
videos.add(v1);
videos.add(v2);
videos.add(v3);
videos.add(v4);
videos.add(v5);
此方法可以解决您的问题:
public void printNames(String query){
if(query.length() < 3){
// report an error, with an exception or print statement
System.out.println("Search query must be at least 3 characters.");
}
for(Video v : videos){
if(v.getName().startsWith(query)){
v.printDetails();
}
}
}
或者,我可能建议使用 StringBuilder
构建您的 return 字符串,然后打印它。或者,return 匹配视频列表并以其他方式处理它们。