C# 在使用随机整数模拟骰子滚动的方法中,Do while循环不起作用
我正在用C#开发一个骰子模拟器,我的do-while循环并没有像我预期的那样工作。我现在已经注释掉了,但是没有注释,骰子模拟器方法的结果总是返回“2”(因为do/while没有检查骰子的总和是否等于用户输入)。是否有人愿意帮助新手纠正代码,使程序正常运行 提前感谢您的帮助C# 在使用随机整数模拟骰子滚动的方法中,Do while循环不起作用,c#,C#,我正在用C#开发一个骰子模拟器,我的do-while循环并没有像我预期的那样工作。我现在已经注释掉了,但是没有注释,骰子模拟器方法的结果总是返回“2”(因为do/while没有检查骰子的总和是否等于用户输入)。是否有人愿意帮助新手纠正代码,使程序正常运行 提前感谢您的帮助 //Begin Program using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst
//Begin Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static int DiceRollSim(int input)
{
Random dice1 = new Random();
Random dice2 = new Random();
int sum = dice1.Next(1, 7) + dice2.Next(1, 7);
int count = 1;
/* do
{*/
{
sum = dice1.Next(1, 7) + dice2.Next(1, 7);
count++;
}
/* } while (sum != input);*/
return count;
}
static void Main(string[] args)
{
Console.WriteLine("Hello! This program rolls a pair of die until the sum of the die is equal to your given number.\n\n");
Console.WriteLine("Please enter a number between 2 and 12: ");
int num = int.Parse(Console.ReadLine());
int rolls = DiceRollSim(num);
int[] array_possiblesum = new int[] { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
if (Array.IndexOf(array_possiblesum, num) > -1)
{
Console.WriteLine("\n\n Congratulations! It took " + rolls + " rolls of the die for them to equal your entry, " + num + ".");
}
else
{
Random newnum = new Random();
num = newnum.Next(2, 13);
int roll2 = DiceRollSim(num);
Console.WriteLine("\n\n I'm sorry! Your input was not valid so we chose a new number for you. The number we chose is:" + num+".");
Console.WriteLine(" \n\n It took " + roll2 + " rolls of the die for them to equal your entry, " + num + ".");
}
}
}
}
您可以同时(或多或少)创建两个Random实例。各国: 通过调用默认构造函数连续创建的不同随机对象将具有相同的默认种子值,因此将生成相同的随机数集 因此,dice1.Next()和dice2.Next()将始终返回相同的值,如果输入不能被2整除,则循环将永远运行
两个骰子只需使用一个实例。您不需要有两个Random类实例。您可以同时(或多或少)创建两个Random实例。各国: 通过调用默认构造函数连续创建的不同随机对象将具有相同的默认种子值,因此将生成相同的随机数集 因此,dice1.Next()和dice2.Next()将始终返回相同的值,如果输入不能被2整除,则循环将永远运行
两个骰子只需使用一个实例。您不需要有两个随机类的实例。您不需要两个随机类,两个随机类的随机性明显低于一个。此外,您不应该每次都重新实例化随机类,只需在开始时实例化一次即可 因此,请尝试这样的课程:
class Program
{
Random dice1 = new Random();
static int DiceRollSim(int input)
{
int sum = dice1.Next(1, 7) + dice1.Next(1, 7);
int count = 1;
/* do
{*/
{
sum = dice1.Next(1, 7) + dice1.Next(1, 7);
count++;
}
/* } while (sum != input);*/
return count;
}
您不需要两个随机类,拥有两个类的随机性明显低于一个类。此外,您不应该每次都重新实例化随机类,只需在开始时实例化一次即可 因此,请尝试这样的课程:
class Program
{
Random dice1 = new Random();
static int DiceRollSim(int input)
{
int sum = dice1.Next(1, 7) + dice1.Next(1, 7);
int count = 1;
/* do
{*/
{
sum = dice1.Next(1, 7) + dice1.Next(1, 7);
count++;
}
/* } while (sum != input);*/
return count;
}
注意:当您同时实例化两个随机类时,它们通常给出相同的“随机”数列表。这意味着您只能滚动偶数。当用户输入无效号码时,您的代码仍将调用DicerlSIM。(如果一个邪恶的用户输入了“A”?)如果你在循环中重写它,那么初始化
sum
有什么意义?它是以前(可能)使用的while
循环的残余吗?注意:当您同时实例化两个随机类时,它们通常给出相同的“随机”数列表。这意味着您只能滚动偶数。当用户输入无效号码时,您的代码仍将调用DicerlSIM。(如果一个邪恶的用户输入了“A”?)如果你在循环中重写它,那么初始化sum
有什么意义?它是以前(可能)使用的while
循环的残余吗?