java猜数字游戏?

java猜数字小游戏代码怎么写?~

import java.util.Scanner;public class Guess { private static final int F = 1; private static final int T = 100; public static void main(String[] args) { int num = (int)(Math.random() * T) + F; Scanner s = new Scanner(System. in ); // 初始化 int guest = -1; while (true) { System.out.print("请输入你猜测的数字(" + F + "-" + T + "):"); // 每次输入的值,采纳即可 guest = s.nextInt(); if (guest > num) { System.out.println(F + "--" + guest); } else if (guest < num) { System.out.println(guest + "--" + T); } else { System.out.println("中奖了"); break; } } s.close(); }}

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() 括号中的条件表达式,当用户猜测的数和系统生成的数字不相等时,就需要继续循环。

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猜数字游戏1.要求随机产生一个数,猜中即为赢,猜不中将提示大了还是...
答:package test;import java.util.Random;import java.util.Scanner;public class GuessNum {public static void main(String[] args) {Scanner sc = new Scanner(System.in);Integer randomNum = new Random().nextInt(100), count = 0;long start = System.currentTimeMillis();boolean bingo = ...

java 编写生成一个10 20 (包括10和20)的随机整数,然后在输入文本框输...
答:1、猜数字游戏 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 ...

怎么用java写一个游戏排名界面,最好是有代码和解释,谢谢!
答: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...

java中四个不重复的数字猜数字游戏
答:int userNum = sc.nextInt();if (gameNum == userNum) {// 如果用户输入等于系统生成,执行下面的代码 System.out.println("恭喜你猜对了O(∩_∩)O哈哈~");num();// 继续猜下一个游戏 } else if (userNum > gameNum) { System.out.println("数字有点大噢/(ㄒoㄒ)/~~");} else...

java猜数字小游戏。用eclipse写的
答:import java.util.Scanner;/*** Java命令行版 猜数字游戏* @author kaifang*/public class GuessNum {public static void main(String[] args) {System.out.println("===猜数字游戏===\n");int answer = (int)(Math.random() * 200 + 1);Scanner sr = new Scanner(System.in);while(tr...

用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 < ...

求教Java达人:用java编写一个猜数字游戏
答:package com.zuxia.cg.guest;import java.util.Random;import java.util.Scanner;/ 猜数游戏 系统自动生成4个0-9的不重复数 用户猜 数字和系统生成的数是一样且位置相同就在数那个位置输出a,数相同但位置不同,则在数那个位置输出b 其他数字不变 author student / public class test { / 产生不...

Java猜数字游戏,事先随机给出3个100以内的正确答案,然后再输入100以内...
答:Random r = new Random();int num[] = new int[3];for(int i = 0;i<3;i++){int n = r.nextInt(100);num[i] = n;}System.out.println("请输入数字:");Scanner s = new Scanner(System.in);int input = s.nextInt();boolean res = false;System.out.println("我猜的数字...

java猜数字小游戏,
答:if(TEMP<0||TEMP>100){System.out.println("输入的数字必须在0-100");} int i=1;int a[]=new int[100];int k=0;while(TEMP!=TARGET){ if(TEMP>TARGET){ str=JOptionPane.showInputDialog("大了点!put in yourGuess again:");TEMP=Integer.parseInt(str);i++;} else if(TEMP<...

JAVA 设计猜数字小游戏
答:面版代码while死循环,不断打印规则并scanner获取用户输入的选择(如Y重新再来),然后调用猜数游戏,如果猜对了使用retrun;跳出循环并打印’游戏结束‘字样,猜数游戏调用完毕,面版代码重新循环打印规则并scanner获取输入,又可以再选择重新开始或退出游戏。当退出游戏时使用System.exit(0);结束程序。

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网