計算公式
周長=(長+寬)*2
面積=長*寬
import javax.swing.JOptionPane; public class MyTest2 { /*輸入矩形的長和寬,計算矩形的周長和面積*/ public static void main(final String[] args) { //定義變量 String s; double length,width,girth,area; //輸入 s=JOptionPane.showInputDialog("請輸入矩形的長:"); length=Double.parseDouble(s); s=JOptionPane.showInputDialog("請輸入矩形的長:"); width=Double.parseDouble(s); //計算 girth=(length+width)*2; area=length*width; //輸出 JOptionPane.showMessageDialog(null,"周長為:\n"+girth+"\n"+"面積為:\n"+area); } }