我是java代码的菜鸟,所以请不要考虑我的写作,我现在要用我的代码做的是创建一个程序从用户获取输入,然后打印出用户的输入(例如:如果用户输入“s”或“S”,程序应该打印出剪刀)并且对于机器人必须打印出它的值(如果它生成为#1,则应该说“我选择Rock”)之后告诉谁是赢家。并且如果用户输入了错误的命令,代码将告诉它“无效”并默认为摇滚然后结果谁是赢家这是我到目前为止我写的代码,但看起来没有什么工作:
[更新]:我刚刚得到了解决方案,所以这里是:
Scanner keyb=new Scanner(System.in);
System.out.println("Please select one of [R/P/S]: ");
String user = keyb.nextLine();
int comt=(int)(3*Math.random())+1;
String comp="";
if (comt==1){
comp= "Rock";
}
else if (comt==2){
comp="Paper";
}
else if (comt==3){
comp="Scissors";
}
if (user.equals("R") || user.equals("r")) {
System.out.println("You choose: Rock");
user = "Rock";
}
else if (user.equals("P")||user.equals("p")) {
System.out.println("You choose: Paper");
user = "Paper";
}
else if (user.equals("S")||user.equals("s")) {
System.out.println("You choos: Scissors");
user = "Scissors";
}
else {
System.out.println("invalid, defaulting to Rock");
user = "Rock";
}
System.out.println("I choose: "+ comp);
if (user.equals(comp)){
System.out.println("A tie!");
}
else if (user.equals("Rock")) {
if (comp.equals("Scissors"))
System.out.println("Rock beats scissor - you win!");
else if (comp.equals("Papper"))
System.out.println("Paper beats rock - you lose");
}
else if (user.equals("Paper")) {
if (comp.equals("Scissors"))
System.out.println("Scissors beats paper- you lose");
else if (comp.equals("Rock"))
System.out.println("Paper beats rock - you win");
}
else if (user.equals("Scissors")) {
if (comp.equals("Papper"))
System.out.println("Scissors beats paper - you win!");
else if (comp.equals("Rock"))
System.out.println("Rock beats scissors - you lose!");
}
keyb.close();
答案 0 :(得分:0)
你忘记了一些括号:
else if (user.equals("R") || user.equals("r")) {
if (comp.equals("S"))
System.out.println("Rock beats scissor - you win!");
else if (comp.equals("P"))
System.out.println("Paper beats rock - you lose");
}
else if (user.equals("P")||user.equals("p")) {
if (comp.equals("S"))
System.out.println("Scissors beats paper- you lose");
else if (comp.equals("R")|| comp.equals("r"))
System.out.println("Paper beats rock - you win");
}
else if (user.equals("S")||user.equals("s")) {
if (comp.equals("P"))
System.out.println("Scissors beats paper - you win!");
else if (comp.equals("R"))
System.out.println("Rock beats scissors - you lose!");
}