2016-09-13 157 views
-4

我很抱歉問。但我是C#的新手。需要幫助代碼不起作用

我的代碼有問題(Visual Studio告訴我這個)。但我找不到什麼是錯的。

你能幫我嗎?

我只是在嘗試一些簡單的互動遊戲。

namespace FSociety 
{ 
    public partial class Form2 : Form 
    { 
     public Form2() 
     { 
      InitializeComponent(); 
     } 

     private void textBox1_TextChanged(object sender, EventArgs e) 
     { 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      if (textBox1.Text == "root" 
       && textBox2.Text == "toor") ;   
      { 
       progressBar1.Increment(100); 

      **}** 
      else 
      { 
       MessageBox.Show("Wrong username or password"); 
      } 
     } 
    } 
} 

加粗}的Visual Studio後告訴我期望}但它已經在那裏,當我再添加一個我有一個5個錯誤。

請幫忙。

謝謝。

+3

除了別的,你在'&& textBox2.Text ==「toor」)之後有一個流氓'''' –

+0

如果這就是你所有的代碼,你肯定需要這個支架。那麼當你把它放進去時會得到什麼樣的錯誤?拿出分號應該修復它。 –

回答

0

刪除;您的if語句後

+0

是的。有效。非常感謝。 :-) –

3
private void button1_Click(object sender, EventArgs e) 
    { 
     if (textBox1.Text == "root" && textBox2.Text == "toor") // there was a ; at the end of the if 
     { 
      progressBar1.Increment(100); 
     } 
     else 
     { 
      MessageBox.Show("Wrong username or password"); 
     } 
    } 

提示:如果visual studio無法格式化代碼,您應該檢查所有結束標記。 visual studio識別代碼結構,並使代碼看起來更好。

+0

謝謝。它有幫助。 :-) –

2
namespace FSociety { 

public partial class Form2 : Form 
{ 
    public Form2() 
    { 

      InitializeComponent(); 
    } 

    private void textBox1_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     if (textBox1.Text == "root" 
      && textBox2.Text == "toor")   
      { 
      progressBar1.Increment(100); 

      } 

     else 
     { 
      MessageBox.Show("Wrong username or password"); 

     } 

    } 
} 

} 

這是工作代碼。 C#中的 您不必放置任何; if(條件)後的

正確的語法是:

if(condition) 
{ 
    //true condition 
} 
else 
{ 
    //false condition 
} 

希望這有助於。

+0

非常感謝。我讚賞它。 :-) –

0

只要你知道未來,如果你得到一個錯誤,說「意外的[字符]」它實際上意味着什麼是「嘿,我希望有那個字符之前有東西」。例如,如果我有這樣的代碼

function foo(){ 
    print 'bar' 
} 

我將得到一個錯誤,指出「意外'}'」。這是因爲計算機後立即「打印‘欄’」期待一個分號因此,這會修正這個錯誤

function foo(){ 
    print 'bar'; 
} 

因此,當你「意外X」啓動X的東西,你離開之前右視(或無意中加入了額外的)

+0

哦,好的,非常感謝你。 :-) –