java页面猜数字代码 java 猜数字
如何用Java语言实现猜数字游戏
java实现的简单猜数字游戏代码,通过随机数与逻辑判断来实现游戏功能 代码如下: import java.util.InputMismatchException; import java.util.Scanner; public class Main { public static void main(String[] args) { // 产生一个随机数 int n
成都创新互联自2013年起,是专业互联网技术服务公司,拥有项目网站建设、成都网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元宿豫做网站,已为上家服务,为宿豫各地企业和个人服务,联系电话:028-86922220
java如何实现的简单猜数字游戏代码
主要通过随机数与逻辑判断来实现游戏功能
实现代码如下:
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 产生一个随机数
int number = (int) (Math.random() * 100) + 1;
// 加入count
int count = 0;
// 在这里加入最大值,和最小值
int max = 100;
int min = 1;
while (true) {
// 键盘录入数据
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要猜的数据:(" + 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 + "次就猜中了");
// 问是否继续
System.out.println("请问还要继续吗?(yes)");
sc = new Scanner(System.in);
String str = sc.nextLine();
if ("yes".equals(str)) {
// 重写赋值随机数
number = (int) (Math.random() * 100) + 1;
count = 0;
max = 100;
min = 1;
} else {
break;
}
}
} catch (InputMismatchException e) {
System.out.println("你输入的数据有误");
}
}
}
简单的java猜数字代码
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("抱歉,你猜错了!");
}
}
}
}
用java编写一个猜数字游戏,
package day06;
import java.util.Scanner;
//猜字符游戏
public class GuessingGame {
//主方法
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int count = 0; //猜错的次数
char[] chs = generate(); //随机生成的字符数组
System.out.println(chs); //作弊
while(true){ //自造死循环
System.out.println("猜吧!");
String str = scan.next().toUpperCase(); //获取用户输入的字符串
if(str.equals("EXIT")){ //判断str是否是EXIT
System.out.println("下次再来吧!");
break;
}
char[] input = str.toCharArray(); //将字符串转换为字符数组
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; //猜对时跳出循环
}else{ //没猜对
count++; //猜错次数增1
System.out.println("字符对:"+result[1]+"个,位置对:"+result[0]+"个");
}
}
}
//随机生成5个字符数组
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;
}
//对比随机数组与用户输入的数组
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]++; //字符对个数增1
if(i==j){ //位置对
result[0]++; //位置对个数增1
}
break;
}
}
}
return result;
}
}
java窗体猜数字代码
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("系统"),
helpmenu = new Menu("帮助");
MenuItem newfile = new MenuItem("文件"), openfile = new MenuItem("打开"),
closefile = new MenuItem("关闭"), quit = new MenuItem("退出"),
inputsecret = new MenuItem("输入密码"), help = new MenuItem("帮助"),
inputcishu = new MenuItem("输入限制次数");
char[] c = new char[4];
String strsecret = "123";
int xianzhi=8;
caishuzi()
{
super("猜数字");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this.setLocationRelativeTo(null);
// 设置菜单
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("剩余次数:");
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, "确认退出?", "",
JOptionPane.YES_NO_OPTION);
//System.out.println(i);
if (i == 0)
{
System.exit(0);
}
}
if (s.equals("帮助"))
{
JOptionPane.showMessageDialog(this,
"猜数字游戏,可以输入限制次数,密码为123,\n在菜单:系统——输入密码 中输入!");
}
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("请输入四不重复的数:");
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()) + "个数");
else
{
jttishi.setText("输入正确,点击确定");
jisuan = true;
}
} else
jttishi.setText("不能输入重复的数");
} else
{
jttishi.setText("不能超过四个数,点击确定");
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()) + "个数");
}
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("超过限制次数,失败!" + "正确答案是:" + str);
start = false;
btn.setText("开始");
} else
jttishi.setText("输入四个数:");
}
}
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("输入限制次数"))
{
while (true)
{
try
{
String str = JOptionPane.showInputDialog(this,
"输入小于15的整数数字:");
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;
}
}
}
}
}
我做课程设计的最初代码 参考下吧 运行时先看帮助~
当前名称:java页面猜数字代码 java 猜数字
文章起源:http://ybzwz.com/article/hphdos.html