在Java字符串中添加新行

时间:2014-09-19 23:10:39

标签: java newline

我希望我的输出有多行,但\n似乎对我不起作用。难道我做错了什么?感谢

渴望输出

您好:姓名

公斤重量:XX

以米为单位的高度:XX

BMI:XX

CODE

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

3 个答案:

答案 0 :(得分: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?

答案 1 :(得分:2)

\ n是字符串的一部分,因此请将其包含在字符串" XX \ n" ,还添加字符串连接correclty,如:

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

答案 2 :(得分:0)

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