java實現(xiàn)的簡單猜數(shù)字游戲代碼,通過隨機數(shù)與邏輯判斷來實現(xiàn)游戲功能 代碼如下: import java.util.InputMismatchException; import java.util.Scanner; public class Main { public static void main(String[] args) { // 產(chǎn)生一個隨機數(shù) int n
成都創(chuàng)新互聯(lián)自2013年起,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元宿豫做網(wǎng)站,已為上家服務(wù),為宿豫各地企業(yè)和個人服務(wù),聯(lián)系電話:028-86922220
主要通過隨機數(shù)與邏輯判斷來實現(xiàn)游戲功能
實現(xiàn)代碼如下:
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 產(chǎn)生一個隨機數(shù)
int number = (int) (Math.random() * 100) + 1;
// 加入count
int count = 0;
// 在這里加入最大值,和最小值
int max = 100;
int min = 1;
while (true) {
// 鍵盤錄入數(shù)據(jù)
Scanner sc = new Scanner(System.in);
System.out.println("請輸入你要猜的數(shù)據(jù):(" + min + "~" + max + ")");
try {
count++;
int guessNumber = sc.nextInt();
// 判斷
if (guessNumber number) {
max = guessNumber;
System.out.println("你猜大了");
} else if (guessNumber number) {
min = guessNumber;
System.out.println("你猜小了");
} else {
System.out.println("恭喜你,花了" + count + "次就猜中了");
// 問是否繼續(xù)
System.out.println("請問還要繼續(xù)嗎?(yes)");
sc = new Scanner(System.in);
String str = sc.nextLine();
if ("yes".equals(str)) {
// 重寫賦值隨機數(shù)
number = (int) (Math.random() * 100) + 1;
count = 0;
max = 100;
min = 1;
} else {
break;
}
}
} catch (InputMismatchException e) {
System.out.println("你輸入的數(shù)據(jù)有誤");
}
}
}
public static void main(String [] args){
int[] num_list={1,3,5,7,9};
java.util.Scanner str=new java.util.Scanner(System.in);
int num = str.nextInt();
for(int i=0;inum_list.length;i++){
if(num!=nullnum.equals("")){
if(num_list[i]==num){
System.out.println("恭喜你,猜對了!");
}else{
System.out.println("抱歉,你猜錯了!");
}
}
}
}
package?day06;
import?java.util.Scanner;
//猜字符游戲
public?class?GuessingGame?{
//主方法
public?static?void?main(String[]?args)?{
Scanner?scan?=?new?Scanner(System.in);
int?count?=?0;?//猜錯的次數(shù)
char[]?chs?=?generate();?//隨機生成的字符數(shù)組
System.out.println(chs);?//作弊
while(true){?//自造死循環(huán)
System.out.println("猜吧!");
String?str?=?scan.next().toUpperCase();?//獲取用戶輸入的字符串
if(str.equals("EXIT")){?//判斷str是否是EXIT
System.out.println("下次再來吧!");
break;
}
char[]?input?=?str.toCharArray();?//將字符串轉(zhuǎn)換為字符數(shù)組
int[]?result?=?check(chs,input);??//對比
if(result[0]==chs.length){?//位置對為5
int?score?=?chs.length*100?-?count*10;?//一個字符100分,錯一次減10分
System.out.println("恭喜你猜對了,得分:"?+?score);
break;?//猜對時跳出循環(huán)
}else{?//沒猜對
count++;?//猜錯次數(shù)增1
System.out.println("字符對:"+result[1]+"個,位置對:"+result[0]+"個");
}
}
}
//隨機生成5個字符數(shù)組
public?static?char[]?generate(){
char[]?chs?=?new?char[5];
char[]?letters?=?{?'A',?'B',?'C',?'D',?'E',?'F',?'G',?'H',?'I',?'J',
'K',?'L',?'M',?'N',?'O',?'P',?'Q',?'R',?'S',?'T',?'U',?'V',
'W',?'X',?'Y',?'Z'};
boolean[]?flags?=?new?boolean[letters.length];?//1.
for(int?i=0;ichs.length;i++){
int?index;
do{
index?=?(int)(Math.random()*letters.length);?//0到25
}while(flags[index]==true);?//2.
chs[i]?=?letters[index];
flags[index]?=?true;?//3.
}
return?chs;
}
//對比隨機數(shù)組與用戶輸入的數(shù)組
public?static?int[]?check(char[]?chs,char[]?input){
int[]?result?=?new?int[2];
for(int?i=0;ichs.length;i++){
for(int?j=0;jinput.length;j++){
if(chs[i]==input[j]){?//字符對
result[1]++;?//字符對個數(shù)增1
if(i==j){?//位置對
result[0]++;?//位置對個數(shù)增1
}
break;
}
}
}
return?result;
}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class caishuzi extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
private JPanel textpanel, buttonpanel, fopanel, downpanel;
private JButton btn;
private JTextField jtinput, jttishi;
private JLabel lbnum0, lbnum, lbtishi, lbinput;
private JTextArea tafo;
private Boolean start = false, jisuan = false, jinru = false;
MenuBar mb = new MenuBar();
Menu filemenu = new Menu("文件"), systemmenu = new Menu("系統(tǒng)"),
helpmenu = new Menu("幫助");
MenuItem newfile = new MenuItem("文件"), openfile = new MenuItem("打開"),
closefile = new MenuItem("關(guān)閉"), quit = new MenuItem("退出"),
inputsecret = new MenuItem("輸入密碼"), help = new MenuItem("幫助"),
inputcishu = new MenuItem("輸入限制次數(shù)");
char[] c = new char[4];
String strsecret = "123";
int xianzhi=8;
caishuzi()
{
super("猜數(shù)字");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this.setLocationRelativeTo(null);
// 設(shè)置菜單
filemenu.add(newfile);
filemenu.add(openfile);
filemenu.addSeparator();
filemenu.add(closefile);
newfile.setEnabled(false);
openfile.setEnabled(false);
closefile.setEnabled(false);
inputsecret.addActionListener(this);
inputcishu.addActionListener(this);
inputcishu.setEnabled(false);
systemmenu.add(inputsecret);
systemmenu.add(inputcishu);
systemmenu.add(quit);
quit.addActionListener(this);
help.addActionListener(this);
helpmenu.add(help);
mb.add(filemenu);
mb.add(systemmenu);
mb.add(helpmenu);
this.setMenuBar(mb);
Container c = this.getContentPane();
BorderLayout b = new BorderLayout();
b.setVgap(10);
c.setLayout(b);
settextpanel();
c.add(textpanel, BorderLayout.NORTH);
setbuttonpanel();
setfopanel();
setdownpanel();
c.add(downpanel, BorderLayout.CENTER);
this.setSize(680,350);
//this.setResizable(false);
//this.pack();
this.setVisible(true);
}
void settextpanel()
{
textpanel = new JPanel();
textpanel.setLayout(new FlowLayout());
lbinput = new JLabel("輸入:");
jtinput = new JTextField("", 10);
jtinput.setEditable(false);
jttishi = new JTextField(30);
lbnum = new JLabel("8");
lbnum0 = new JLabel("剩余次數(shù):");
lbtishi = new JLabel("提示:");
jttishi.setEditable(false);
textpanel.add(lbinput);
textpanel.add(jtinput);
textpanel.add(lbtishi);
textpanel.add(jttishi);
textpanel.add(lbnum0);
textpanel.add(lbnum);
}
void setbuttonpanel()
{
String[] str =
{ "7", "8", "9", "4", "5", "6", "1", "2", "3", "確定", "0", "退格" };
buttonpanel = new JPanel();
buttonpanel.setLayout(new GridLayout(4, 3, 4, 8));
for (int i = 0; i str.length; i++)
{
btn = new JButton(str[i]);
btn.addActionListener(this);
buttonpanel.add(btn);
}
}
void setfopanel()
{
JButton btnright = new JButton("正確答案");
JPanel p = new JPanel();
btnright.addActionListener(this);
fopanel = new JPanel();
fopanel.setLayout(new BorderLayout());
btn = new JButton("開始");
btn.addActionListener(this);
tafo = new JTextArea(15,12);
p.setLayout(new GridLayout(1, 2));
p.add(btn);
p.add(btnright);
fopanel.add(p, BorderLayout.NORTH);
tafo.setEditable(false);
fopanel.add(tafo, BorderLayout.CENTER);
}
void setdownpanel()
{
downpanel = new JPanel();
downpanel.setLayout(new GridLayout(1, 2, 30, 0));
downpanel.add(buttonpanel);
downpanel.add(fopanel);
}
public static void main(String[] args)
{
new caishuzi();
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("退出"))
{
int i = JOptionPane.showConfirmDialog(this, "確認(rèn)退出?", "",
JOptionPane.YES_NO_OPTION);
//System.out.println(i);
if (i == 0)
{
System.exit(0);
}
}
if (s.equals("幫助"))
{
JOptionPane.showMessageDialog(this,
"猜數(shù)字游戲,可以輸入限制次數(shù),密碼為123,\n在菜單:系統(tǒng)——輸入密碼 中輸入!");
}
if (s.equals("輸入密碼"))
{
while (true)
{
String inputValue = "";
inputValue = JOptionPane.showInputDialog(this, "輸入密碼");
// System.out.println(inputValue);
if (inputValue == null)
return;
if (inputValue.equals(strsecret))
{
jinru = true;
jttishi.setText("密碼正確,點擊開始");
inputsecret.setEnabled(false);
break;
} else
{
int i = JOptionPane.showConfirmDialog(this, "密碼錯誤,重新輸入?",
"", JOptionPane.YES_NO_OPTION);
if (i == 1)
break;
}
}
}
if (!jinru)
{
jttishi.setText("請輸入密碼:");
return;
}
if (s.equals("開始") || s.equals("重玩"))
{
StringBuffer sb = new StringBuffer("0123456789");
for (int i = 0; i c.length; i++)
{
int n = (int) (Math.random() * sb.length());
c[i] = sb.charAt(n);
sb.deleteCharAt(n);
}
btn.setText("重玩");
start = true;
lbnum.setText(String.valueOf(xianzhi));
jisuan = false;
//System.out.println(c);
jttishi.setText("請輸入四不重復(fù)的數(shù):");
jtinput.setText("");
tafo.setText("");
}
if (s.compareTo("0") = 0 s.compareTo("9") = 0)
{
jisuan = false;
if (!start)
jttishi.setText("點擊開始!");
else
{
if (jtinput.getText().length() 4)
{
if (jtinput.getText().indexOf(s) 0)
{
jtinput.setText(jtinput.getText() + s);
if (jtinput.getText().length() 4)
jttishi.setText("還差"
+ (4 - jtinput.getText().length()) + "個數(shù)");
else
{
jttishi.setText("輸入正確,點擊確定");
jisuan = true;
}
} else
jttishi.setText("不能輸入重復(fù)的數(shù)");
} else
{
jttishi.setText("不能超過四個數(shù),點擊確定");
jisuan = true;
}
}
}
if (s.equals("退格") start)
{
if (jtinput.getText().length() 0)
{
jtinput.setText(jtinput.getText().substring(0,
jtinput.getText().length() - 1));
jttishi.setText("還差" + (4 - jtinput.getText().length()) + "個數(shù)");
}
jisuan = false;
}
if (s.equals("確定"))
{
if (!start)
{
jttishi.setText("還沒開始呢!");
return;
}
if (!jisuan)
{
jttishi.setText("輸入不正確!");
return;
}
int numa = 0, numb = 0;
char[] c1 = jtinput.getText().toCharArray();
// System.out.println(c1);
for (int i = 0; i c1.length; i++)
{
for (int j = 0; j c.length; j++)
{
if (c1[i] == c[j] i != j)
{
numb++;
}
if (c1[i] == c[j] i == j)
{
numa++;
}
}
}
tafo.append(jtinput.getText() + "\t" + numa + "A" + numb + "B"
+ "\n");
jtinput.setText("");
jisuan = false;
if (numa == 4)
{
jttishi.setText("答對了,再來一局吧!");
start = false;
btn.setText("開始");
tafo.append("恭喜,答對了! ");
} else
{
lbnum.setText(String
.valueOf(Integer.valueOf(lbnum.getText()) - 1));
if (Integer.valueOf(lbnum.getText()) == 0)
{
String str = "";
jttishi.setText("機會沒有了!很遺憾");
for (int i = 0; i c.length; i++)
str += c[i];
tafo.append("超過限制次數(shù),失敗!" + "正確答案是:" + str);
start = false;
btn.setText("開始");
} else
jttishi.setText("輸入四個數(shù):");
}
}
if (s.equals("正確答案"))
{
if (!start)
{
jttishi.setText("還沒開始呢!");
return;
}
String str = "";
for (int i = 0; i c.length; i++)
str += c[i];
tafo.append("正確答案是:" + str);
jttishi.setText("重新開始吧!");
start = false;
jisuan = false;
btn.setText("開始");
}
if (!start)
{
inputcishu.setEnabled(true);
} else
inputcishu.setEnabled(false);
if (s.equals("輸入限制次數(shù)"))
{
while (true)
{
try
{
String str = JOptionPane.showInputDialog(this,
"輸入小于15的整數(shù)數(shù)字:");
if (str == null)
return;
if (Integer.valueOf(str) = 15 Integer.valueOf(str) 0)
{
xianzhi=Integer.valueOf(str);
lbnum.setText(String.valueOf(xianzhi));
break;
} else
{
int i = JOptionPane.showConfirmDialog(this, "輸入錯誤,重試?",
"", JOptionPane.YES_NO_OPTION);
if (i == 1)
break;
}
} catch (Exception ex)
{
int i = JOptionPane.showConfirmDialog(this, "輸入錯誤,重試?", "",
JOptionPane.YES_NO_OPTION);
if (i == 1)
break;
}
}
}
}
}
我做課程設(shè)計的最初代碼 參考下吧 運行時先看幫助~