2014-09-19 221 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?