其實好多程式會成日重複用,加上為了方便人地好多程已經寫好了..只要CALL它們出來就可以了...
例一.
import javax.swing.*;
public class SquareRoot {
public static void main( String args[] ) {
String numString;
double number;
double answer;
numString = JOptionPane.showInputDialog("Enter a double number" );
number = Double.parseDouble( numString );
answer = Math.sqrt( number );
JOptionPane.showMessageDialog( null,"The square root of " + number + " is " + answer);
System.exit( 0 );}}
執行結果:
Enter a double number
20.5
The square root of 20.5 is4.52769256
等本人試下解釋以上程式.首先輸入一個double number,而個答案一早放左係Math.sqrt( )到..call 番佢出黎就可以...
例二.
import javax.swing.JOptionPane;
public class Hello {
public static void main(String args[]) {
int value;
String output = "";
for (int counter = 1; counter <= 20; counter++) {
value = 1 + (int) (Math.random() * 6);
output += value + " ";
if (counter % 5 == 0)
output += "\n";}
JOptionPane.showMessageDialog(null, output,"20 Random Numbers from 1 to 6",JOptionPane.INFORMATION_MESSAGE);
System.exit(0); }}
例三.
public class CalSquare {
public static void main(String args[]) {
int result;
System.out.println("Value\tResult");
for(int i=1; i<=10; i++)
{
result = square(i);
System.out.println(i+"\t"+result);}
}
public static int square(int value){
int retvalue;
retvalue = value * value;
return retvalue;}}
執行結果:
value result
1 1
2 4
3 9
4 16
5 25
6 36
7 4 9
8 64
9 81
10 100
沒有留言:
張貼留言