0
所以我試圖做一個窗口形式猜數字遊戲,很簡單,但是當我點擊猜測時,無論標籤上升一個。我認爲,儘管有它們成爲全球userGuess這可能是由於我的變量仍然出來作爲一個局部變量...` 註釋掉userScore消除這個問題,但我還是不明白爲什麼邏輯失敗C#按鈕點擊和私有變量
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
namespace Guess_The_Number_Form
{
public partial class Form1 : Form
{
private int userScore;
private int randNum;
private int userGuess;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
txtBoxGuess.Hide()
;
}
private void toolTip1_Popup(object sender, PopupEventArgs e)
{
}
private void btnRandom_Click(object sender, EventArgs e)
{
Random rand = new Random();
int randNum = rand.Next(0, 10);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
userGuess = Convert.ToInt32(txtBoxGuess.Text);
}
private void txtBoxGuess_Enter(object sender, EventArgs e)
{
}
private void btnGuess_Click(object sender, EventArgs e)
{
if (userGuess == randNum)
{
// userScore++;
lbluserScore.Text = userScore.ToString();
lbluserScore.Text = $"{userScore}";
}
else if (userGuess != randNum)
{
userScore--;
lbluserScore.Text = userScore.ToString();
lbluserScore.Text = $"{userScore}";
}
else if (userScore < 0)
{
lbluserScore.Text = Color.Red.ToString();
}
}
}
}
啊我真是個白癡,謝謝!所以用於控制檯應用程序我忘記了它的方式。試圖修復userGuess錯誤,因爲我們說 編輯:修正,歡呼隊友! –