所以我正在为我的 java 类创建 Kevin Bacon 游戏。
这些是我必须使用的名称文件
演员:
Leonardo Dicaprio
Susan Sarandon
Tom Hanks
Robert De Niro
Barack Obama
Helen Keller
Katharine Cornell
Helen Hayes
John Laughlin
Mark Zuckerberg
Joe Lipari
Welker White
关系:
Susan Sarandon | Tom Hanks : Cloud Atlas
Tom Hanks | Kevin Bacon : Apollo 13
Leonardo Dicaprio | Kevin Bacon : This Boy's Life
Robert De Niro | Kevin Bacon : This Boy's Life
Barack Obama | Tom Hanks : the Road We've Traveled
Helen Keller | Katharine Cornell : Helen Keller in Her Story
Katharine Cornell | Helen Hayes : Stage Door Canteen
Helen Hayes | John Laughlin : Murder with Mirrors
John Laughlin | Kevin Bacon : Footloose
Mark Zuckerberg | Joe Lipari : Terms and Conditions May Apply
Joe Lipari | Welker White : Eat Pray Love
Welker White | Kevin Bacon : Lemon Sky
这是我现在拥有的程序:
package Game;
import java.io.*;
import java.util.HashMap;
import java.util.Scanner;
import java.util.regex.Pattern;
/**
* @author
*
*/
public class BaconNumber
{
/**
* @param args
*/
private HashMap<String,String> relationships;
private HashMap<String,String> actors;
public static void main(String[] args)
throws FileNotFoundException
{
Scanner input = new Scanner(new File("relationships"));
HashMap<String, String> relationships = new HashMap<String, String>();
while (input.hasNextLine()) {
String[] columns = input.nextLine().split(Pattern.quote(" | "));
relationships.put(columns[0], columns[1]);
}
System.out.println(relationships);
}
public BaconNumber()
{
relationships = new HashMap<String,String>();
actors = new HashMap<String,String>();
}
public void printActors() throws FileNotFoundException
{
Scanner input = new Scanner(new File("actors"));
while (input.hasNextLine())
{
System.out.println(input.nextLine());
}
}
public int getBaconNumber( String actor , int number)
{
if( actor == "Kevin Bacon")
{
return number;
}
else
{
relationships.get(actor);
System.out.println(actor + " starred in " + relationships.value + "with" + relationships.value );
System.out.println( " The Bacon Number for " + actor + " is " + number );
return number; // fix this
}
relationships.containsKey("Kevin Bacon")
// {
// number++;
// System.out.println(" The bacon number for" + actor + " is " + number );
// }
// else
// {
//
// }
}
}
我的 getBaconNumber() 需要一些帮助,我需要程序来查找演员并计算最终到达凯文培根时的培根编号。
下面是教授对这个程序的要求: 1. 在你的 hashmap 中查找 actor 关系 2. 将当前关系打印到控制台 3. 使用关系中的第二个 actor 递归调用该方法(确保增加你的 bacon 编号)。
这种方法让我感到困惑,我无法完成它。
我需要它来打印这样的东西:
Helen Keller
Helen Keller starred in "Helen Keller in her story" with Katherine Cornell.
Katherine Cornell starred in " Stage Door Canteen " with Helen Hayes.
Helen Hays starred in "Murder with mirrors" with John Laughlin.
John Laughlin starred in " FootLoose" with Kevin Bacon.
The bacon number for Helen Keller is 4
如果有人可以提供帮助,请我真的需要帮助吗