首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c#中的布尔帮助--如何格式化random1是否与num1匹配

c#中的布尔帮助--如何格式化random1是否与num1匹配
EN

Stack Overflow用户
提问于 2014-04-14 02:05:48
回答 1查看 61关注 0票数 0

我被指示这样做:“在布尔检查中跟踪每个数字的匹配状态,检查所有三个布尔匹配变量,如果它们都是真的,那么我们就有一个与获胜数字匹配的,退出while循环”。

我已经完成了90%的任务,但我不知道如何正确地使用bool。

我猜他是想让我看看randomNum1是否和userEnteredNum1匹配。

但我很难把它格式化。

我一开始就是这么做的:

代码语言:javascript
复制
     bool doTheyMatch1 = true

但我不知道从那里往哪里走。所以我去看了一个更熟悉的“如果”声明

代码语言:javascript
复制
    if (random1 = userEntered1stDigit)

并得到“无法隐式将int类型转换为bool”错误。任何建议或有用的链接都是最欢迎的!

更有帮助的是,下面是我的全部代码:

代码语言:javascript
复制
       public Form1()
    {
        InitializeComponent();
    }

    //DECLARE CLASS LEVEL FIELD VARIABLES
    int CONST_cashPayoutPick3 = 500;
    int CONST_cashPayoutPick4 = 5000;
    int CONST_cashPayoutPick5 = 50000;

    private void myBtnGenRandomNumbers_Click(object sender, EventArgs e)
    {
        //DECLARE LOCAL VARIABLES
        int userEntered1stDigit = 0;
        int userEntered2ndDigit = 0;
        int userEntered3rdDigit = 0;


        //INPUT-VALIDATION
        //winning number: validate the 1st digit to be numeric, if not display error message and return
        if (int.TryParse(myTxtBox1stDigit.Text, out userEntered1stDigit))
        {
            //ok
        }
        else
        {
            MessageBox.Show("Please enter a number in the first box");
            return;
        }
        //winning number: validate the 2nd digit to be numeric, if not display error message and return
        if (int.TryParse(myTxtBox2ndDigit.Text, out userEntered2ndDigit))
        {
            //ok
        }
        else
        {
            MessageBox.Show("Please enter a number in the second box");
            return;
        }
        //winning number: validate the 3rd digit to be numeric, if not display error message and return
        if (int.TryParse(myTxtBox3rdDigit.Text, out userEntered3rdDigit))
        {
            //ok
        }
        else
        {
            MessageBox.Show("Please enter a number in the third box");
            return;
        }


        //INITIALIZE ANY VARIABLES
        userEntered1stDigit = int.Parse(myTxtBox1stDigit.Text);
        userEntered2ndDigit = int.Parse(myTxtBox2ndDigit.Text);
        userEntered3rdDigit = int.Parse(myTxtBox3rdDigit.Text);

        //GOOD SO FAR

        //PROCESSING
        //setup your own Random number generator object
        int random1 = 0;
        int random2 = 0;
        int random3 = 0;

        Random myRandomNumberObject = new Random();
        int matchNum1;
        int matchNum2;
        int matchNum3;

        //clear the list box
        myListBoxResults.Items.Clear();

        //set number of attempts = 0
        int myAttemptsInt;
        myAttemptsInt = 0;


        //LOOP THROUGH AND CREATE SETS OF 3 RANDOM DIGITS EACH TIME THROUGH LOOP UNTIL A MATCH IS FOUND OR TRY 1,000 TIMES
        while (myAttemptsInt <= 1000) 
        {

            if (myAttemptsInt <= 999)
            {
                //ok
            }
            else
            {
                break;
            }

            //get next random digit generated from 0 to 9, for your generated digit position 1
            random1 = myRandomNumberObject.Next(10);
            //get next random digit generated from 0 to 9, for your generated digit position 2
            random2 = myRandomNumberObject.Next(10);
            //get next random digit generated from 0 to 9, for your generated digit position 3
            random3 = myRandomNumberObject.Next(10);


            //display the number of match attempts so far

            //attempt # originally went here
            myAttemptsInt = myAttemptsInt + 1;

            int index = myListBoxResults.Items.Add("Attempt # " + myAttemptsInt);

            //display the generated digit 1,2,3 in the labels on the Form
            matchNum1 = random1;
            myLabelGenerated1stDigit.Text = matchNum1.ToString();
            matchNum2 = random2;
            myLabelGenerated2ndDigit.Text = matchNum2.ToString();
            matchNum3 = random3;
            myLabelGenerated3rdDigit.Text = matchNum3.ToString();

            //set the Label BackColor of all the generated digits to Color.LightGray
            myLabelGenerated1stDigit.BackColor= Color.LightGray;
            myLabelGenerated2ndDigit.BackColor = Color.LightGray;
            myLabelGenerated3rdDigit.BackColor = Color.LightGray;

            //for any generated digit that matches the winning digit,
            if (random1 == userEntered1stDigit)
            {
                myLabelGenerated1stDigit.BackColor = Color.LightGreen;
            }
            if (random2 == userEntered2ndDigit)
            {
                myLabelGenerated2ndDigit.BackColor = Color.LightGreen;
            }
            if (random3 == userEntered3rdDigit)
            {
                myLabelGenerated3rdDigit.BackColor = Color.LightGreen;
            }

            //  -keep track of the matching status of each digit in a boolean
            bool doTheyMatch = true;
            if (random1 = userEntered1stDigit)

            //check all three boolean match variables, if they are all true then we have a match to the winning number, exit out of the while
            /*
            if (??)
            {
                //add what happened to the event log

                //break out of while loop since found a match
                break;
                //
            }
            else
            {
                //add what happened to the event log

                //no need to break out of loop; keep looping
            }
            */

        }  // End of While
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-14 02:07:24

简单,用==更改==

在任何C类型中,=分配并返回赋值,==执行布尔操作并返回truefalse

另外,你也可以避免如果做

代码语言:javascript
复制
bool theyMatch = (numberOne == numberTwo) && (numberThree == numberFour) && (numberFive == numberSix);

此外,在测试bool时,不需要与true进行比较,因为它已经是一个bool

代码语言:javascript
复制
if(theyMatch == true)
{
   //your code
}

//Behaves the same as 

if(theyMatch)
{

   //Your code

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23050858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档