背景:编程方面的初学者,对整体来说是非常新的" Java"事情。
我喜欢这个帮助,但我不想直接回答,更像是正确方向上的一个点,或者我可以学习的任何方式,而不是盲目地复制/粘贴。而不是"继承正确的代码"更多的"听说如何获得正确的代码" 谢谢:))
好的,我的问题是,IF / THEN语句的提示符有什么问题。当我用提示符运行它时,它说找不到符号 - 方法提示符(java.lang.String) 没有提示,当我运行它时,在我输入我的选择之后,无论是对还是错,它总是返回"抱歉,这是一个选择。选择摇滚,纸张或剪刀!"即使它是对的!
如果您需要有关我的问题的更多信息,请告诉我:) 无论如何这里是班级:
//Player class
import java.util.Scanner;
import java.lang.String;
public class Player
{
private String name;
private String choice;
public Player(String nm)
{
name = nm;
}
public Player(String nm, String ch)
{
name = nm;
choice = ch;
}
public void setName( String nm)
{
name = nm;
}
public void setChoice( String ch )
{
}
public String getChoice()
{
Scanner scan = new Scanner (System.in);
System.out.println("Choose rock, paper or scissors:");
String player = scan.next(); player.toLowerCase();
if ((player != ("rock"))
|| (player != ("paper"))
|| (player != ("scissors")))
{
System.out.println("Sorry, that is'nt a choice. Choose rock, paper or scissors!");
player = prompt("Choose rock, paper or scissors:");
}
else
{
System.out.println("Good choice!");
}
System.out.println("You chose " + player);
return "";
}
public String getName()
{
Scanner scan = new Scanner (System.in);
System.out.println("Whats your name?");
String name = scan.next();
return "";
}
public String toString()
{
return "";
}
}

跑步者:
public class PlayerRunner
{
public static void main(String[] args)
{
Player s = new Player("Michael Jackson", "rock");
System.out.println(s.getChoice());
System.out.println(s.getName());
//outs rock
//call the getName() method
System.out.println(s); //outs Michael Jackson rock
//set the choice to paper
System.out.println(s); //outs Michael Jackson paper
//instantiate a new Player named jb named Jim Bob that chose scissors
//print out Jim Bob
}
}

如果您发现我做了其他任何错误,请告诉我:)再次,我是初学者,但我在这里学习,而不仅仅是粘贴正确答案并继续前进。 谢谢!!
答案 0 :(得分:1)
怎么样:
public String getChoice() {
Scanner scan = new Scanner(System.in);
System.out.println("Choose rock, paper or scissors:");
String player = scan.next();
while ((!player.equalsIgnoreCase("rock"))
&& !player.equalsIgnoreCase("paper")
&& !player.equalsIgnoreCase("scissors")) {
System.out.println("Sorry, that is'nt a choice. Choose rock, paper or scissors!");
System.out.println("Choose rock, paper or scissors:");
player = scan.next();
}
System.out.println("Good choice!");
System.out.println("You chose " + player);
return "";
}
答案 1 :(得分:0)
使用==
运算符时,您要比较两个对象是否相同,或者两个基本类型的值是否相同。
由于字符串实际上是Java中的对象,
String a = "string";
String b = "string";
System.out.println(a == b);
将返回false,但
String a = "string";
String b = a;
System.out.println(a == b);
将返回true。
相反,如果调用String类的equals
方法,则可以比较这两个值是否相同。
String a = "string";
String b = "string";
System.out.println(a.equals(b));
在您的情况下,player != ("rock")
将始终评估为true,这看起来会导致一些问题。
您可以在此处阅读更多内容:Java String.equals versus ==