编写Java程序,已知圆球体积为4⼀3π*r3,输入圆球的半径,计算输出圆球的体积

2024-11-27 03:10:22
推荐回答(2个)
回答1:

import java.util.Scanner;

class Test{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
double r = sc.nextDouble();//球的半径r
double v = 4.0/3.0*3.14*r*r*r;//球的体积v
System.out.println(v);//输出体积
}
}

//这个够简单吧?希望采纳哟

回答2:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Ball {
public static void main(String[] args) {

InputStreamReader is = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(is);
try {
System.out.print("请输入圆的半径:");
double n = Double.parseDouble(br.readLine());
System.out.println("圆的体积为:"+(Math.PI*4*Math.pow(n, 3)/3));
} catch (IOException e) {
System.out.println("系统错误1!");
e.printStackTrace();
} catch (Exception e) {
System.out.println("所输入不为数字!");
e.printStackTrace();
} finally {
try {
is.close();
br.close();
} catch (IOException e) {
System.out.println("关闭流发生错误!");
e.printStackTrace();
}
}
}
}