「java猜数游戏设计作业」java设计猜数字游戏

博主:adminadmin 2022-12-28 16:33:09 54

今天给各位分享java猜数游戏设计作业的知识,其中也会对java设计猜数字游戏进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

JAVA大神快来帮忙设计一个猜数字游戏,悬赏双手奉上~!!!

//因为时间的关系,就先没写界面了。

import java.util.Random;

import java.util.Scanner;

class GuessNum implements Runnable{

int num=0;

public synchronized void guess(){

notify();

System.err.println(Thread.currentThread().getName()+"请输入你猜中的数字");

Scanner s=new Scanner(System.in);

int number=s.nextInt();

try {

if (number==num) {

System.err.println("猜对了--游戏结束");

System.exit(0);

}else if(numbernum){

System.err.println("猜大了");

wait();

}else{

System.err.println("猜小了");

wait();

}

} catch (Exception e) {

e.printStackTrace();

}

}

public  void initNum(){

try {

Random r=new Random();

num=r.nextInt(100)+1;

System.err.println("数字已准备好"+num);

} catch (Exception e) {

e.printStackTrace();

}

}

@Override

public void run() {

initNum();

}

}

class People1 implements Runnable{

public GuessNum guessNum;

@Override

public void run() {

try {

Thread.currentThread().sleep(10);

} catch (InterruptedException e) {

e.printStackTrace();

}

while (true) {

guessNum.guess();

}

}

/** 

* @Description: People1构造函数

* @param guessNum 

*/ 

public People1(GuessNum guessNum) {

this.guessNum = guessNum;

}

}

class People2 implements Runnable{

public GuessNum guessNum;

@Override

public void run() {

try {

Thread.currentThread().sleep(20);

} catch (InterruptedException e) {

e.printStackTrace();

}

while (true) {

guessNum.guess();

}

}

/** 

* @Description: People2构造函数

* @param guessNum 

*/ 

public People2(GuessNum guessNum) {

this.guessNum = guessNum;

}

}

public class TestGuessNum {

public static void main(String[] args) {

GuessNum g=new GuessNum();

People1 p1=new People1(g);

People2 p2=new People2(g);

Thread t1=new Thread(p1);

Thread t2=new Thread(p2);

Thread t3=new Thread(g);

t1.setName("第一个人");

t2.setName("第二个人");

t3.setName("给出数字的人");

t3.setPriority(10);

t3.start();

t1.start();

t2.start();

}

}

用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课程设计程序-猜数游戏

Newload()

{

jf1=new JFrame("猜数游戏");

jf2=new JFrame("猜数游戏");

jf3=new JFrame("猜数游戏");

jf1_title=new JLabel("猜数游戏-欢迎进入");

jf1_title.setFont(new Font("仿宋体",Font.BOLD,40));//设置字体大小,及文字字体

jf1_title.setHorizontalAlignment(JLabel.CENTER);

JLabel jf2title=new JLabel("猜数游戏");

jf2title.setFont(new Font("仿宋体",Font.BOLD,40));//设置字体大小,及文字字体

jf2title.setHorizontalAlignment(JLabel.CENTER);

jf1_username=new JLabel("用户名");

jf1_userpass=new JLabel("密码");

jf2_question=new JLabel("There is question which needs you to guess!");

jf2_question.setFont(new Font("仿宋体",Font.BOLD,20));//设置字体大小,及文字字体

jf2_question.setHorizontalAlignment(JLabel.CENTER);

jf2_rightface=new JLabel(iron1);

jf2_wrongface=new JLabel(iron2);

jf2_rightface.setVisible(false);

jf2_wrongface.setVisible(false);

jf2_reelresult=new JLabel();

jf3_pinyu=new JLabel("your result is");

jf1_usernameT=new JTextField(6);

jf2_anwser=new JTextField(6);

jf2_anwser.addActionListener(this);

jf1_password=new JPasswordField(6);

jf1_password.addActionListener(this);

jf1_ok=new JButton("确定");

jf1_ok.addActionListener(this);

jf1_quit=new JButton("退出");

jf1_quit.addActionListener(this);

jf2_newgame=new JButton("新游戏(k)");

jf2_newgame.setMnemonic(KeyEvent.VK_K);

jf2_newgame.addActionListener(this);

jf2_ok=new JButton("确定");

jf2_ok.addActionListener(this);

jf1.setLayout(new BorderLayout());

jf2.setLayout(new BorderLayout());

JPanel jf1p1=new JPanel(),jf2p1=new JPanel(),jf2p2=new JPanel(),jf2p3=new JPanel();

jf2p1.setLayout(new BorderLayout());

jf1p1.setLayout(new FlowLayout());

jf2p2.setLayout(new FlowLayout());

jf2p3.setLayout(new FlowLayout());

jf1.add(jf1_title,"Center");

jf1p1.add(jf1_username);jf1p1.add(jf1_usernameT);

jf1p1.add(jf1_userpass);jf1p1.add(jf1_password);

jf1p1.add(jf1_ok);jf1p1.add(jf1_quit);

jf1.add(jf1p1,"South");

jf2p2.add(jf2_rightface);

jf2p2.add(jf2_wrongface);

jf2p2.add(jf2_reelresult);

jf2p1.add(jf2p2,"South");

jf2p1.add(jf2_question);

jf2.add(jf2title,"North");

jf2.add(jf2p1,"Center");

jf2p3.add(jf2_ans);jf2p3.add(jf2_anwser);jf2p3.add(jf2_ok);jf2p3.add(jf2_newgame);

jf2.add(jf2p3,"South");

jf3.add(jf3_pinyu);

jf1.setSize(700,400);

jf2.setSize(700,400);

jf3.setSize(700,400);

jf1.setLocation(300,150);

jf2.setLocation(300,150);

jf3.setLocation(300,150);

jf1.setVisible(true);

jf2.setVisible(false);

jf3.setVisible(false);

jf1.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

jf2.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

}

