簡體   English   中英

剪刀石頭布的麻煩

[英]Trouble with Rock-Paper-Scissors

我一直在為學校設計一個項目,使用功能制作石頭剪刀布游戲。 這是我到目前為止的代碼,但是該程序存在一些問題。 這是程序現在返回的內容。

Rock, Paper, Scissors

~~~~~~~~~~~~~~~~~~~~~

Rock: 1

Paper: 2

Scissors: 3

2

You picked:

Computer picked:



Press any key to continue . . .

它沒有顯示任何字符串,並且我對該語言不了解為什么:(我希望代碼上有一些幫助/指針/修復。我沒有使用std名稱空間,因為我找到了一個指南建議可能是造成問題的原因。

#include <cmath>
#include <iostream>
#include <string>

std::string GetComputerChoice(std::string ComputerChoice)
{
   double Computer;
   Computer = rand() % 3 + 1;

   if (Computer == 1)
   {
      ComputerChoice == "rock";
      return ComputerChoice;
   }
   else if (Computer == 2)
   {
      ComputerChoice == "paper";
      return ComputerChoice;
   }
   else
   {
      ComputerChoice == "scissors";
      return ComputerChoice;
   }
}

std::string GetUserChoice(std::string UserChoice)
{
   double User;
   std::cin >> User;

   if (User < 1 || User > 3)
   {
      std::cout << "Invalid entry ";
      system("pause");
      return 0;
   }
   else if (User = 1)
   {
      UserChoice == "rock";
      return UserChoice;
   }
   else if (User = 2)
   {
      UserChoice == "paper";
      return UserChoice;
   }
   else
   {
      UserChoice == "scissors";
      return UserChoice;
   }
}

std::string DetermineWinner( std::string UserChoice, std::string ComputerChoice, std::string Victory )
{
   Victory == "Something Has Gone Wrong";

   if (ComputerChoice == UserChoice)
   {
      Victory == "Tie!";
      return Victory;
   }
   else if (UserChoice == "rock")
   {
      if (ComputerChoice == "paper")
      {
         Victory == "Paper beats rock! You lose!";
         return Victory;
      }
      else
      {
         Victory == "Rock smashes scissors! You win!";
         return Victory;
      }
   }
   else if (UserChoice == "paper")
   {
      if (ComputerChoice == "rock")
      {
         Victory == "Paper beats rock! You win!";
         return Victory;
      }
      else
      {
         Victory == "Scissors cut paper! You lose!";
         return Victory;
      }
   }
   else
   {
      if (ComputerChoice == "paper")
      {
         Victory == "Scissors cut paper! You win!";
         return Victory;
      }
      else
      {
         Victory == "Rock smashes scissors! You lose!";
         return Victory;
      }
   }
}

int main()
{
   std::cout << "Rock, Paper, Scissors" << std::endl;
   std::cout << "~~~~~~~~~~~~~~~~~~~~~" << std::endl;
   std::cout << "Rock: 1" << std::endl;
   std::cout << "Paper: 2" << std::endl;
   std::cout << "Scissors: 3" << std::endl;

   std::string UserChoice, ComputerChoice, Victory;

   GetUserChoice( UserChoice );
   GetComputerChoice( ComputerChoice );
   DetermineWinner( UserChoice, ComputerChoice, Victory);

   std::cout << "You picked: " << UserChoice << std::endl;
   std::cout << "Computer picked: " << ComputerChoice << std::endl;
   std::cout << Victory << std::endl;
   system("pause");
   return 0;
}

您不能像這樣將參數重新分配給該函數。 您需要改用GetUserChoice的返回值。 放棄參數到Get函數,因為它沒有做任何事情,而是重新分配該函數的返回值給變量:

UserChoice = GetUserChoice();

作為旁注,請查找適當的命名對話。 任意變量名都不能這樣大寫。 大寫的標識符主要保留給類名。

您遇到了幾個問題:

  1. 在分配字符串時,您錯誤地比較了字符串:

     ComputerChoice == "rock"; 

    要將"rock"分配給ComputerChoice ,請確保使用=

     ComputerChoice = "rock"; 
  2. 您可以通過return ComputerChoice;返回計算機的選擇return ComputerChoice; ,但您永遠不會在調用GetComputerChoice()時保存返回的值:

     GetComputerChoice( ComputerChoice ); 

    相反,您必須將其編寫為:

     ComputerChoice = GetComputerChoice(); 

    注意,您不需要將任何東西傳遞給函數; 您可以刪除std::string ComputerChoice作為函數參數。

當然,上面的兩個例子並不是代碼中唯一不正確的地方。 您必須檢查並修復代碼中遇到上述問題之一的任何部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM