2014-09-19 222 views
0

我想讓我的輸出成爲多行,但\n似乎不適合我。難道我做錯了什麼?由於在Java字符串中添加新行

所需的輸出

您好:名字公斤

重量:XX

身高以米爲單位:XX

BMI:XX

CODE

SimpleOutput.showInformation("Hello: " + name \n "Weight in kilograms: " + weightKilograms \n "Height in meters: " + heightMeters \n "BMI: " + (int)bmi); 
+2

什麼編程語言翻譯年齡是? – Lucas 2014-09-19 23:13:23

+0

語言是Java – 2014-09-19 23:15:13

+2

\ n是字符串的一部分,所以將它包含在您的字符串「XX \ n」 – 2014-09-19 23:15:18

回答

2

\ n是字符串的一部分,所以它包含在你的字符串 「XX \ n」,還增加了字符串連接correclty,如:

SimpleOutput.showInformation("Hello: " + name + "\nWeight in kilograms: " + weightKilograms + "\nHeight in meters: " + heightMeters + "\nBMI: " + (int)bmi); 
+0

你沒有拼寫「correclty」correclty。 – Air 2014-09-19 23:30:44

0
SimpleOutput.showInformation("Hello: " + name + "\nWeight in kilograms: " + weightKilograms + "\nHeight in meters: " + heightMeters + "\nBMI: " + (int)bmi); 
3

你可以使用的String.format

String.format("Hello: %s%n Weight in kilograms: %d%n Height in meters: %d%n BMI: %d", name, weightKilograms, heightMeters, (int)bmi) 

這也會給你一個平臺獨立的行分隔符(而不是「\ n」),見 How do I get a platform-dependent new line character?