1. 程式人生 > >請設計輸出實數的格式,包括:⑴一行輸出一個實數;⑵一行內輸出兩個實數;⑶一行內輸出三個實數。實數用"6.2f"格式輸出。

請設計輸出實數的格式,包括:⑴一行輸出一個實數;⑵一行內輸出兩個實數;⑶一行內輸出三個實數。實數用"6.2f"格式輸出。

import java.util.Scanner;
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
float x=scanner.nextFloat();
for(int i=0;i<3;i++){
    for(int j=0;j<=i;j++){
        java.text.DecimalFormat bg=new DecimalFormat("0.00");
        String x1=bg.format(x);
        if(j<i){
        System.out.print(x1+" ");
        }
        else 
            System.out.println(x1);    
    }
}
}
}