2016-05-24 50 views
-1
double dblSquare; 
double strlbl; 
double sum; 

dblSquare = double.Parse(txtSquare.Text); 

sum = "the square of" dblSquare "is" dblSquare* dblSquare; 

strlbl = sum; 

我如何得到這個工作,以便我可以讓標籤說「(輸入的數字的平方)是(輸入的數字的平方)」如何使用標籤c#

+1

你指定字符串的雙重價值,而不是用正確的字符串concatentation,然後將該「字符串」分配給另一個double。 – Steve

+0

那麼我該如何解決這個問題 –

+0

看到我的回答馬修,請將值設置爲label.Text –

回答

1

直接把字符串變量賦值這樣

strlbl = "the square of"+ dblSquare+ "is"+ dblSquare* dblSquare; 
1

你需要做的string concatenation象下面這樣:

string strResult = "the square of " + dblSquare + " is " + dblSquare * dblSquare; 

然後

yourlabel.Text = strResult; 

串聯是將一個字符串追加到 另一個字符串的末尾的過程。當通過使用+運算符連接字符串文字或字符串 常量時,編譯器會創建一個單一的 字符串。沒有運行時間連接發生。但是,字符串變量 只能在運行時連接。在這種情況下,您應該瞭解各種方法的性能影響。

1

使用+進行串接。

設置標籤的價值爲:

 
strlbl.Text = "The square of " + dblSquare + " is " + (dblSquare * dblSquare); 
0
string strlbl = string.Format("the square of {0} is {1}.", dblSquare, dblSquare * dblSquare);