2010-11-05 56 views
3

我希望看到label6顯示用戶選擇數字的正確次數。 label7顯示用戶錯誤選擇的次數。它不會遞增1。將數字增加1

錯誤1個運算符「++」不能被應用於類型「字符串」 錯誤2運算符「++」的操作數可以不被應用於類型「字符串」

private void button1_Click(object sender, EventArgs e) 
    { 
     string correct="0"; 
     string incorrect="0"; 
     RandomNumber(0,99); 
     button2.Enabled = true ; 
     button1.Enabled = false; 
     label3.Visible = true; 
     if (textBox1.Text == label1.Text) 
      label3.Text=("Winner"); 
       label6.Text = correct +1; 
       if (textBox1.Text != label1.Text) 
        label7.Text = incorrect= +1; 
      label3.Text=(string.Format("Sorry - You Lose, The number is {0}", label1.Text)); 

    } 


的操作數

編輯(來自OP的回答他自己的問題):

我已經嘗試過你的建議,但數字不會增加一次,我猜錯了。

private void button1_Click(object sender, EventArgs e) 


    { 
     int correct=0; 
     int incorrect=0; 
     RandomNumber(0,99); 
     button2.Enabled = true ; 
     button1.Enabled = false; 
     label3.Visible = true; 
     if (textBox1.Text == label1.Text) 
     { 
      label3.Text = ("Winner"); 
      label6.Text = (++correct).ToString(); 
     } 

     else if (textBox1.Text != label1.Text) 
     { 
      label7.Text = (incorrect+1).ToString(); 

      label3.Text = (string.Format("Sorry - You Lose, The number is {0}", label1.Text)); 
     } 


    } 
+0

或者只是' 「」 +(int.Parse(正確)+1);' – Margus 2010-11-05 20:09:08

+0

這是否即使編譯? – 2010-11-05 20:12:41

+4

即使它起作用,Margus的建議是*錯誤的方式™* – Brian 2010-11-05 20:46:38

回答

4

添加到字符串中,correctincorrect將只添加所添加內容的字符串表示形式。您必須將其轉換爲整數類型,然後遞增,然後再轉換回字符串。然而,將這些變量保存爲整數實例變量會更容易。這種方式增加是微不足道的,你實際上保持正確的計數,而不是每次按鈕被點擊時重置。(實際上有許多問題與代碼)

// instance variables 
private int correct = 0; 
private int incorrect = 0; 

private void button1_Click(object sender, EventArgs e) 
{ 
    RandomNumber(0,99); 
    button2.Enabled = true ; 
    button1.Enabled = false; 
    label3.Visible = true; 
    if (textBox1.Text == label1.Text) 
    { 
     label3.Text=("Winner"); 
     label6.Text = (++correct).ToString(); // convert int to string 
    } 
    // indentation does not indicate the block 
    else //if (textBox1.Text != label1.Text) 
    { 
     label3.Text=(string.Format("Sorry - You Lose, The number is {0}", label1.Text)); 
     label7.Text = (++incorrect).ToString(); 
    } 
} 
10

它看起來並不像你堅持的correctincorrect

創建屬性:

public int Correct { get; set; } 
public int Incorrect { get; set;} 

然後:

private void button1_Click(object sender, EventArgs e) 
{ 
    RandomNumber(0,99); 
    button2.Enabled = true ; 
    button1.Enabled = false; 
    label3.Visible = true; 

    if (textBox1.Text == label1.Text) 
    { 
    label3.Text=("Winner"); 
    label6.Text = (++this.Correct).ToString(); 
    } 
    else 
    { 
     label3.Text=(string.Format("Sorry - You Lose, The number is {0}", label1.Text)); 
     label7.Text = (++this.Incorrect).ToString(); 
    } 
} 
+0

+1,但我真的認爲你也應該編輯縮進。 – Brian 2010-11-05 21:00:45

+0

雖然*技術上正確*(最好的一種正確的;-)),但由於縮進,我猶豫不決,儘管它反映了非常格式的原稿。 – 2010-11-05 21:00:58

+0

我在那裏固定縮進,希望我沒有誤解他的邏輯。 – Gabe 2010-11-05 21:04:21

8

你存儲你的正確,不正確的變量,如string

使用int,而不是像這樣:

int correct = 0; 
int incorrect = 0; 

和你的代碼更改爲:

correct++; 
label6.Text = correct.ToString(); 

和:

incorrect++; 
label7.Text = incorrect.ToString(); 
+0

不應該是'label6.Text = correct.ToString()'和'label7.Text = incorrect.ToString();'而不是?沒有從'int'到'string'的隱式轉換。 – Brian 2010-11-05 20:44:48

+0

是的,謝謝,自從我完成C#以來已經有幾年了。我修正了這個例子。 – 2010-11-05 20:47:04

1

或者你可以使用:

正確=正確+ 1; (我認爲這是你試圖實現的) 不正確=不正確+ 1;

correct + = 1; incorect + = 1;

0
label6.Text = correct +1; 

僅將label6的值設置爲正確+1;它不會改變正確的值。如果你使用:

int correct; 
label6.Text = (++correct).ToString(); 

你應該看到你想要的。

+0

是的,但是因爲correct是一個局部變量,所以改變它是沒有意義的,因爲它不會持久。 – Brian 2010-11-05 21:22:06

+0

@布萊恩:這是正確的;我應該更清楚地說明這個變量應該被聲明爲一個int而不是一個字符串。感謝您指出了這一點。 – 2010-11-07 21:14:11