本文介绍了在 Java 字符串中添加新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

期望的输出

你好:名字

体重(公斤):XX

身高(米):XX

体重指数:XX

代码

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

推荐答案

你可以使用 String.format

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)

这也会给你一个独立于平台的行分隔符(而不是\n"),见如何获得依赖于平台的换行符字符?

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

这篇关于在 Java 字符串中添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 05:45