「java猜数游戏简介」猜数游戏java代码

博主:adminadmin 2023-01-04 05:00:08 682

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

本文目录一览:

java猜数字游戏?

import java.util.Random;

import java.util.Scanner;

/**

* @Author: Cool_Wu

* @Date: 2020-12-01 23:39

*/

public class GuessNumberGame {

 static int count = 0;

 static int answer = new Random().nextInt(100);

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

     System.out.println("猜数字游戏开始,该数字是一个0~100之间的整数");

     compareNum();

 }

 public static void compareNum() throws Exception {

     if (count = 10){

         System.out.println("正确答案是:" + answer);

         System.out.println("你太笨了,下次再来吧!");

         return;

     }

     count++;

     int n = receiveNum();

     if (n 0){

         throw new Exception("您输入的数字不符合要求,请重新输入!");

     }

     if (n 99){

         throw new Exception("输入错误,请输入正确的数字!");

     }

     if (n answer){

         System.out.println("太小了,再大一点!");

         compareNum();

     }

     if (n answer){

         System.out.println("太大了,再小一点!");

         compareNum();

     }

     if (n == answer){

         System.out.println("恭喜你,猜对了!");

     }

 }

 public static int receiveNum() {

     System.out.println("请输入您猜的数字:");

     int n = new Scanner(System.in).nextInt();

     return n;

 }

}

运行结果

Java猜数字游戏

public static void main(String[] args) {

// TODO 自动生成方法存根

System.out.println("欢迎进入猜数字游戏!您只有10次机会!猜的数字在0到100之间");

Random r = new Random();

int num = r.nextInt(100);

Scanner input = new Scanner(System.in);

int cai;

for (int i = 0; i 10; i++) {

System.out.print("输入竞猜数字:");

cai = input.nextInt();

if (cai 0 || cai 100) {

System.out.println("数字在0到100之间");

continue;

}

if (cai == num) {

System.out.println("猜中数字,胜利了");

break;

} else {

System.out.println("没有猜中");

}

if (i == 9) {

System.out.println("时间到,竞猜失败");

}

}

}

java猜数游戏

//线程调用创建主类

import java.util.concurrent.Callable;  

import java.util.concurrent.ExecutionException;  

import java.util.concurrent.ExecutorService;  

import java.util.concurrent.Executors;  

import java.util.concurrent.Future;  

  

public class CallableTest {  

  

    /** 

     * @param args 

     * @throws ExecutionException 

     * @throws InterruptedException 

     */  

    public static void main(String[] args) throws InterruptedException,  

            ExecutionException {  

  

        // 线程池  

        ExecutorService pool = Executors.newFixedThreadPool(10);  

  

        CallableString c1 = new CallableThread("线程1");  

        CallableString c2 = new CallableThread("线程2");  

  

        while(true){

         // 表示异步计算的结果  

         FutureString f1 = pool.submit(c1);  

         FutureString f2 = pool.submit(c2);  

        

         if("100".equals(f1.get())||"100".equals(f2.get())){

         // 关闭线程池  

         pool.shutdown(); 

         break;

         }

        }

  

    }  

  

}

//线程计算类

import java.util.concurrent.Callable;  

public class CallableThread implements CallableString {  

    private String str;  

  

    public CallableThread(String str) {  

        this.str = str;  

    }  

  

    // 需要实现Callable的Call方法  

    public String call() throws Exception {  

         

        

        int num = 0;

for (int j = 0; j  10; j++) {

num += (int)(Math.random() * 30)+3;

}

        String strnum = ""+num;

        

        System.out.println(str+":"+strnum);  

        

        return strnum;  

    }  

}

--如果可以,望采纳

用JAVA语言编写一个“猜数字游戏”的程序

int num = (int)(Math.random()*100)+1;

Scanner sc = new Scanner(System.in);   

int guessNum = -1;

while (guessNum != num) {

System.out.println("请输入1-100之间整数");

guessNum = sc.nextInt();

if (guessNum == num) {

System.out.println("中啦");

} elseif (guessNum num) {

System.out.println("小啦");

} else {

System.out.println("大了");

}

}

扩展资料:

编写思路

1、成1-100之间随机数

(int)(Math.random()*100)+1;

提示用户输入数字,

Scanner  sc=new Scanner(System.in);

int guessNum = sc.nextInt();

需要将随机数和用户输入的数字进行比较。

猜一次:

Scanner sc = new Scanner(System.in);

int num = (int)(Math.random()*100)+1;

System.out.println("请输入0-100之间整数");

int guessNum = sc.nextInt();

if (guessNum == num) {

System.out.println("中啦");

} elseif (guessNum num) {

System.out.println("小啦");

} else {

System.out.println("大了");

}

二、使用while循环

publicstaticvoid main(String[] args) {

int num = (int)(Math.random()*100)+1;

Scanner sc = new Scanner(System.in);

while (true) {

System.out.println("请输入1-100之间整数");

int guessNum = sc.nextInt();

if (guessNum == num) {

System.out.println("中啦");

} elseif (guessNum num) {

System.out.println("小啦");

} else {

System.out.println("大了");

}

}

}

三、最后用while() 括号中的条件表达式,当用户猜测的数和系统生成的数字不相等时,就需要继续循环。

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语言实现猜数字游戏

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

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