我一直遇到堆栈溢出错误(开个玩笑):“未处理的类型为'System.StackOverflowException'的异常在NumberGuessingGame.exe中发生”
我猜如果您看到我的代码以及它发生的位置会更有帮助:
源代码 - testClass:
namespace NumberGuessingGame_08029490
{
public class testClass : Form1
{
public bool _gameWon;
public bool _gameRunning;
public int _number;
public int _guessesRemaining;
public int guessesRemaining
{
get { return _guessesRemaining; }
}
public bool gameEnded
{
get { return !_gameRunning; }
}
public bool gameWon
{
get { return _gameWon; }
}
public testClass()
{
_gameRunning = false;
_gameWon = false;
}
public void saveNewTestGame(int numberGuesses)
{
if (numberGuessesComboBox.SelectedIndex == 0)
{
numberGuesses = 1;
}
if (numberGuessesComboBox.SelectedIndex == 1)
{
numberGuesses = 3;
}
if (numberGuessesComboBox.SelectedIndex == 2)
{
numberGuesses = 5;
}
_guessesRemaining = numberGuesses;
_gameRunning = true;
}
public bool makeGuess(int guessNumber)
{
if (_gameRunning)
{
_guessesRemaining--;
if (_guessesRemaining <= 0)
{
_gameWon = false;
_gameRunning = false;
return false;
}
if (guessNumber == _number)
{
_gameWon = true;
return true;
}
if (guessNumber > _number)
{
guessResultTextBox.Text = "Your Guess is too high, try again";
_gameWon = false;
return false;
}
if (guessNumber < _number)
{
guessResultTextBox.Text = "Your Guess is too low, try again";
_gameWon = false;
return false;
}
else
{
return false;
}
}
else
{
throw new Exception("The game is not running. Call newGame() before making a guess.");
}
}
}
}
来源 - 游戏类:
namespace NumberGuessingGame_08029490
{
public class gameClass : Form1
{
public bool _gameWon;
public bool _gameRunning;
public static Random randomNum = new Random();
public int _number;
public int _guessesRemaining;
public int guessesRemaining
{
get { return _guessesRemaining; }
}
public bool gameEnded
{
get { return !_gameRunning; }
}
public bool gameWon
{
get { return _gameWon; }
}
public gameClass()
{
_gameRunning = false;
_gameWon = false;
}
public void saveNewGame(int numberGuesses)
{
if (numberGuessesComboBox.SelectedIndex == 0)
{
numberGuesses = 1;
}
if (numberGuessesComboBox.SelectedIndex == 1)
{
numberGuesses = 3;
}
if (numberGuessesComboBox.SelectedIndex == 2)
{
numberGuesses = 5;
}
if (rangeNumbersComboBox.SelectedIndex == 0)
{
int randomNumFive = randomNum.Next(1, 5);
randomNumFive = _number;
}
if (rangeNumbersComboBox.SelectedIndex == 1)
{
int randomNumTen = randomNum.Next(1, 10);
randomNumTen = _number;
}
if (rangeNumbersComboBox.SelectedIndex == 2)
{
int randomNumTwenty = randomNum.Next(1, 20);
randomNumTwenty = _number;
}
_guessesRemaining = numberGuesses;
_gameRunning = true;
}
public bool makeGuess(int guessNumber)
{
if (_gameRunning)
{
_guessesRemaining--;
if (_guessesRemaining <= 0)
{
_gameWon = false;
_gameRunning = false;
return false;
}
if (guessNumber == _number)
{
_gameWon = true;
return true;
}
if (guessNumber > _number)
{
guessResultTextBox.Text = "Your Guess is too high, try again";
_gameWon = false;
return false;
}
if (guessNumber < _number)
{
guessResultTextBox.Text = "Your Guess is too low, try again";
_gameWon = false;
return false;
}
else
{
return false;
}
}
else
{
throw new Exception("The game is not running. Call newGame() before making a guess.");
}
}
}
}
来源 - 表单1:
namespace NumberGuessingGame_08029490
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
testClass newTest = new testClass();
gameClass newGame = new gameClass();
private void saveButton_Click(object sender, EventArgs e)
{
if (testCheckBox.Checked == true)
{
int numberGuesses = Convert.ToInt32(numberGuessesComboBox.SelectedIndex);
int _number = Convert.ToInt32(testNumberTextBox.Text);
newTest.saveNewTestGame(numberGuesses);
}
if (testCheckBox.Checked == false)
{
int numberGuesses = Convert.ToInt32(numberGuessesComboBox.SelectedIndex);
int _number = Convert.ToInt32(rangeNumbersComboBox.SelectedIndex);
newGame.saveNewGame(numberGuesses);
}
}
private void guessButton_Click(object sender, EventArgs e)
{
if (testCheckBox.Checked == true)
{
int guessNumber = Convert.ToInt32(guessNumberTextBox.Text);
bool correctAnswer = newTest.makeGuess(guessNumber);
if (correctAnswer)
{
MessageBox.Show("Weldone, you Won!!");
}
// if (game.GameEnded)
// {
// disable guess button, show loss label
// }
}
if (testCheckBox.Checked == false)
{
int guessNumber = Convert.ToInt32(guessNumberTextBox.Text);
bool correctAnswer = newGame.makeGuess(guessNumber);
if (correctAnswer)
{
MessageBox.Show("Weldone, you Won!!");
}
// if (game.GameEnded)
// {
// disable guess button, show loss label
// }
}
}
}
}
编辑:不再出现StackOverflowException, 代码错误:
感谢大家的帮助 <3
已将顶部的"表单1"移除,目前似乎已解决问题,但由于组合框、文本框变量不再继承,无法在代码中使用它们,因此无法测试解决方案。有什么想法可以让它们被使用吗?
Error 1 The name 'numberGuessesComboBox' does not exist in the current context E:\Projects\NumberGuessingGame\NumberGuessingGame\testClass.cs 46 17 NumberGuessingGame
Error 7 The name 'rangeNumbersComboBox' does not exist in the current context E:\Projects\NumberGuessingGame_08029490\NumberGuessingGame_08029490\gameClass.cs 58 17 NumberGuessingGame_08029490
Error 10 The name 'guessResultTextBox' does not exist in the current context E:\Projects\NumberGuessingGame_08029490\NumberGuessingGame_08029490\testClass.cs 84 21 NumberGuessingGame_08029490