简体   繁体   中英

Adding New Line in Java String

I want my output to be in multiple lines but \\n doesn't seem to be working for me. Am I doing something wrong? Thanks

DESIRED OUTPUT

Hello: name

Weight in Kilograms: XX

Height in meters: XX

BMI: XX

CODE

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

You could use String.format

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

This will also give you a platform independent line separator (as opposed to "\\n"), see How do I get a platform-dependent new line character?

\\ n是字符串的一部分,因此将其包含在字符串“ XX \\ n”中,还应添加字符串连接正确性,例如:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM