本题要求编写程序,顺序读入浮点数1、整数、字符、浮点数2,再按照字符、整数、浮点数1、浮点数2的顺序输出。
输入格式:
输入在一行中顺序给出浮点数1、整数、字符、浮点数2,其间以1个空格分隔。
输出格式:
在一行中按照字符、整数、浮点数1、浮点数2的顺序输出,其中浮点数保留小数点后2位。
输入样例:
2.12 88 c 4.7
输出样例:
c 88 2.12 4.70
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String input = in.nextLine();
if(input.split(" ").length==4)
{
// 输入在一行中顺序给出浮点数1、整数、字符、浮点数2,其间以1个空格分隔。
// 格式化数字
DecimalFormat num = new DecimalFormat("##0.00");
// 分割第一个空格前的字符串,并且转换成浮点类型
float aa = Float.parseFloat(input.split(" ")[0]);
// 按照上面规定的格式,格式化输出aa
String a = num.format(aa);
int b = Integer.parseInt(input.split(" ")[1]);
String c = input.split(" ")[2];
float dd = Float.parseFloat(input.split(" ")[3]);
String d = num.format(dd);
// 在一行中按照字符、整数、浮点数1、浮点数2的顺序输出,其中浮点数保留小数点后2位。
System.out.println(c+" "+b+" "+a+" "+d);
}
}
}