「java写数字游戏」java数字游戏编程

博主:adminadmin 2022-12-17 23:09:09 63

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

本文目录一览:

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

import java.util.Random;

import java.util.Scanner;

public class GuessingNumber {

    private int magicNum;

    public GuessingNumber() {

        magicNum = new Random().nextInt(10);

    }

    public void guess() {

        Scanner scanner = new Scanner(System.in);

        int expected = Integer.MIN_VALUE;

        System.out.println("Plesae input an integer between 0 and 10: ");

        while(scanner.hasNextInt()) {

            expected = scanner.nextInt();

            if(expected == magicNum) {

                System.out.println("Congratulations");

                break;

            } else {

                System.out.println("Please try again, input another integer between 0 and 10: ");

            }

        }

    }

}

public class TestGuessingNumber {

    public static void main(String[] args) {

        new GuessingNumber().guess();

    }

}

输出结果

Plesae input an integer between 0 and 10:

5

Please try again, input another integer between 0 and 10:

8

Please try again, input another integer between 0 and 10:

1

Please try again, input another integer between 0 and 10:

2

Congratulations

用JAVA写一个程序 求解 题目:猜数字游戏 随机生成4个0到9的整数,组成一个序列

import java.util.Random;

import java.util.Scanner;

/*

 * 游戏随即给出一个0~99(包括0和99)的数字,然后让你猜是什么数字。你可以随便猜一个数字,游戏会提示太大还是太小,从而缩小结果范围。经过几次猜测与提示后,最终退出答案。在游戏过程中。记录你最终猜对时所需要的次数。游戏结束后公布结果。见下

次数                 结果

1                    你太有才了!

2~6                  这么快就猜出来了,很聪明么!

大于7                猜了半天才猜出来,小同志,尚需努力啊!

 */

public class guessGame {

 /**

  * @param args

  */

 public static void main(String[] args) {

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

  System.out.println("Rand:"+gameValue);

  Scanner sc = new Scanner(System.in);

  System.out.println("请输入一个数字");

  int num = sc.nextInt();

  int guessCorrectNum=1;

  

  while(true){

 

  if(num==gameValue){

   if(guessCorrectNum == 1)

   System.out.println("你太有才了!");

   else if((guessCorrectNum =2)  (guessCorrectNum=6))

   System.out.println("这么快就猜出来了,很聪明么");

   else if(guessCorrectNum 7)

   System.out.println("猜了半天才猜出来,小同志,尚需努力啊!");

break;

  }

  else{

   if (guessCorrectNum =20){

   

    guessCorrectNum = guessCorrectNum + 1;

    num = sc.nextInt(); 

    

   }

   else{

    System.out.println("20次都猜不出来...,不让你猜了");

    break;

   }

  }

  }

 }

}

please tell me your q-number,so I can send it by q.

用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.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写数字游戏」java数字游戏编程

用Java写了猜数字的游戏,我怎么猜了一次就结束了

给你改了一下

public static void game() {

        System.out.println("——————————游戏开始——————————");

        boolean status = true;

        int num = new Random().nextInt(100);

        // System.out.println("目标值为:"+num);

        int count = 1;

        while (status) {

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

            Scanner sc = new Scanner(System.in);

            int temp = sc.nextInt();

            if (temp  num) {

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

            } else if (temp  num) {

                System.out.println("您猜小了");

            } else {

                System.out.println("恭喜您猜对了");

                status = false;

            }

            count++;

            if (count  5  status) {

                System.out.println("抱歉您猜了5次都没对,游戏结束了");

                status = false;

            }

        }

        System.out.println("——————————游戏结束——————————");

    }

用Java编写一个猜数字的游戏

新手炒沥青初期需要投入多少资金?

我这边是3W,最低没有门槛,你就是入两千都可以玩,但问题是两千只能操作1手,而且没有抗风险资金了,你买了之后只要点位瞬间波动一个点你就直接平仓了,没法玩的,所以我都是入金五万以上玩的。

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

The End

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