在剪刀石头布游戏中确定获胜者 [英] determining the winner in a rock paper scissor game
问题描述
在这款剪刀石头布游戏中,我需要一种不同的方法来确定获胜者.当我运行程序时,其余程序运行平稳,但我不会显示获胜者.我认为它们可能是我代码中的逻辑错误.
in this rock paper scissor game i need an different method to determine winner. when i run my program the rest runs smoothly but i wont display a winner. i think their might be a logical error in my code.
public static void determineWinner(String userLower, String comp) {
String user = userLower;
String computer = comp;
if(user.equals(computer)) {
System.out.println("It's a tie!");
} else if(user.equals("rock")) {
if(computer.equals("scissors")) {
System.out.println("You win!");
}
} else if(computer.equals("rock")) {
if(user.equals("scissors")) {
System.out.println("You lose!");
}
} else if(user.equals("scissors")) {
if(computer.equals("paper")){
System.out.println("You win!");
}
} else if(computer.equals("scissors")) {
if(user.equals("paper")) {
System.out.println("You lose");
}
} else if(user.equals("paper")) {
if(computer.equals("rock")) {
System.out.println("You Win!");
}
} else if(computer.equals("paper")) {
if(user.equals("rock")) {
System.out.println("You lose!");
}
}
推荐答案
基本上,您正在测试每种可能的组合(除了平局).使用剪刀石头布,您会有9种不同的结果,其中3种是平局,所以还剩下6种.
Basically, you are testing every possible combination (with the exception of a tie). With rock-paper-scissors, you have 9 different outcomes, and 3 of them are ties so there are 6 more left.
现在想像一下,您必须针对21世纪的变体石头剪刀布-蜥蜴-Spock"执行此操作:25种不同的结果.这是if else
的一长串.
Now imagine you have to do this for the 21st century variant "rock-paper-scissors-lizard-Spock": 25 different outcomes. That's a long list of if else
s.
但是:所有可能的结果都可以存储在一个表中,并且该表可以随您想要的大小而变.该表用于RPSLS(找不到用于RPS的表),但是原理是相同的:
However: all possible outcomes can be stored into a table, and that table can be as small (or large) as you want. This table is for RPSLS (can't find one for RPS), but the principle is the same:
您在垂直方向选择您的选择,在水平方向选择您的计算机;那么,左边的数字就是对您的得分(显然,Win代表1,Lose代表输),右边的数字是对手.
You pick your choice vertically and the computer's horizontally; then, the number on the left is points for you (obviously, 1 is for Win, -1 is Lose) and the number on the right is the opponent.
(编辑)这是剪刀石头布的表格.每个组合不需要两个元素,另一个总是与显示的相反.该表在列中显示了获胜者,在行中显示了失败者.
(Edit) Here is the table for Rock-Paper-Scissors. You don't need two elements per combination, the other one is always the opposite of what's shown. The table shows Winner in the columns, Loser in the rows.
+----------+-------+-------+--------+
| | Rock | Paper |Scissors|
+----------+-------+-------+--------+
| Rock | 0 | 1 | -1 |
| Paper | -1 | 0 | 1 |
| Scissors | 1 | -1 | 0 |
+----------+-------+-------+--------+
这篇关于在剪刀石头布游戏中确定获胜者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!