Java 在整个printf方法中调用方法后,如何获取要显示的字符串?
我希望最后显示字符串“(磅)”,但我的scanner类一直忽略它们。告诉我为什么会这样Java 在整个printf方法中调用方法后,如何获取要显示的字符串?,java,Java,我希望最后显示字符串“(磅)”,但我的scanner类一直忽略它们。告诉我为什么会这样 System.out.printf("Weight: %.1f \n", person.getWeight(), " (pounds)"); System.out.printf("Height: %.1f \n", person.getHeight(), " (inches)"); 您需要以您的格式显示包含单位的字符串%s的位置,如 System.out.printf("Weight: %.1f
System.out.printf("Weight: %.1f \n", person.getWeight(), " (pounds)");
System.out.printf("Height: %.1f \n", person.getHeight(), " (inches)");
您需要以您的格式显示包含单位的字符串
%s
的位置,如
System.out.printf("Weight: %.1f %s\n", person.getWeight(), "(pounds)");
System.out.printf("Height: %.1f %s\n", person.getHeight(), "(inches)");
也可以手动将单位放入格式中
System.out.printf("Weight: %.1f (pounds)\n", 1.1);
System.out.printf("Height: %.1f (inches)\n", .2);
顺便说一句,您可以使用
%n
而不是\n
为Windows生成一组特定于操作系统的分隔符,如\r\n
。它将产生与System.lineSeparator()相同的结果为什么不这样做呢
System.out.printf("Weight: %.1f (pounds)\n", person.getWeight());
System.out.printf("Height: %.1f (inches)\n", person.getHeight());
这应该可以吗
System.out.printf("Weight: %.1f (pounds)\n", person.getWeight(), );
或者你的意思不同吗?试试这个
System.out.printf("Weight: %.1f (pounds) \n", person.getWeight());