public void actionPerformed(ActionEvent e) {

if(e.getSource()==jf1_ok||e.getSource()==jf1_password)

{char[] a=jf1_password.getPassword();String paas="";

for(int i=0;ia.length;i++)//JPasswordField是一种特殊的类只能得到char数组,将其转成String

paas=paas+a[i];

if(jf1_usernameT.getText().equals("user")paas.equals("pass"))

{jf2.setVisible(true);jf1.setVisible(false);

number=returnquestion();

jf2_anwser.requestFocus();

}

else

JOptionPane.showMessageDialog(null,"用户名不正确或密码错误!");

}

if(e.getSource()==jf1_quit)

{

System.exit(0);

}

if(e.getSource()==jf2_ok||e.getSource()==jf2_anwser)

{

if(times=5){

if(Integer.parseInt(jf2_anwser.getText())==number)

{

jf2_rightface.setVisible(true);

jf2_wrongface.setVisible(false);

jf2_reelresult.setText("you are right! and your have used "+times+" times!"

+((times=3)?"very good!":"pleas do more work for it"));

}

else

if(Integer.parseInt(jf2_anwser.getText())number)

{times++;

jf2_wrongface.setVisible(true);

jf2_rightface.setVisible(false);

jf2_reelresult.setText("your answer is bigger than the one produced by computer!"

+"and your have used "+times+" times!");

}

else

if(Integer.parseInt(jf2_anwser.getText())number)

{times++;

jf2_wrongface.setVisible(true);

jf2_rightface.setVisible(false);

jf2_reelresult.setText("your answer is smaller than the one produced by computer!"

+"and your have used "+times+" times!");

}

}

else

{JOptionPane.showMessageDialog(null,"你已经超过六次了,请重新开始吧!");}

jf2_anwser.requestFocus();

jf2_anwser.setText("");

}

if(e.getSource()==jf2_newgame)

{

number=returnquestion();

times=0;

jf2_rightface.setVisible(false);

jf2_wrongface.setVisible(false);

jf2_anwser.setText("");

jf2_reelresult.setText("");

jf2_anwser.requestFocus();

}

}

public static void main(String args[])

{

new Newload();

}

int returnquestion()

{

double db=Math.random()*100;

return (int)db;

}

}

用JAVA编猜数字游戏

1)

程序随机分配给客户一个1—100之间的整数

Random

gen

=

new

Random();

int

a

=

gen.nextInt(100)+1;

//不加1是0到99

2)

用户在输入对话框中输入自己的猜测

Scanner

in

=

new

Scanner(System.in);

System.out.println("give

me

a

number")

int

b

=

in.nextInt();

3)

程序返回提示信息,提示信息分别是:“猜大了”、“猜小了”和“猜对了”。

if(ba)

System.out.println("big");

else

if(ba)

System.out.println("small");

else

System.out.println("right");

4)

用户可根据提示信息再次输入猜测,直到提示信息是“猜对了”。

while(b!=a){

if(ba)

System.out.println("big");

else

if(ba)

System.out.println("small");

System.out.println("give

me

a

new

number")

int

b

=

in.nextInt();

}

System.out.println("right");

3看懂以后,3,4可以合在一起,用4的回答.

如何用Java语言实现猜数字游戏

java实现的简单猜数字游戏代码,通过随机数与逻辑判断来实现游戏功能 代码如下: import java.util.InputMismatchException; import java.util.Scanner; public class Main { public static void main(String[] args) { // 产生一个随机数 int n

java猜数游戏设计作业的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java设计猜数字游戏、java猜数游戏设计作业的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-28,除非注明,否则均为首码项目网原创文章,转载请注明出处。