我正在创建一个游戏,即使用户输入了有效答案,我希望 Java 运行的代码也没有运行,我不确定如何解决这个问题。
我尝试为变量创建一个键盘语句,并在公共类之后单独尝试一个静态字符串变量名称,但是当我最初没有时它却给我错误。
下面是我的原始代码:
public static void carMethod (Scanner keyboard)
{
System.out. println ("Now, what is it that you decide to do?");
System.out.println ("Here are your options:");
System.out.println ();
System.out.println ();
System.out.println ("(A.) Go up to the car and see if anyone’s inside.");
System.out.println ("It’s weird enough that a car is in your area anyway");
System.out.println ();
System.out.println ("OR");
System.out.println ();
System.out.println ("(B.) Ignore it. It’s obviously none of your business.");
System.out.println ("So, what’s it going to be?");
System.out.println ("A or B?");
System.out.println ();
String carSelection=keyboard.nextLine();
if (carSelection == "A" || carSelection == "a") {
System.out.println ("You peep into the passenger's side’s windows and don’t notice much");
System.out.println ("other than used soda cans and chip bags scattered across the floor.");
System.out.println ("You also notice that the driver’s sides’s windows are completely open");
System.out.println ("and the keys are casually sitting on top of the driver’s seat, waiting to be taken.");
System.out.println ("What do you plan on doing?");
TextAdventure.searchMethod (keyboard); //note that this is a seperate method and does not have anything to do with the problem
}
if (carSelection == "B" || carSelection == "b") {
System.out.println ("Well my friend…");
System.out.println ("...you let the car be and continue on with your day");
System.out.println ("and nothing eventful ever happens to you again.");
System.out.println ("THE END");
}
}
任何帮助将不胜感激!谢谢。