读心术


import java.awt.Graphics;
import java.util.Random;
import javax.swing.*;

public class ReadHeart extends JFrame{
String a1="κ",a2="χ",a3="ε",a4="η",a5="ζ",a6="ψ",a7="ι",a8="λ",a9="γ",a10="θ" ;
Random r = new Random();
String a,b,d = "",str;
MyCanvas mc;
public class MyCanvas extends JPanel {
String group[][] = new String[10][10];
public void paint(Graphics g) {
g.drawLine(0, 20, 300,20 );
g.drawLine(20, 0, 20, 300);
for(int i=0; i<=9; i++) {
for(int j=0; j<=9; j++) {
group[i][j] = chooseString(r.nextInt(9));
}
}
group[0][0] = group[0][9] = group[1][8] = group[2][7] = group[3][6] = group[4][5] =
group[5][4] = group[6][3] = group[7][2] = group[8][1] = group[9][0] = group[9][9]
= chooseString(r.nextInt(9));
for(int i=0; i<10; i++) {
for(int j=0; j<10; j++) {
d = d + group[i][j];
g.drawString(d, i*20+30, j*20+40);
d = "";
}
b = String.valueOf(i);
g.drawString(b, i*20+30,15);
g.drawString(b, 5, i*20+20+20);
}
}
}
public void luanchJFrame() {
mc = new MyCanvas();
this.add(mc);
this.setLocation(300, 300);
this.setSize(400, 400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
str = "这是一个读心术程序\n先在你内心默念一个两位以内的数字,\n" +
"然后将你想到的数字减去十位上的数字,\n" +
"再减去个位上的数字,\n" +
"然后在屏幕上找到对应数字(从左到右,从上到下的顺序)上的汉字\n" +
"再点击确定我就能猜到你想的那个数字对应的图案!";
JOptionPane.showMessageDialog(null, str, "读心术", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "你得到的汉字是"+mc.group[8][1]);
}

public static void main(String[] args) {
new ReadHeart().luanchJFrame();

}
String chooseString(int a) {
String b = " ";
switch(a) {
case 0:b = a1;
break;
case 1:b = a2;
break;
case 2:b = a3;
break;
case 3:b = a4;
break;
case 4:b = a5;
break;
case 5:b = a6;
break;
case 6:b = a7;
break;
case 7:b = a8;
break;
case 8:b = a9;
break;
case 9:b = a10;
break;
}
return b;
}
}

你可能感兴趣的:(Java小程序)