我编写了一个程序,其他一切似乎都有效,除了我无法弄清楚如何制作右对齐列并在我的代码中只浮动2个小数位。
编码"%。2f"使字符串在其中一列中显示为 - 它替换变量。
ie:System.out.printf("% - 20s%-15s%10s%n","%。2f",num2,square2,sqrt2);
我确实有专栏,但他们没有正当理由。
我的代码:
import java.util.Scanner;
public class SquaresIn {
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println ("Please enter 3 integers, separated by spaces:");
System.out.printf ("%-20s %-15s %10s %n" , heading1, heading2, heading3);
System.out.printf ("%-20s %-15s %10s %n" , num1, square1, sqrt1);
System.out.printf ("%-20s %-15s %10s %n" , num2, square2, sqrt2);
System.out.printf ("%-20s %-15s %10s %n" , num3, square3, sqrt3);
sc.close();
} // main method
}
答案 0 :(得分:0)
我认为这样可以解决问题:
System.out.printf("%20s %15s %15s \n", heading1, heading2, heading3);
System.out.printf("%20s %15d %15.2f \n", num1, square1, sqrt1);
System.out.printf("%20s %15d %15.2f \n", num2, square2, sqrt2);
System.out.printf("%20s %15d %15.2f \n", num3, square3, sqrt3);
产生以下输出:
Number Square Square root
11 121 3,32
17 289 4,12
19 361 4,36
编辑: - 使用%d表示整数值。
答案 1 :(得分:0)
试试这个:
System.out.printf ("%-20s %-15s %10s %n" , heading1, heading2, heading3);
System.out.printf ("%-20s %-15.2f %.2f %n" , num1, square1, sqrt1);
System.out.printf ("%-20s %-15.2f %.2f %n" , num2, square2, sqrt2);
System.out.printf ("%-20s %-15.2f %.2f %n" , num3, square3, sqrt3);