难以将输出格式化为3位小数

cal*_*011 0 java

我是Java的新手,我只是想通过一些课堂练习.

我被赋予了从给定半径计算用户周长和圆圈面积的任务.

import java.util.Scanner;

public class Circle {

    public static void main(String[] args) {
    System.out.println("Please enter the radius of your circle below: ");

    Scanner input = new Scanner(System.in);
    float r =input.nextFloat();

    float p = (float) (2 * Math.PI * r);
    float a = (float) ((Math.PI) * Math.pow(r,2));

    System.out.format("Your perimeter is %.3f", p + " and your area is %.3f", a);

    }
}
Run Code Online (Sandbox Code Playgroud)

到目前为止这是我的代码,我很确定这就是我所需要的,但是编译器抛出错误:

'线程中的异常"main"java.util.IllegalFormatConversionException:f!= java.lang.String'

我曾尝试过几次使用System.out系列,但我无法弄清楚我哪里出错了!

有任何想法吗?提前致谢 :)

Rei*_*eus 5

p + " and your area is %.3f"被解释为String由于类型推广.使用

System.out.format("Your perimeter is %.3f and your area is %.3f", p, a);
Run Code Online (Sandbox Code Playgroud)