当所有骰子掷出相同的数字时中断 while 循环
breaks while loop when all die rolls the same number
using System;
using System.Collections.Generic;
namespace AnyDice
{
class Program
{
static void Main(string[] args)
{
int diceSides;
int rollDie;
int count = 0;
bool keepRolling = true;
List<int> num = new List<int>();
Random random = new Random();
Console.Write("Write the number of sides of your die: ");
diceSides = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Type the numbers of the die");
for (int i = 0; i < diceSides; i++)
{
int rank = 1 + i;
Console.Write(rank + "~~> ");
num.Add(Convert.ToInt32(Console.ReadLine()));
}
num.Sort();
Console.WriteLine("\nHere's the die and its contents");
for (int i = 0; i < num.Count; i++)
{
Console.Write("[");
Console.Write(num[i]);
Console.Write("]");
}
Console.WriteLine("\nHow many times do you want to roll at once");
rollDie = Convert.ToInt32(Console.ReadLine());
while (keepRolling)
{
for (int i = 0; i < rollDie; i++)
{
Console.Write("[");
Console.Write(num[random.Next(num.Count)]);
Console.Write("]");
count++;
}
Console.ReadLine();
}
Console.WriteLine("It took you " + count + " attempts");
Console.ReadLine();
}
}
}
例如,如果 (4,4,4,4) 滚动或 (2,2) 在任何“n”列中,while 循环就会中断。
我想将每个骰子滚动值存储在另一个数组列表中并比较其中的每个值。如果它完全相等,那么它就会崩溃。但我不知道如何实现它。
我不是 100% 确定我理解你的要求,但无论如何,你应该编写一个单独的函数,returns 一个标志,指示数组是否处于应触发中断的状态。
bool KeepRolling(int[] num)
{
for (int i=0; i<num.Length; i++)
{
if (num[i] >= i) return false;
}
return true;
}
然后在你的循环中调用它:
keepRolling = KeepRolling(num);
我们有 Linq。它位于 System.Linq
命名空间中,这可能对您有所帮助。
我应该有两种方法来检查所有模具是否相同:
int first = dies.First();
if (dies.All(i => i == first))
{
// break if all are equals to the first die
}
或者使用 Distinct
我们可以过滤掉任何副本。
if (dies.Distinct().Count() == 1)
{
// if we only have unique items and the count is 1 every die is the same
}
while (keepRolling)
{
rolls.Clear();
for (int i = 0; i < rollDie; i++)
{
var firstRoll = num[random.Next(num.Count)];
rolls.Add(firstRoll);
Console.Write(firstRoll + " ");
count++;
}
if (rolls.Distinct().Count() == 1)
{
Console.WriteLine("It took you " + count + " attempts");
keepRolling = false;
break;
}
Console.ReadLine();
}
using System;
using System.Collections.Generic;
namespace AnyDice
{
class Program
{
static void Main(string[] args)
{
int diceSides;
int rollDie;
int count = 0;
bool keepRolling = true;
List<int> num = new List<int>();
Random random = new Random();
Console.Write("Write the number of sides of your die: ");
diceSides = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Type the numbers of the die");
for (int i = 0; i < diceSides; i++)
{
int rank = 1 + i;
Console.Write(rank + "~~> ");
num.Add(Convert.ToInt32(Console.ReadLine()));
}
num.Sort();
Console.WriteLine("\nHere's the die and its contents");
for (int i = 0; i < num.Count; i++)
{
Console.Write("[");
Console.Write(num[i]);
Console.Write("]");
}
Console.WriteLine("\nHow many times do you want to roll at once");
rollDie = Convert.ToInt32(Console.ReadLine());
while (keepRolling)
{
for (int i = 0; i < rollDie; i++)
{
Console.Write("[");
Console.Write(num[random.Next(num.Count)]);
Console.Write("]");
count++;
}
Console.ReadLine();
}
Console.WriteLine("It took you " + count + " attempts");
Console.ReadLine();
}
}
}
例如,如果 (4,4,4,4) 滚动或 (2,2) 在任何“n”列中,while 循环就会中断。 我想将每个骰子滚动值存储在另一个数组列表中并比较其中的每个值。如果它完全相等,那么它就会崩溃。但我不知道如何实现它。
我不是 100% 确定我理解你的要求,但无论如何,你应该编写一个单独的函数,returns 一个标志,指示数组是否处于应触发中断的状态。
bool KeepRolling(int[] num)
{
for (int i=0; i<num.Length; i++)
{
if (num[i] >= i) return false;
}
return true;
}
然后在你的循环中调用它:
keepRolling = KeepRolling(num);
我们有 Linq。它位于 System.Linq
命名空间中,这可能对您有所帮助。
我应该有两种方法来检查所有模具是否相同:
int first = dies.First();
if (dies.All(i => i == first))
{
// break if all are equals to the first die
}
或者使用 Distinct
我们可以过滤掉任何副本。
if (dies.Distinct().Count() == 1)
{
// if we only have unique items and the count is 1 every die is the same
}
while (keepRolling)
{
rolls.Clear();
for (int i = 0; i < rollDie; i++)
{
var firstRoll = num[random.Next(num.Count)];
rolls.Add(firstRoll);
Console.Write(firstRoll + " ");
count++;
}
if (rolls.Distinct().Count() == 1)
{
Console.WriteLine("It took you " + count + " attempts");
keepRolling = false;
break;
}
Console.ReadLine();
}