我收到此代码的StackOverflowException错误。我无法找出错误

时间:2019-03-14 00:40:23

标签: c#

我正在尝试用C#制作具有两个类的剪刀石头布的决策者。我认为很多事情可能是错误的,所以去镇上。

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();
        }    
    }
}

1 个答案:

答案 0 :(得分:-1)

替换

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; }