一道入门java题求助

来源:百度知道 编辑:UC知道 时间:2024/06/19 05:54:14
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class program3 {
public static void main(String[] args)throws IOException{
InputStreamReader reader= new InputStreamReader(System.in);
BufferedReader console= new BufferedReader(reader);
System.out.print("Enter the radius of the sphere:");
String input=console.readLine();
double x= Integer.parseInt(input);
double V; double a=3.141593;
V=(4/3)*a*x;
System.out.print("The volume of the sphere:");
System.out.print(V);
System.out.println();

}

}
输入1,2,3,4这种能算V, 但是如果我想输入0.2这样的改怎么改呢??

这样改:

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class Print {
public static void main(String[] args)throws IOException{
InputStreamReader reader= new InputStreamReader(System.in);
BufferedReader console= new BufferedReader(reader);
System.out.print("Enter the radius of the sphere:");
String input=console.readLine();
double x = Double.parseDouble(input);
double V; double a=3.141593;
V=(4/3)*a*x;
System.out.print("The volume of the sphere:");
System.out.print(V);
System.out.println();

}

}

double x= Integer.parseInt(input); 改:Double.parseDouble