簡體   English   中英

我如何將一個整數的值從一個類返回到我的main? C#

[英]How do i return the value of a integer from a class to my main? C#

所以我很難從一個類的整數返回值。

這是我的主要程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MatchGame
{
    class Program
{
    static void Main(string[] args)
    {
        //( Amount of matches, bot argument, player argument, gameover?,)

        //AI MatchGame = new AI(0, 0, 0, false, 0, false);

        int matchAmount = 0;
        int botMove = 0;
        int playerMove = 0;
        bool gameOver = false;
        int playerMoveAttempt = 0;
        bool choseRightAmount = false;


        void TurnPlayer()
        {

            PlayersTurn PlayerMoved = new PlayersTurn(); //choseRightAmount, playerMoveAttempt, playerMove, matchAmount);

            PlayersTurn.PlayerTurnMove(choseRightAmount, playerMoveAttempt, playerMove, matchAmount);




                Console.WriteLine(playerMoveAttempt);
                playerMove = playerMoveAttempt;
                matchAmount = matchAmount - playerMove;




        } 

       /*
        void PlayerTurn()
        {
            choseRightAmount = false;

            while (!choseRightAmount)
            {

                playerMoveAttempt = Convert.ToInt32(Console.ReadLine());
                if (playerMoveAttempt < 1 || playerMoveAttempt > 3)
                {
                    Console.WriteLine("Please take between 1 and 3 matches");
                }
                else
                {
                    playerMove = playerMoveAttempt;
                    matchAmount = matchAmount - playerMove;
                    choseRightAmount = true;
                }

            }
        } */


        void BotTurn()
        {
            botMove = matchAmount % 4;
            matchAmount = matchAmount - botMove;
        }


        void Status()
        {
            Console.WriteLine("Bot took " + botMove + " matches from the board.");
            Console.WriteLine("There are " + matchAmount + " matches left.");
        }


        void Game()
        {
            Console.WriteLine("Welcome to the game!");
            Console.WriteLine("To win you need to take the last match!");
            Console.WriteLine("You can only take between  1 and 3 matches per turn.");
            Console.WriteLine("Please choose the amount of matches you want.");
            matchAmount = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("You chose " + matchAmount + " It is your turn, please take between 1 and 3 matches");


            while (!gameOver)
            {

                TurnPlayer();

                if (matchAmount <= 0)
                {
                    Console.WriteLine("You Won.");
                    break;
                }

                BotTurn();
                if (matchAmount <= 0)
                {
                    Console.WriteLine("You lost! The bot won the match!");
                    gameOver = true;
                    break;
                }

                Status();

            }

            Console.ReadKey();

        }

        Game();


    }
}
}

所以我需要從該類中獲取playerMove嘗試的值。 我知道我可以輕松地在主程序中執行此操作,但是我的老師希望我們使用課程。 我需要在TurnPlayer()中使用它,以便我可以計算出剩余的匹配數。 這在按鈕的while循環中使用。

這是課程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MatchGame
{
    public class PlayersTurn
{

    public static int PlayerTurnMove(bool choseRightAmount, int playerMoveAttempt, int playerMove, int matchAmount)
    {



        choseRightAmount = false;

        while (!choseRightAmount)
        {

            playerMoveAttempt = Convert.ToInt32(Console.ReadLine());
            if (playerMoveAttempt < 1 || playerMoveAttempt > 3)
            {
                Console.WriteLine("Please take between 1 and 3 matches");
            }
            else
            {

                choseRightAmount = true;
                return playerMoveAttempt;


            }



        }

        return playerMoveAttempt;

    }

}
}

是的,正如您所看到的,我正在嘗試返回playerMoveAttempt。 我已經知道該類正在工作,只是不返回新值。

希望你們中的一些可以幫助! 提前致謝。

您需要將方法調用的結果分配給變量,如下所示:

playerMoveAttempt = PlayersTurn.PlayerTurnMove(choseRightAmount, playerMoveAttempt, playerMove, matchAmount);
Console.WriteLine(playerMoveAttempt);

請記住,您可以在PlayerTurnMove方法中執行的所有操作都不會在其PlayerTurnMove更改playerMoveAttemptplayerMovematchAmount的值,盡管可以實現此行為。 您應該閱讀有關可變范圍的內容

由於您的方法僅返回playerMoveAttempt的值 ,因此可以將您的方法的結果分配給變量,例如上面的代碼示例。


我意識到這是一項家庭作業,您正在學習基礎知識,只是想指出您應該考慮學習static類/方法,何時使用它們以及何時不使用它們。 盡管上面的代碼示例應該可以工作,但是解決您的問題的更優雅的解決方案是使用您的類,如下所示:

public class PlayersTurn
{

    public int PlayerMoveAttempt { get; set; }
    public int PlayerMove{ get; set; }
    public int MatchAmount{ get; set; }

    public PlayersTurn(int playerMoveAttempt, int playerMove, int matchAmount)
    {
        this.PlayerMoveAttempt = playerMoveAttempt;
        this.PlayerMove = playerMove;
        this.MatchAmount = matchAmount;
    }

    public void PlayerTurnMove()
    {
        while (this.playerMoveAttempt < 1 || this.playerMoveAttempt > 3)
        {
            Console.WriteLine("Please take between 1 and 3 matches");
            this.playerMoveAttempt = Convert.ToInt32(Console.ReadLine());
        }    
    }
}

...然后從控制台應用程序更改您的方法:

void TurnPlayer()
{
    PlayersTurn playerMoved = new PlayersTurn(playerMoveAttempt, playerMove, matchAmount);
    playerMoved.PlayerTurnMove();

    Console.WriteLine(playerMoved.PlayerMoveAttempt);
    playerMove = playerMoved.PlayerMoveAttempt;
    matchAmount = playerMoved.MatchAmount - playerMoved.PlayerMove;
} 

仍然不是最好的設計,因為在類中使用Console.ReadLine()通常不是一個好主意,因為此方法與Console Applications高度結合,並且之所以起作用是因為我們在其中使用了該類。

如果您曾經想在另一種應用程序(例如Windows窗體,Web應用程序)中重用該類及其邏輯,則此方法將無法正常工作。 您的班級不應意識到從用戶那里獲取輸入的機制

我想現在就可以了,因為您只是在學習,也許您的老師的下一步就是完全重構這個,但是如果您開始嘗試理解這些概念,那就太好了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM