首页 > 解决方案 > 我收到此代码的 StackOverflowException 错误。我一直无法找出错误

问题描述

我正在尝试用 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();
        }    
    }
}

标签: c#

解决方案


代替

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

推荐阅读