我应该怎么做才能显示要得到一个有利数所需的循环次数?

时间:2019-04-11 18:38:53

标签: c#

我是编程新手,目前正在尝试制作骰子程序,在这种情况下,用户可以输入他们想进行的掷骰子数量,然后列表将显示获得特定数字所花费的掷骰子数量这个数字是6(后来我想将所有数字1-6都设为6)我应该怎么做?

我目前正在尝试使用if语句来识别何时滚动特定号码,目前我希望程序识别号码6,但是我有点不确定如何显示获取该号码所花费的掷骰数量,并在列表中保持循环,直到执行完所有掷骰为止。

Option Explicit

Public Sub GetInfo()
    Dim html As HTMLDocument, csrft As String, lastRow As Long, sourceValues() '<  VBE > Tools > References > Microsoft HTML Object Library
    Set html = New HTMLDocument
    Dim ws As Worksheet, i As Long
    Set ws = ThisWorkbook.Worksheets("Sheet4")
    lastRow = ws.Cells(ws.rows.Count, "B").End(xlUp).Row
    sourceValues = ws.Range("B2:D" & lastRow).Value
    Dim results()
    ReDim results(1 To UBound(sourceValues, 1), 1 To 4)
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://www.partstown.com", False
        .send
        html.body.innerHTML = .responseText

        csrft = html.querySelector("[name=CSRFToken]").Value
        Stop
        For i = LBound(sourceValues, 1) To UBound(sourceValues, 1)
            If sourceValues(i, 1) <> vbNullString And sourceValues(i, 3) <> vbNullString Then
                DoEvents
                .Open "POST", "https://www.partstown.com/track-my-order", False
                .setRequestHeader "Referer", "https://www.partstown.com/track-my-order"
                .setRequestHeader "User-Agent", "Mozilla/5.0"
                .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
                .setRequestHeader "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
                .setRequestHeader "Accept-Encoding", "gzip, deflate"
                .setRequestHeader "Accept-Language", "en-US,en;q=0.9"
                .send "orderNo=" & sourceValues(i, 1) & "&postalCode=" & sourceValues(i, 3) & "&CSRFToken=" & csrft

                html.body.innerHTML = .responseText

                Dim shipping As String, order As String, items() As String

                shipping = html.querySelector("[data-label=Shipping]").innerText
                order = html.querySelector(".order-history__item-descript--min").innerText
                items = Split(order, vbNewLine)

                Dim qtyOrdered As Long, qtyShipped As String, product As String

                qtyOrdered = CLng(Replace$(items(0), "Qty Ordered: ", vbNullString))
                qtyShipped = CLng(Replace$(items(1), "Qty Shipped: ", vbNullString))
                product = html.querySelector(".details-table a").Title

                results(i, 1) = shipping
                results(i, 2) = qtyOrdered
                results(i, 3) = qtyShipped
                results(i, 4) = product
            End If
            'Application.Wait Now + TimeSerial(0, 0, 1)
        Next
    End With
    'results written out from row 2 column E
    ws.Cells(2, 5).Resize(UBound(results, 1), UBound(results, 2)) = results
End Sub

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

2 个答案:

答案 0 :(得分: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,证明该代码确实有效,甚至可以向您显示每一卷。

让我知道这是否有帮助,或者您是否需要其他帮助。

更新

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

让我知道这是否是您想要的。

答案 1 :(得分: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
        }
    }
}