//「コアJava2」P128をApplet版に書き換えたもの //cd c:\JavaTry2005\Pascal //javac Pascal2.java -Xlint:unchecked //appletviewer Pascal2.java import java.awt.*; import java.util.*; import java.applet.Applet; /* */ public class Pascal2 extends Applet { public void paint(Graphics g) { final int NMAX = 15; int[][] odds = new int[NMAX + 1][]; for (int n = 0; n <= NMAX; n++) odds[n] = new int[n + 1]; // fill triangular array for (int n = 0; n < odds.length; n++) for (int k = 0; k < odds[n].length; k++) { /* compute binomial coefficient n * (n - 1) * (n - 2) * . . . * (n - k + 1) ------------------------------------------- 1 * 2 * 3 * . . . * k */ int lotteryOdds = 1; for (int i = 1; i <= k; i++) lotteryOdds = lotteryOdds * (n - i + 1) / i; odds[n][k] = lotteryOdds; } setBackground(Color.yellow); g.setFont(new Font("Serif", Font.BOLD, 20)); g.setColor(Color.blue); // print triangular array for (int n = 0; n < odds.length; n++) { for (int k = 0; k < odds[n].length; k++) { String output =(new Integer(odds[n][k])).toString(); g.drawString(output,400 - 25 * n + 45 * k, 50 + 30 * n); /* String output = " " + odds[n][k]; // make output field 4 characters wide output = output.substring(output.length() - 4); System.out.print(output); */ } } } }