第一題
import java.util.Scanner;
import java.text.DecimalFormat;
public class test
{
public static void main(String [] args)
{
double r,s,v;
Scanner scan = new Scanner(System.in);
System.out.println("輸入一個球體的半徑:");
r = scan.nextDouble();
DecimalFormat fmt = new DecimalFormat("0.###");
v = 4.0/3*Math.PI*Math.pow(r,3);
s = 4*Math.PI*Math.pow(r,2);
System.out.println("球的體積爲:"+fmt.format(v));
System.out.println("球的表面積爲:"+fmt.format(s));
}
}
第二題
import java.awt.*;
import javax.swing.*;
public class second
{
public static void main(String [] args)
{
JFrame frame = new JFrame("MyName");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel name = new JPanel();
JLabel firstName = new JLabel(" Jayson");
JLabel lastName = new JLabel("Leang ");
name.add(lastName);
name.add(firstName);
frame.getContentPane().add(name);
frame.pack();
frame.setVisible(true);
}
}