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

2025-03-10 23:11:21
推荐回答(1个)
回答1:

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.