在'> ='之前预期的主要表达错误

时间:2018-01-31 14:15:37

标签: c++ compiler-errors

我目前正在用c ++制作Rock,Paper,Scissors代码。但是,当我尝试将玩家猜测与计算机猜测进行比较时,我会在标题中得到错误等等。这是我的代码:

#include <stdio.h>
#include <iostream>
#include <random>

void
RocPaprSkisors ()
{

  std::random_device rd;
  std::mt19937 mt(rd());
  std::uniform_real_distribution<double> dist(1, 4);
  std::cout << dist(mt) << "\n";

  int player_guess;
  std::cout << "Hello! Welcome to Rock, Paper, Scissors!" << std::endl;
  std::cout <<
    "You have to try and beat the computer by choosing either rock, paper, or scissors"
    << std::endl;
  std::cout <<
    "Rock beats scissors, scissors beats paper, and paper beats rock." <<
    std::endl;
  std::cout << "Please guess 1 for paper, 2 for rock, or 3 for scissors" <<
    std::endl;
  std::cin >> player_guess;
  if (player_guess == dist(mt))
    {
      std::cout << "You tied!" << std::endl;
      std::cout << dist(mt) << std::endl;
    }
  else
    {
      if (player_guess == 1 and dist(mt) < 3 and >= 2)
    {
      std::cout << "The computer guessed rock! That means you win!" <<
        std::endl;
    }
      else
    {
      if (player_guess == 2 and dist(mt) >= 1 and < 2)
        {
          std::cout <<
        "The computer guessed paper. That means you be loser. :(" <<
        std::endl;
        }
      else
        {
          if (player_guess == 1 and dist(mt) >= 3 and <= 4)
        {
          std::cout <<
            "The computer guessed scissors. That means you be loser. :("
            << std::endl;
        }
          else
        {
          if (player_guess == 3 and dist(mt) >= 1 and < 2)
            {
              std::cout <<
            "The computer guessed paper!. That means you win!" <<
            std::endl;
            }
          else
            {
              if (player_guess == 2 and dist(mt) >= 3 and <= 4)
            {
              std::cout <<
                "The computer guessed scissors! That means you win!"
                << std::endl;
            }
              else
            {
              if (player_guess == 3 and dist(mt) >= 2 and < 3)
                {
                  std::cout <<
                "The computer guessed rock. That means you be   loser. :("
                << std::endl;
                            {
            }
            }
        }
        }
    }
    }
}

int
main ()
{
  RocPaprSkisors ();
  return 0;
}

当我运行代码时,我遇到了这些错误:

main.cpp: In function 'void RocPaprSkisors()':
main.cpp:39:50: error: expected primary-expression before '>=' token
       if (player_guess == 1 and dist(mt) < 3 and >= 2)
                                                  ^
main.cpp:46:48: error: expected primary-expression before '<' token
    if (player_guess == 2 and dist(mt) >= 1 and < 2)
                                                ^
main.cpp:54:52: error: expected primary-expression before '<=' token
        if (player_guess == 1 and dist(mt) >= 3 and <= 4)
                                                    ^
main.cpp:62:49: error: expected primary-expression before '<' token
     if (player_guess == 3 and dist(mt) >= 1 and < 2)
                                                 ^
main.cpp:70:53: error: expected primary-expression before '<=' token
         if (player_guess == 2 and dist(mt) >= 3 and <= 4)
                                                     ^
main.cpp:78:50: error: expected primary-expression before '<' token
      if (player_guess == 3 and dist(mt) >= 2 and < 3)
                                                  ^

有谁知道如何解决这些错误?我刚刚开始用c ++编写代码,所以请保持简单的答案。

2 个答案:

答案 0 :(得分:3)

您的代码中有2个问题:

如果您有要求,可以用数学方式编写1 < a < 5,则无法表达如下:

if( a > 1 and < 5 )

必须是:

if( a > 1 and a < 5 )

因为这是编程语言,而不是编写数学方程式的语言。该问题导致语法错误。

现在您的程序中存在逻辑错误 - 您不能每次都调用dist(),因为它会在每次调用时生成新的随机值。你需要的是调用它一次,记住在一个变量中并在下面的逻辑中使用它。并且您可能不需要首先生成的double类型的随机值,因为您需要[1,3]范围内的整数

答案 1 :(得分:0)

编译器不知道要评估>= 2的内容,因为你没有说明它(你的语法不正确)。

你可能想做的是:

if (player_guess == 1 && dist(mt) < 3 && dist(m) >= 2)

if (player_guess == 2 && dist(mt) >= 1 && dist(m) < 2)

这会回答你的问题吗?