2013-07-28 73 views
-2
namespace WindowsFormsApplication2 
{ 
    public partial class Form1 : Form 
    { 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      Not.Text = 
       (Convert.ToInt32(quiz1per.Text)) * (Convert.ToInt32(quiz1poi.Text)) + 
       (Convert.ToInt32(quiz2per.Text)) * (Convert.ToInt32(quiz2poi.Text)) + 
       (Convert.ToInt32(odev1per.Text)) * (Convert.ToInt32(odev1poi.Text)) + 
       (Convert.ToInt32(odev2per.Text)) * (Convert.ToInt32(odev2poi.Text)) + 
       (Convert.ToInt32(vizeper.Text)) * (Convert.ToInt32(vizepoi.Text)) + 
       (Convert.ToInt32(finalper.Text)) * (Convert.ToInt32(finalpoi.Text)); 
     } 
    } 
} 

一旦我到了最後的分號,我有一個問題轉換INT爲字符串。 我剛剛開始C#,我想先學習基本的東西。問題轉換「詮釋」到「線」,也與數學運算符

回答

0

在C#中的每個對象都有一個ToString()。您可以致電ToString()對您的結果。

private void button1_Click(object sender, EventArgs e) 
{ 
    int result = 
     (Convert.ToInt32(quiz1per.Text)) * (Convert.ToInt32(quiz1poi.Text)) + 
     (Convert.ToInt32(quiz2per.Text)) * (Convert.ToInt32(quiz2poi.Text)) + 
     (Convert.ToInt32(odev1per.Text)) * (Convert.ToInt32(odev1poi.Text)) + 
     (Convert.ToInt32(odev2per.Text)) * (Convert.ToInt32(odev2poi.Text)) + 
     (Convert.ToInt32(vizeper.Text)) * (Convert.ToInt32(vizepoi.Text)) + 
     (Convert.ToInt32(finalper.Text)) * (Convert.ToInt32(finalpoi.Text)); 
    Not.Text = result.ToString(); 
} 

您還需要使用int.TryParse以及。如果在文本框中輸入非數字值Convert.ToInt32將引發異常。

0

您無法將int分配給textBox的文本屬性。這兩種格式中的int一個字符串,

Not.Text = 
     (Convert.ToInt32(quiz1per.Text)) * (Convert.ToInt32(quiz1poi.Text)) + 
     (Convert.ToInt32(quiz2per.Text)) * (Convert.ToInt32(quiz2poi.Text)) + 
     (Convert.ToInt32(odev1per.Text)) * (Convert.ToInt32(odev1poi.Text)) + 
     (Convert.ToInt32(odev2per.Text)) * (Convert.ToInt32(odev2poi.Text)) + 
     (Convert.ToInt32(vizeper.Text)) * (Convert.ToInt32(vizepoi.Text)) + 
     (Convert.ToInt32(finalper.Text)) * (Convert.ToInt32(finalpoi.Text)).ToString(); 

或將其轉換爲字符串

Not.Text = 
     (string)(Convert.ToInt32(quiz1per.Text)) * (Convert.ToInt32(quiz1poi.Text)) + 
     (Convert.ToInt32(quiz2per.Text)) * (Convert.ToInt32(quiz2poi.Text)) + 
     (Convert.ToInt32(odev1per.Text)) * (Convert.ToInt32(odev1poi.Text)) + 
     (Convert.ToInt32(odev2per.Text)) * (Convert.ToInt32(odev2poi.Text)) + 
     (Convert.ToInt32(vizeper.Text)) * (Convert.ToInt32(vizepoi.Text)) + 
     (Convert.ToInt32(finalper.Text)) * (Convert.ToInt32(finalpoi.Text)); 
+1

您不能在C#中將'int'強制轉換爲'string'。你的第二個例子甚至不會編譯。 – shf301

0

由於錯誤明確指出,您不能將int分配給應該採用string的屬性。

您可以撥打ToString()方法將數字轉換爲string