狠狠干影院/欧美午夜电影在线观看/高黄文/国产精品一区二区在线观看完整版

java小游戲源代碼

| 瀏覽次數:

 Java 小游戲 第一個Java 文件:

 import java.util.Scanner;

 public class GameA_B {

 public static void main(String[] args) {

 Scanner reader=new Scanner(System.in);

  int area;

  System.out.println("Game Start…………Please enter the area:(1-9)" +

 "\n"+"1,2,3 means easy"+"\n"+"4,5,6 means middle"+"\n"+

 "7,8,9 means hard"+"\n"+"Please choose:");

 area=reader.nextInt();

 switch((area-1)/3)

 {

 case 0:System.out.println("You choose easy! ");break;

 case 1:System.out.println("You choose middle! ");break;

 case 2:System.out.println("You choose hard! ");break;

 }

 System.out.println("Good Luck!");

 GameProcess game1=new GameProcess(area);

  game1.process();

 }

 }

  第二個 Java 文件:

 import java.util.Random; import java.util.Scanner;

 public class GameProcess {

 int area,i,arrcount,right,midright,t;

 int base[]=new int[arrcount],userNum[]=new int[area],sysNum[]=new int[area];

 Random random=new Random();

  Scanner reader=new Scanner(System.in);

 GameProcess(int a)

 {

  area=a;

  arrcount=10;

  right=0;

 midright=0;

  t=0;

  base=new int[arrcount];

  userNum=new int[area];

  sysNum=new int[area];

  for(int i=0;i<arrcount;i++)

 {

 base[i]=i;

 //System.out.println(base[i]);

 }

 }

 void process()

  {

 rand();

 while(right!=area)

 {

 scanf();

 compare();

 print();

 check();

 }

  }

  void rand()

  {

 for(i=0;i<area;i++)

 {

  t=random.nextInt(arrcount);

  //System.out.println(t);

  sysNum[i]=base[t];

  System.out.println(base[t]);

  delarr(t);

 }

  }

  void delarr(int t)

  {

 for(int j=t;j<arrcount-1;j++)

  base[j]=base[j+1];

 arrcount--;

  }

  void scanf()

  {

 System.out.println("The system number has created!"+"\n"+"Please enter "+area+" Numbers");

  for(int i=0;i<area;i++)

 {

 userNum[i]=reader.nextShort();

  }

  }

  void check()

  {

  if(right==area)

 System.out.println("You win…………!");

  }

  boolean check(int i)

  {

 return true;

  }

  void compare()

  {

 int i=0,j=0;

 right=midright=0;

 for(i=0;i<area;i++)

 {

  for(j=0;j<area;j++)

  {

  if(userNum[i]==sysNum[j])

  {

 if(i==j)

 right++;

 else

 midright++;

  }

  }

 }

  }

  void print()

  {

 System.out.println(" A "+right+" B "+midright);

  } }

 import java.awt.*; import java.awt.event.*; import javax.swing.*;

 class TestGame {

 public static void main(String[] args) {

  App ap = new App(); //調用 App()開始運行程序

  ap.show();

 } }

 class App extends JFrame {

 MainPanel mp;

  public App() {

  mp = new MainPanel();

  this.getContentPane().add(mp);

  this.setSize(400, 450);

  this.setTitle("小游戲");

 } }

 /**

 * 主面板

 * 顯示格子

 * @author Administrator

 *

 */ class MainPanel extends JPanel {

 ButtonPanel bp = new ButtonPanel();

 CtrlPanel rp = new CtrlPanel();

  public MainPanel() {

  this.setLayout(new BorderLayout());

  rp.btnstart.addActionListener(new StartListener());

  this.add(bp, "Center");

  this.add(rp, "South");

  }

 class StartListener implements ActionListener {

  /**

 * 重新開始按鈕的事件

 * 調用按鈕面板里面的顏色初始化方法

 */

  public void actionPerformed(ActionEvent e) {

 if (e.getActionCommand() == "重新開始") {

  bp.ColorInit();

 }

  }

 } }

 class ButtonPanel extends JPanel {

 JButton[][] b = new JButton[5][5];

  /**

  * 按鈕界面的構造器

  * 設置布局方式為 Grid 布局,并生成5*5的格子,

  * 在每個格子生成一個按鈕,

  * 為每個按鈕添加一個監聽事件

  */

 public ButtonPanel() {

  this.setLayout(new GridLayout(5, 5));

 for (int i = 0; i < 5; i++) {

 for (int j = 0; j < 5; j++) {

 b[i][j] = new JButton();

  b[i][j].setActionCommand("" + (i + 1) + (j + 1));

  b[i][j].addActionListener(new MyButtonListener());

  this.add(b[i][j]);

  }

  }

  this.ColorInit();

 }

 /**

 * 面板初始化時候給所有的格子都繪上深灰色

  * i.j 分別是行和列

  */

 public void ColorInit() {

  for (int i = 0; i < 5; i++) {

 for (int j = 0; j < 5; j++) {

  b[i][j].setBackground(Color.DARK_GRAY);

 }

  }

 }

  /**

  * 按鈕上監聽的時事件,監聽點擊

  * @author Administrator

  *

  */

 class MyButtonListener implements ActionListener {

  int r, c;

  /**

 * 需要改變顏色的行和列

 * r

  row

 * c

 colunm

 * 調用 change()來改變顏色

 */

  public void actionPerformed(ActionEvent e) {

 int i = Integer.parseInt(e.getActionCommand());

 r = i / 10 - 1;

 c = i % 10 - 1;

 this.changer();

 }

 /**

 * 傳一個按鈕控件進去

 * 判斷顏色,如果是深灰則變為粉紅

 * 否則義相反

 * @param b

 */

  public void btnChange(JButton b) {

  if (b.getBackground() == Color.DARK_GRAY) {

  b.setBackground(Color.pink);

 } else {

  b.setBackground(Color.DARK_GRAY);

 }

  }

 /**

 * 這個方法是根據點擊的按鈕判斷周圍需要

 * 不能超越數組的下標

 */

  public void changer() {

 this.btnChange(b[r][c]);

 if (r > 0) //行號大于0

  this.btnChange(b[r - 1][c]);

 if (r < 4)

  this.btnChange(b[r + 1][c]);

 if (c > 0)//列號大于0

  this.btnChange(b[r][c - 1]);

 if (c < 4)//列好小余0

  this.btnChange(b[r][c + 1]);

 }

 } }

 /**

 * 控制面板

 * @author Administrator

 *下面的開始按鈕

 */ class CtrlPanel extends JPanel {

 JButton btnstart;

  public CtrlPanel() {

  btnstart = new JButton("重新開始");

  this.add(btnstart);

 } }

 import java.util.*;

 public class Cai {

  enum Res{SHITOU, JIANZI, BU};

  Res res;

  public static void main(String[] args) throws Exception {

  // TODO Auto-generated method stub

  Cai cai = new Cai();

  System.out.println("請輸入你的選擇:");

  System.out.println("0表示石頭,1表示剪子,2表示布");

  char yourResultOfChar =(char) System.in.read();

 int yourResultOfInt = yourResultOfChar - "0";

  int computerResult = pb();

 cai.getYourResult(yourResultOfInt);

  switch (computerResult)

  {

  case 0:

 System.out.println("電腦選擇石頭");

 break;

  case 1:

 System.out.println("電腦選擇剪子");

 break;

  case 2:

 System.out.println("電腦選擇布");

 break;

 }

  cai.pa(computerResult);

 }

 public void getYourResult(int count)

 {

  Res[] result = Res.values();

 res = result[count];

 }

  void pa(int computer)

 {

  Res[] result = Res.values();

  if(this.res == Res.SHITOU)

  {

 System.out.println("我選擇石頭");

 switch(result[computer])

 {

 case SHITOU:

  System.out.println("平局,再來!");

  break;

 case JIANZI:

  System.out.println("我贏了!");

  break;

 case BU:

  System.out.println("我輸了!");

  break;

 }

  } else if(this.res == Res.JIANZI)

  {

 System.out.println("我選擇剪子");

 switch(result[computer])

 {

 case JIANZI:

  System.out.println("平局,再來!");

  break;

 case BU:

  System.out.println("我贏了!");

  break;

 case SHITOU:

  System.out.println("我輸了!");

  break;

 }

  } else if(this.res == Res.BU)

  {

 System.out.println("我選擇布");

  switch(result[computer])

 {

 case BU:

  System.out.println("平局,再來!");

  break;

 case SHITOU:

  System.out.println("我贏了!");

  break;

 case JIANZI:

  System.out.println("我輸了!");

  break;

 }

  }

 }

  static int pb()

 {

  Random ran = new Random();

  int res = ran.nextInt(3);

  return res;

 //輸出0-2的整數,0表示石頭,1表示剪子,2表示布,和 enum Res 中的順序相對應

 } }

 import java.util.*; //導入實用包 util 下所有的類 import javax.swing.*; import java.awt.*; import java.awt.event.*;

 public class CaiShu {

 public static void main(String[] args) {

  Win f = new Win();

  f.setVisible(true);

 } }

 class Win extends JFrame implements ActionListener {

 JLabel labe;

 JButton butt;

 JButton button;

 Random a = new Random();

 private int i = 0;

 private int num;

  JTextField text1, text2;

  JPanel p;

  public Win() {

  super("猜數游戲");

  labe = new JLabel("我心里有個數,它是1---100之間的,你能猜出來嗎?");

  butt = new JButton("確認");

  button = new JButton("重開");

  text1 = new JTextField(5);

  text2 = new JTextField(20);

 p = new JPanel();

  Container con = getContentPane();

  // 調用 JFrame 的 getContentPane 得到容器

  text2.setEditable(false);

  // 使輸出結果文本域不可編輯

  butt.addActionListener(this);

  // 執行結果動作

  con.setLayout(new GridLayout(4, 1));

 // 設置整個界面的長寬比

  p.add(text1);

  // 添加輸入數字文本域

  p.add(butt);

  p.add(button);

  button.addActionListener(new ActionListener(){

 public void actionPerformed(ActionEvent e) {

  text1.setText("");

  text2.setText("");

  i=0;

 }

  });

  con.add(labe);

  // 添加游戲標簽

  con.add(p);

  con.add(text2);

  // 添加輸出結果信息文本域

 setSize(300, 300);

  // 設置窗口尺寸

  setVisible(true);

  // 設置窗口可視

  pack();

  addWindowListener(new WindowAdapter() {

 public void windowClosing(WindowEvent e) {

  setVisible(false);

  System.exit(0);

 }

  });

  }

  public void actionPerformed(ActionEvent e) {

  int shu;

  while (true) {

 shu = Integer.parseInt(text1.getText());

  if (i == 0) {

  num = a.nextInt(100);

 }

  i++;

 if (i == 10) {

  text2.setText("結束吧,你沒有希望了!!");

  i = 0;

  break;

 }

  if (e.getSource() == butt) {

  if (shu > num) {

 text2.setText("輸入的數大了,輸小點的!");

  } else if (shu < num) {

 text2.setText("輸入的數小了,輸大點的!");

  } else if (shu == num) {

 text2.setText("恭喜你,猜對了!");

 if (i <= 2)

  text2.setText("你真是個天才!");

 else if (i <= 6)

  text2.setText("還將就,你過關了!");

 else if (i <= 8)

  text2.setText("但是你還……真笨!");

 else

  text2.setText("你實在是太笨了!");

  }

  break;

 }

  }

 } }

推薦訪問: 小游戲 源代碼 java

【java小游戲源代碼】相關推薦

工作總結最新推薦

NEW