C# 我收到此代码的StackOverflowException错误。我还没能找出错误
我正在尝试做一个有两门C语言课的石头剪刀决策者。我想这可能是错误的,所以去城里吧C# 我收到此代码的StackOverflowException错误。我还没能找出错误,c#,C#,我正在尝试做一个有两门C语言课的石头剪刀决策者。我想这可能是错误的,所以去城里吧 using System; using static System.Console; namespace Tes { class PlayerApp { public static void Main() { Player player1 = new Player(); player1.PlayerChoice = In
using System;
using static System.Console;
namespace Tes
{
class PlayerApp
{
public static void Main()
{
Player player1 = new Player();
player1.PlayerChoice = InputValue();
player1.Classif = InputValue();
Clear();
Write(player1);
Write("\n\n\n\n");
ReadKey();
}
public static string InputValue()
{
Write("Please enter rock, paper, or scissors:\t");
return ReadLine();
}
}
class Player
{
private string classif;
// constructors
public Player()
{}
public Player(string pC)
{
PlayerChoice = pC;
}
// properties
public string PlayerChoice
{
get
{
return PlayerChoice;
}
set
{
PlayerChoice = value;
}
}
public string Classif
{
get
{
return classif;
}
set
{
classif = value;
}
}
public double SetFine()
{
if (classif == "rock")
{
WriteLine("The computer chose paper. You lose.");
}
else if (classif == "paper")
{
WriteLine("The computer chose scissors. You lose.");
}
else if (classif == "scissors")
{
WriteLine("The computer chose rock. You lose.");
}
return SetFine();
}
}
}
替换
与
PlayerChoice似乎调用自身,这将导致即时堆栈溢出。它不应该从其他地方获取值吗?例如,是否应该有一个变量playerChoice,就像您使用classIf时一样?您应该为playerChoice属性使用一个支持字段,或者使用一个自动实现的属性public string playerChoice{get;set;}如果您描述了为什么原始代码是导致溢出的一个问题,那么可能的重复将更加有用。还有,Classif属性没有问题,是吗?另外,您可以通过在每行前面放置4个空格,或者通过选择代码行并从编辑器菜单中选择{}来格式化代码。
public string PlayerChoice
{
get
{
return PlayerChoice;
}
set
{
PlayerChoice = value;
}
}
public string Classif
{
get
{
return classif;
}
set
{
classif = value;
}
}
public string PlayerChoice { get; set; }
public string Classif { get; set; }