Rock,Paper,Scissor游戏 – 当一个人赢三次时如何结束?

我正在写一部Rock(Sten),Paper(Påse),Scissor(Sax)游戏,它与电脑对战。 它可以工作,但除了我赢得三次之外,我想打破游戏。 但它不断循环…我真的很新编程所以如果代码是凌乱的借口… :(和我瑞典语所以代码是瑞典语…希望你理解,如果不是问我..

这是主要的:

static void Main(string[] args) { Game ssp = new Game(); Interaction.MsgBox("Welcome!"); string Choice = Interaction.InputBox("Chose Rock, Scissor eller Paper:"); ssp.Start(); ssp.Seewicharethevinner(Choice); } 

这是带有游戏方法的类:

 string CompusterChoice; //Starts the game public void Start() { //Computers hand Random rnd = new Random(); int x = rnd.Next(0, 3); if (x == 0) { DatornsVal = "Rock"; } else if (x == 1) { DatornsVal = "Paper"; } else if (x == 2) { DatornsVal = "Scissor"; } } //Look who will win public void Seewicharethewinner(string _Choice) { string PlayerChoice = _Choice; string _PlayerChoice = _Choice.ToUpper(); string _ComputerChoice = ComputerChoice.ToUpper(); if (_PlayerChoice == _ComputerChoice) { Interaction.MsgBox("Tie!\nYour choice was: " + _Choice + "\n" + "Computer choice was: " + _ComputerChoice); string Choice = Interaction.InputBox("Chose Rock, Scissor eller Paper:"); ssp.Start(); ssp.Seewicharethevinner(Choice); } else if (_ComputerChoice == "ROCK" && _PlayerChoice == "SCISSOR" || _ComputerChoice == "SICSSOR" && _PlayerChoice == "PAPER" || _ComputerChoice == "PAPER" && _PlayerChoice == "ROCK") { Interaction.MsgBox("You Lose!\nYour choice was: " + _Choice + "\n" + "Computer choice was: " + _ComputerChoice); int player = 0; int computer = 1; Points(computer, player); string Choice = Interaction.InputBox("Chose Rock, Scissor eller Paper:"); ssp.Start(); ssp.Seewicharethevinner(Choice); } else if (_ComputerChoice == "ROCK" && _PlayerChoice == "PAPER" || _ComputerChoice == "SICSSOR" && _PlayerChoice == "ROCK" || _ComputerChoice == "PAPER" && _PlayerChoice == "SICSSOR") { Interaction.MsgBox("You won!\nYour choice was: " + _Choice + "\n" + "Computer choice was: " + _ComputerChoice); int player = 1; int computer = 0; Points(computer, player); string Choice = Interaction.InputBox("Chose Rock, Scissor eller Paper:"); ssp.Start(); ssp.Seewicharethevinner(Choice); } } public void Points(int _computer, int _player) { int computerpoints = 0; int playerpoints = 0; if (_computer > _player) { computerpoints++; } else { playerpoints++; } if (computerpoints == 3) { Interaction.MsgBox("Computer won three times!"); } if (playerpoints == 3) { Interaction.MsgBox("You won three times!"); } } 

所以看起来问题是在你的Poang方法中,你检查一下是否有人赢了3次,如果是,则显示一条消息,但是在你检查到你没有终止程序之后。 它一直在继续,好像什么也没发生。 此外,您的获胜计数变量是本地范围的,因此每次函数结束时它们都会丢失它们的值。

有很多事情可以使这个程序更好,但我只是提供最简单的解决方案:

  public void UtseVinnare(string _Val) { string SpelareVal = _Val; string _SpelarVal = _Val.ToUpper(); string _DatornsVal = DatornsVal.ToUpper(); if (_DatornsVal == _SpelarVal) { Interaction.MsgBox("Oavgjort!\nDitt val var: " + SpelareVal + "\n" + "Datorns val var: " + DatornsVal); string Val = Interaction.InputBox("Välj Sten, Sax eller Påse:"); Starta(); UtseVinnare(Val); } else if (_DatornsVal == "STEN" && _SpelarVal == "SAX" || _DatornsVal == "SAX" && _SpelarVal == "PÅSE" || _DatornsVal == "PÅSE" && _SpelarVal == "STEN") { Interaction.MsgBox("Du förlorade!\nDitt val var: " + SpelareVal + "\n" + "Datorns val var: " + DatornsVal); int spelare = 0; int dator = 1; if (Poang(dator, spelare)) { return; } string Val = Interaction.InputBox("Välj Sten, Sax eller Påse:"); Starta(); UtseVinnare(Val); } else if (_DatornsVal == "STEN" && _SpelarVal == "PÅSE" || _DatornsVal == "SAX" && _SpelarVal == "STEN" || _DatornsVal == "PÅSE" && _SpelarVal == "SAX") { Interaction.MsgBox("Du vann!\nDitt val var: " + SpelareVal + "\n" + "Datorns val var: " + DatornsVal); int spelare = 1; int dator = 0; if (Poang(dator, spelare)) { return; } string Val = Interaction.InputBox("Välj Sten, Sax eller Påse:"); Starta(); UtseVinnare(Val); } } int datorpoangraknare = 0; int spelarpoangraknare = 0; public bool Poang(int _dator, int _spelare) { if (_dator > _spelare) { datorpoangraknare++; } else { spelarpoangraknare++; } if (datorpoangraknare == 3) { Interaction.MsgBox("Datorn vann tre gånger!"); return true; } if (spelarpoangraknare == 3) { Interaction.MsgBox("Du vann tre gåger!"); return true; } return false; } 

我建议您添加以下内容,以便更轻松地使用代码,而不是尝试使用当前方法修复代码:

1:使用枚举为数字赋予明确的含义。

 public enum Choice { Rock, Paper, Scissor } public enum WinResult { Won, Tie, Lost } 

2:添加一个方法来询问用户的输入并返回结果。

 private Choice GiveChoice() { // This is a label where we can jump to if the input was invalid. start: // Ask the question. Console.Clear(); Console.WriteLine("Choose (0:Rock, 1:Paper, 2:Scissor):"); string answer = Console.ReadLine(); int result = -1; // Validate and re-ask if invalid. if (!int.TryParse(answer, out result) || (result < 0 && result > 2)) goto start; return (Choice) result; } 

3:添加一种方法来比较彼此的2个结果。

 // Returns if v1 has won, tied or lost from v2. (Left to right) private WinResult CompareForWinner(Choice v1, Choice v2) { if (v1 == Choice.Paper) { if (v2 == Choice.Paper) return WinResult.Tie; if (v2 == Choice.Rock) return WinResult.Lost; return WinResult.Won; } if (v1 == Choice.Rock) { if (v2 == Choice.Paper) return WinResult.Lost; if (v2 == Choice.Rock) return WinResult.Tie; return WinResult.Won; } // v1 = Scissor. if (v2 == Choice.Paper) return WinResult.Won; if (v2 == Choice.Rock) return WinResult.Lost; return WinResult.Tie; } 

这不是你问题的直接答案。 但我认为它会帮助你自己解决它。

当然这个问题的答案应该是去读一本关于编程c#的书吗?

我注意到这被标记为上面的重复。 当你去那个复制时,它也被标记为重复,并且仅在11个小时前。

我真的不认为人们应该只是张贴他们的作业….