我希望显示末尾的字符串“(磅)”,但我的扫描仪类一直将它们排除在外。告诉我为什么会这样?
System.out.printf("Weight: %.1f \n", person.getWeight(), " (pounds)");
System.out.printf("Height: %.1f \n", person.getHeight(), " (inches)");
我希望显示末尾的字符串“(磅)”,但我的扫描仪类一直将它们排除在外。告诉我为什么会这样?
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
生成操作系统特定的分隔符集,例如\r\n
Windows。它将产生与 相同的结果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());