我应该怎么做才能显示获得有利数字所需的循环次数?

问题描述 投票:0回答:2

我是编程的新手,我正在尝试制作一个骰子程序,用户可以输入他们想要做多少次投掷,然后一个列表将显示获取特定数字所花费的数量,在这种情况下,该数字是6(后来我想为所有数字1-6制作它)我该怎么做呢?

我目前正在尝试使用if语句来识别特定号码的滚动时间,目前我希望程序能够识别号码6,但我不知道如何显示获取该号码所需的滚动量,列表,并保持循环,直到所有卷已执行。

private void Btnkast_Click(object sender, EventArgs e)
{
    bool throws;
    int numberofthrows = 0;

    int dice;
    Random dicethrow = new Random();
    throws = int.TryParse(rtbantal.Text, out numberofthrows);
    int[] list = new int[numberofthrows];

    for (int i = 0; i <= numberofthrows; i++)
    {
        dice = dicethrow.Next(1, 7);
        if (dice == 6)
        {...}
    }
}

另外,我使用tryparse的唯一原因是在必须处理字符串值时防止崩溃。

c#
2个回答
0
投票

我已经使用C#控制台应用程序为您编写了此文件,但我确信您将能够编辑它以满足您对Windows窗体的要求。

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        Random rnd = new Random(); // create Random object

        Console.WriteLine("Enter a number between  1 and 6: "); // prompt user to enter a number between 1 and 6
        int chosenNumberInt;
        var chosenNumber = int.TryParse(Console.ReadLine(), out chosenNumberInt); // check to see if user actually entered a number.  If so, put that number into the chosenNumberInt variable


        Console.WriteLine("How many rolls would you like?"); // prompt user to enter how many rolls they would like to have
        int chosenRollsInt;
        var chosenRolls = int.TryParse(Console.ReadLine(), out chosenRollsInt);

        Console.WriteLine(); // to create space
        Console.WriteLine(); // to create space

        Console.WriteLine("Chosen Number = " + chosenNumberInt + " --- Chosen Rolls = " + chosenRollsInt); // show user what they entered
        Console.WriteLine("------------");

        int count = 0;
        int numberRolled = 0;
        var lstRolls = new List<int>(); // create list object

        for(int i = 1; i <= chosenRollsInt; i++)
        {
            count++;
            int dice = rnd.Next(1, 7);
            numberRolled = dice;

            lstRolls.Add(numberRolled); // add each roll to the list

            Console.WriteLine("Roll " + i + " = " + numberRolled); // show each roll
        }

        var attempts = lstRolls.Count; // how many rolls did you do
        var firstIndexOfChosenNumber = lstRolls.FindIndex(x => x == chosenNumberInt) + 1; // have to add 1 because finding the index is 0-based
        Console.WriteLine("-------------");

        if(firstIndexOfChosenNumber == 0)
            Console.WriteLine("The chosen number was " + chosenNumberInt + " and that number was NEVER rolled with " + chosenRollsInt + " rolls.");
        else
            Console.WriteLine("The chosen number was " + chosenNumberInt + " and the number of rolls it took to hit that number was " + firstIndexOfChosenNumber);
    }
}

我没有添加的东西是验证,以确保用户确实输入1到6之间的数字,但你可以这样做,我敢肯定。

我创建了一个DotNetFiddle,证明这个代码确实有用,甚至可以向你显示每个卷。

如果这有帮助或者您需要更多帮助,请告诉我。

UPDATE

根据您对我原始帖子的评论,我编辑了我的代码,允许用户输入他们想要的号码,以及多少卷。然后,一旦完成所有卷,我找到他们在开头选择的第一次出现的索引。

如果这是你想要的,请告诉我。


0
投票

阅读我在代码中添加的评论

private void Btnkast_Click(object sender, EventArgs e)
{
    bool throws;
    int numberofthrows = 0;

    int dice;
    Random dicethrow = new Random();
    throws = int.TryParse(rtbantal.Text, out numberofthrows);
    List<int> list = new List<int>(); //I changed this to a list

    for (int i = 0; i < numberofthrows; i++)
    {

        dice = dicethrow.Next(1, 7);
        list.Add(dice); //add every roll to the array to check later the values if you want
        if (dice == 6)
        {
           //Print that you found 6 at the roll number list.Count
           Console.WriteLine("Found 6 at roll number: " + list.Count);
           break; //probably break the loop so you won't continue doing useless computation
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.