當前位置:遊戲中心平台 - 遊戲盒子 - 用java編寫壹個猜數遊戲

用java編寫壹個猜數遊戲

import java.util.Random;

import javax.swing.*;

public class Game {

private static Random r = new Random();

private static int difficulty = 3;

private static int times = 8;

private static int digit = 0;

private static int number = 0;

private static boolean good = false;

private static int m = r.nextInt(100);

public static void main(String[] args) {

inPut();

}

private static void inPut() {

String inPut = JOptionPane.showInputDialog("請輸入數字:1:開始猜數 2:遊戲參數設置 9:退出遊戲");

int a = Integer.parseInt(inPut);

if(a!=1 && a!=2 && a!=9) {

JOptionPane.showMessageDialog(null, "輸入有誤,請重新輸入!");

inPut();

}

switch(a) {

case 1:

start();

break;

case 2:

modify();

break;

case 9:

break;

}

}

private static void start() {

String inPut = JOptionPane.showInputDialog("遊戲開始 請輸入所猜數字1-100");

digit = Integer.parseInt(inPut);

if(digit>100 || digit<1) {

JOptionPane.showMessageDialog(null, "輸入有誤,請重新輸入!");

start();

}

number = 1;

while(compare()) {

if(number>=times) {

JOptionPane.showMessageDialog(null, "超過次數,尚需努力");

break;

}

number++;

}

if(good) {

double x = (1 - (double)number/8.00) * 100;

JOptionPane.showMessageDialog(null, "恭喜妳猜對了,妳的戰鬥力是"+x+"%");

}

}

private static boolean compare() {

if(digit>m) {

JOptionPane.showMessageDialog(null, "大了");

if(number<times) {

String inPut = JOptionPane.showInputDialog("重新輸入所猜數字1-100");

digit = Integer.parseInt(inPut);

}

return true;

}

else if(digit<m) {

JOptionPane.showMessageDialog(null, "小了");

if(number<times) {

String inPut = JOptionPane.showInputDialog("重新輸入所猜數字1-100");

digit = Integer.parseInt(inPut);

}

return true;

}

else {

good = true;

return false;

}

}

private static void modify() {

String inPut = JOptionPane.showInputDialog("1:困難 2:壹般 3:簡單");

difficulty = Integer.parseInt(inPut);

switch(difficulty) {

case 1:

times = 4;

start();

break;

case 2:

times = 6;

start();

break;

case 3:

times = 8;

start();

break;

}

}

}

  • 上一篇:推薦壹些好看的小說,比如城市,異世界,網遊。
  • 下一篇:火影忍者練手遊之路312過關
  • copyright 2024遊戲中心平台