gpt4 book ai didi

c# - 剪刀石头布c#登场如何显示胜率?

转载 作者:太空宇宙 更新时间:2023-11-03 21:19:22 25 4
gpt4 key购买 nike

我最近开始制作一个 RPS 游戏机应用程序,它运行良好,我决定给自己更多的挑战,并决定如果它对计算机和玩家都有赢率并且由于某种原因它没有显示赢率,那将会很酷左侧的费率编辑:所以我让它向左显示,但百分比仍为 0 知道如何解决这个问题吗?

abstract class Participant
{
public int wins;
int _winRate;

public int winRate
{
get
{
return _winRate;
}
set
{
if (value < 0 || value > 100)
{
throw new Exception("value cannot be less than 0 or greater than 100");
}
_winRate = value;
}
}
public abstract string Choice();
public abstract void PrintWinRate();
}

class Computer : Participant
{
string[] Rock_Paper_Scissor = {"rock","paper","scissor"};
Random rand = new Random();

public override string Choice()
{
string element = Rock_Paper_Scissor[rand.Next(3)];
return element;
}

public override void PrintWinRate()
{
winRate = (wins / Game_Logic.gamesPlayed) * 100;
string _winRate = "computer win rate: "+winRate.ToString()+"%".PadRight(10);
Console.WriteLine(_winRate);
}
}

class Player : Participant
{
public override string Choice()
{
string playerChoice = Console.ReadLine().Trim();
return playerChoice;
}

public override void PrintWinRate()
{
winRate = (wins / Game_Logic.gamesPlayed) * 100;
string _winRate = "player win rate: " + winRate.ToString()+"%".PadRight(10);
Console.WriteLine(_winRate);
}
}

class Game_Logic
{
public static int gamesPlayed;

static void Main()
{
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Black;
Computer comp = new Computer();
Player player = new Player();
string computerChoice;
string playerChoice;
ConsoleKeyInfo input;
bool playAgain;
int computerWins = comp.wins;
int playerWins = player.wins;

do
{
Console.Clear();
computerChoice = comp.Choice();
playerChoice = player.Choice();
Console.Clear();

while (playerChoice == computerChoice)
{
computerChoice = comp.Choice();
}

Console.WriteLine("Player: "+ playerChoice);
Console.WriteLine("\n"+"Computer: " + computerChoice);

if (playerChoice == "rock" && computerChoice == "scissor" || playerChoice == "paper" && computerChoice == "rock")
{
playerWins++;
Console.WriteLine("\n" + "You won!");
}
else if (playerChoice == "scissor" && computerChoice == "rock" || playerChoice == "rock" && computerChoice == "paper")
{
computerWins++;
Console.WriteLine("\n" + "Computer won!");
}
else if (playerChoice == "scissor" && computerChoice == "paper")
{
playerWins++;
Console.WriteLine("\n" + "You won!");
}
else if (playerChoice == "paper" && computerChoice == "scissor")
{
computerWins++;
Console.WriteLine("\n" + "Computer won!");
}
else
{
Console.WriteLine("\n" + "invalid value");
}
Game_Logic.gamesPlayed++;
Console.WriteLine("\n"+"Play again? <y/n>");
Console.WriteLine("\n");

comp.PrintWinRate();
player.PrintWinRate();

input = Console.ReadKey(true);
playAgain = input.KeyChar == 'y';
} while (playAgain);
}
}

最佳答案

首先,我认为递增逻辑存在问题,无法递增参与者对象的实际值。 playerWinscomputerWins 是您的 main 的局部变量,并被视为新变量,而不仅仅是您的参与者对象的引用。更改以下行:

  1. playerWins++ to player.wins++//改变参与者的实际值
  2. computerWins++comp.wins++

接下来,你什么时候说左?您具体想要什么格式?现在尝试以下几行:

  1. string _winRate = string.Format("玩家获胜率:{0}%", winRate);

关于c# - 剪刀石头布c#登场如何显示胜率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31845103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com