为什么$ computer_choice不是随机的?

时间:2019-05-08 09:20:00

标签: bash shell cygwin

所以我从没真正上过编程课,我只关注youtube上的一些教程,并尝试从头开始编写自己的代码。我正在尝试在nano中制作此硬币翻转程序,但似乎不起作用。当我选择H(正面)时,我有时会赢或输,但是当我选择T(尾部)时,它会说“您输了”两次。

我正在使用的终端是cygwin,出于某种原因,该终端不允许使用C ++。我什至不知道随机函数是否有效。

@Entity
@Table(name = "test_case")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class TestCase implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

    @NotNull
    @Column(name = "component_name", nullable = false)
    private String techComponent;
}

我希望看到“你赢”或“你输”,但是,只有当你选择正面时,你才会看到自己获胜;当你选择尾巴时,你总是会失败,但是你却两次看到。我再一次对任何编程语言都缺乏经验。如果有人能解释出什么问题了,将不胜感激!

1 个答案:

答案 0 :(得分:0)

printf "(H) heads or (T) tails"
read user_choice
if [ $user_choice != H ] && [ $user_choice != T ]; then
  echo invalid choice defaulting to heads
 $user_choice=H=1
 $user_choice=T=2
fi
#value of 1 is heads, 2 is Tails
computer_choice=$(($RANDOM% 2 + 1))
if [ $computer_choice == 1 ]; then
  echo computer chooses heads
elif [ $computer_choice == 2 ]; then
echo computer chooses tails
fi
if [ $computer_choice == 1 ] && [ $user_choice = H ]; then
  echo you win!
else
if [ $computer_choice == 2 ] && [ $user_choice = T ]; then
    echo you win!
  else
    echo you lose
  fi
fi