我希望显示最后的字符串“(磅)”,但是我的扫描仪类会一直将它们排除在外。告诉我为什么会这样?
System.out.printf("Weight: %.1f \n", person.getWeight(), " (pounds)");
System.out.printf("Height: %.1f \n", person.getHeight(), " (inches)");
答案 0 :(得分:2)
您需要以格式显示包含单位的字符串%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);
BTW而不是\n
您可以使用%n
为Windows生成特定于操作系统的分隔符集,例如\r\n
。它将产生与System.lineSeparator()
相同的结果。
答案 1 :(得分:0)
为什么不做这样的事情?
System.out.printf("Weight: %.1f (pounds)\n", person.getWeight());
System.out.printf("Height: %.1f (inches)\n", person.getHeight());
答案 2 :(得分:0)
应该这样做吗?
System.out.printf("Weight: %.1f (pounds)\n", person.getWeight(), );
或者你的意思是什么不同?
答案 3 :(得分:0)
试试这个
System.out.printf("Weight: %.1f (pounds) \n", person.getWeight());