計算公式
周長=2*PI*R
面積=PI*R*R
控制台
import java.io.*; public class MyTest { /*輸入圓的半徑,求圓的周長和面積*/ public static void main(final String[] args) throws IOException { //定義變量PI final double PI=3.14; //定義變量 byte buf[]=new byte[50]; double r,girth,area; //輸入圓的半徑 System.out.println("請輸入圓的半徑:"); System.in.read(buf); String str=new String(buf); r=Double.parseDouble(str.trim()); //計算周長和面積 girth=2*PI*r; area=PI*r*r; //輸出結果 System.out.println("圓的周長為:"+girth); System.out.println("圓的面積為:"+area); } }
GUI
import javax.swing.JOptionPane; public class MyTest2 { /*輸入圓的半徑,求圓的周長和面積*/ public static void main(final String[] args) { //定義變量PI final double PI=3.14; double r,girth,area; //圖形化輸入方案 final String s1=JOptionPane.showInputDialog("請輸入圓的半徑:"); r=Double.parseDouble(s1); //計算周長和面積 girth=2*PI*r; area=PI*r*r; //輸出結果 JOptionPane.showMessageDialog(null,"圓的周長為\n"+girth+"\n"+"圓的面積為\n"+area); } }