C# 方法不会从另一个方法返回随机数

C# 方法不会从另一个方法返回随机数,c#,class,C#,Class,很难为这个问题找到合适的标题。我之前玩这个骰子游戏没什么问题,我解决了,谢谢大家 我的游戏已经99%准备好了,但是主程序无法返回骰子号码。这场比赛是为了打破当玩家有三个胜利,这使他成为一个赢家。这就是为什么有圆的 控制台应如下所示: 玩家1的名字: 玩家2的名字: 第一轮 “玩家1名”:5+4=9 “玩家2姓名”:1+2=3 第二轮等等 当玩家获得三场胜利时,就是赢家。我还有两个其他类,但问题是骰子,所以我不附加其他在这里 班级游戏 using System; using System.Coll

很难为这个问题找到合适的标题。我之前玩这个骰子游戏没什么问题,我解决了,谢谢大家

我的游戏已经99%准备好了,但是主程序无法返回骰子号码。这场比赛是为了打破当玩家有三个胜利,这使他成为一个赢家。这就是为什么有圆的

控制台应如下所示:

玩家1的名字:

玩家2的名字:

第一轮

“玩家1名”:5+4=9

“玩家2姓名”:1+2=3

第二轮等等

当玩家获得三场胜利时,就是赢家。我还有两个其他类,但问题是骰子,所以我不附加其他在这里

班级游戏

using System;
using System.Collections.Generic;
using System.Text;
using static System.Console;

namespace Dicegame
{
    static class Game
    {
        static int winline = 3;

        static int round = 1;

        public static void Aja()
        {
            Player p1 = new Player
            (
                Helpers.Syote.Merkkijono("Name of player 1: ")
            );
            Player p2 = new Player
            (
                Helpers.Syote.Merkkijono("Name of player 2: ")
            );

            Dice dice1 = new Dice();
            Dice dice2 = new Dice();


            for (int i = 1; p1.Points < winline | p1.Points < winline; i++)
            {
                int p1throw1 = dice1.Throw(), p1throw2 = dice2.Throw(), p1total = p1throw1 + p1throw2,
                    p2throw1 = dice1.Throw(), p2throw2 = dice2.Throw(), p2total = p2throw1 + p2throw2;

                round++;
            }
        }
    }
}

您根本无法实现P1或P2的要点。
此外,您还可以使用for循环,其中简单的while循环可以更好地工作:

