[英]Boolean help in c# How to format whether or not random1 matches num1
我被指示這樣做:“在布爾值中跟蹤每個數字的匹配狀態,檢查所有三個布爾值匹配變量,如果它們都為真,則我們與獲勝數字匹配,退出while循環”
我90%完成了這項作業,但不確定我是否正確使用bool。
我假設他要我檢查randomNum1是否匹配userEnteredNum1。
但我正在努力格式化。
我首先這樣做:
bool doTheyMatch1 = true
但是我不知道從那里去哪里。 所以我去了一個比較熟悉的“如果”陳述
if (random1 = userEntered1stDigit)
並收到“無法將類型int隱式轉換為bool”錯誤。 任何建議或有用的鏈接都是最歡迎的!
為了更有用,這是我的整個代碼:
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
輕松,用==
更改=
在任何C風格中, =
分配並返回分配的值, ==
進行布爾運算並返回true
或false
另外,您可以避免
bool theyMatch = (numberOne == numberTwo) && (numberThree == numberFour) && (numberFive == numberSix);
另外,在測試bool
,無需與true進行比較,因為它已經是bool
if(theyMatch == true)
{
//your code
}
//Behaves the same as
if(theyMatch)
{
//Your code
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.