while(p1.Points < winline || p2.Points < winline)
{
    p1.AddPoints(dice1.Throw() + dice2.Throw());
    p2.AddPoints(dice1.Throw() + dice2.Throw());
    round++;
}
while(p1.Points
我发布了我的解决方案,我希望它对将来的人有所帮助。对所有内容进行了注释,因此对于初学者来说也很容易理解

do 
    {
        if (p1.Points == WINLINE || p2.Points == WINLINE)
        {
            break; // Break this loop when either one reach WINLINE points (3)
        }
        else
        {
                // Get random numbers for each throw between 1-6 and sum them together
            int p1throw1 = dice1.Throw(), p1throw2 = dice2.Throw(), p1total = p1throw1 + p1throw2,

                // Get random numbers for each throw between 1-6 and sum them together
                p2throw1 = dice1.Throw(), p2throw2 = dice2.Throw(), p2total = p2throw1 + p2throw2; 

            // Write round number which is how many times dice is thrown. Split with 2, because same dice is throwed two times in one round
            WriteLine($"Round {dice1.ThrowCount / 2}");

            // Write given player names and points of each throw and after all sum of those points
            WriteLine($"{p1.Name}: {p1throw1} + {p1throw2} = {p1total}");
            WriteLine($"{p2.Name}: {p2throw1} + {p2throw2} = {p2total}");

            // If player 1 get more points than player 2, add one point for player 1 and reset player 2 points
            if (p1total > p2total)
            {
                p1.Points++;
                p2.Points = 0;
            }

            // If player 2 get more points than player 1, add one point for player 2 and reset player 1 points
            else if (p1total < p2total)
            {
                p2.Points++;
                p1.Points = 0;
            }
                else // If something else (same points for both) do nothing
            {

        }
    }
}
while (true);

// When do loop broke print results of the game
if (p1.Points == WINLINE) // Print this if player 1 wins
{
    WriteLine($"Winner of the game is {p1.Name} and total rounds: {dice1.ThrowCount / 2}."); 
}
else if (p2.Points == WINLINE) // Print this if player 2 wins
{
    WriteLine($"Winner of the game is {p2.Name} and total rounds: {dice1.ThrowCount / 2}");

}
do
{
如果(p1.Points==WINLINE | | p2.Points==WINLINE)
{
break;//当其中一个到达WINLINE点(3)时中断此循环
}
其他的
{
//获得1-6之间每次投掷的随机数,并将其相加
int p1throw1=dice1.Throw(),p1throw2=dice2.Throw(),p1total=p1throw1+p1throw2,
//获得1-6之间每次投掷的随机数,并将其相加
p2throw1=dice1.Throw(),p2throw2=dice2.Throw(),p2total=p2throw1+p2throw2;
//写下掷骰子的轮数。用2分开,因为同一个骰子在一轮中掷两次
写线($“Round{dice1.ThrowCount/2}”);
//写下给定的球员姓名和每次投掷的分数,以及这些分数的总和
写线($“{p1.Name}:{p1throw1}+{p1throw2}={p1total}”);
写线($“{p2.Name}:{p2throw1}+{p2throw2}={p2total}”);
//如果玩家1得到的分数比玩家2多,则为玩家1添加一分并重置玩家2分
如果(p1总计>p2总计)
{
p1.点数++;
p2.点=0;
}
//如果玩家2得到的分数比玩家1多,则为玩家2添加一分并重置玩家1的分数
否则如果(p1total
In
Random。下一个(磅,磅)
lb是包含的,ub是独占的。您的骰子将始终返回[1,5]范围内的值。您应该调用
randomnumber.Next(1,7)
。可读性建议:为每条语句声明一个变量,而不是在一条语句中声明6个变量。还要注意
中(int i=1;p1.Points
。你重复了两次
p1
。添加点有什么意义。我的课程只有分数。我得到了这个错误
'Player'不包含'AddPoints'的定义,并且找不到可访问的扩展方法'AddPoints',接受类型为'Player'的第一个参数(您是否缺少using指令或程序集引用?
@masteroscar这就是问题所在,您有很多包含点的变量,但是你对他们什么也不做。您需要使用
AddPoints
(我刚刚发明的,如果不先编写,您将找不到)或类似的方法将它们添加到
Player
。否则,循环的条件将永远无法满足,您将永远卡在循环中:
p1.点
p2.点
将永远无法
=winline
,因为您永远不会修改它们。@alvinastor谢谢。我会尽力解决的,谢谢你的提示。
do 
    {
        if (p1.Points == WINLINE || p2.Points == WINLINE)
        {
            break; // Break this loop when either one reach WINLINE points (3)
        }
        else
        {
                // Get random numbers for each throw between 1-6 and sum them together
            int p1throw1 = dice1.Throw(), p1throw2 = dice2.Throw(), p1total = p1throw1 + p1throw2,

                // Get random numbers for each throw between 1-6 and sum them together
                p2throw1 = dice1.Throw(), p2throw2 = dice2.Throw(), p2total = p2throw1 + p2throw2; 

            // Write round number which is how many times dice is thrown. Split with 2, because same dice is throwed two times in one round
            WriteLine($"Round {dice1.ThrowCount / 2}");

            // Write given player names and points of each throw and after all sum of those points
            WriteLine($"{p1.Name}: {p1throw1} + {p1throw2} = {p1total}");
            WriteLine($"{p2.Name}: {p2throw1} + {p2throw2} = {p2total}");

            // If player 1 get more points than player 2, add one point for player 1 and reset player 2 points
            if (p1total > p2total)
            {
                p1.Points++;
                p2.Points = 0;
            }

            // If player 2 get more points than player 1, add one point for player 2 and reset player 1 points
            else if (p1total < p2total)
            {
                p2.Points++;
                p1.Points = 0;
            }
                else // If something else (same points for both) do nothing
            {

        }
    }
}
while (true);

// When do loop broke print results of the game
if (p1.Points == WINLINE) // Print this if player 1 wins
{
    WriteLine($"Winner of the game is {p1.Name} and total rounds: {dice1.ThrowCount / 2}."); 
}
else if (p2.Points == WINLINE) // Print this if player 2 wins
{
    WriteLine($"Winner of the game is {p2.Name} and total rounds: {dice1.ThrowCount / 2}");

}