发布网友 发布时间:2022-04-22 11:31
共5个回答
热心网友 时间:2023-11-03 08:32
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() 括号中的条件表达式,当用户猜测的数和系统生成的数字不相等时,就需要继续循环。
热心网友 时间:2023-11-03 08:33
import java.util.Random;
import java.util.Scanner;
/**
* Created by Administrator on 2018/9/21 0021.
*/
public class Guess {
public static void main(String[] args) {
System.out.println("欢迎玩猜数字(智商版),5次试玩机会");
int rmb=5; //试玩次数
boolean status = true;
Random random = new Random();
int r = random.nextInt(1000);
int count=0;
//作弊代码,开挂是狗啊,请谨慎开挂。
//System.out.println(r);
while (status) {
System.out.println("请输入你要输入的数字");
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextInt()) {
int num = scanner.nextInt();
if (rmb==0){
System.out.println("请先充值谢谢。");
status=false;
}
if (num > r) {
System.out.println("您猜的数字大了");
} else if (num < r) {
System.out.println("您猜的数字小了");
} else {
System.out.println("恭喜你,你猜对了,数字是" + r);
}
count++;
rmb--;
if (rmb<1){
System.out.println("次数用完请充值,充值请输入“我是傻*”,退出请随机输入");
String q=scanner.next();
if (q.equals("我是傻*")){
System.out.println("智商充值成功!+3次机会,请继续");
rmb=rmb+3;
status=true;
}
else {
System.out.println("没钱玩个锤子,滚");
status=false;
}
}
if (num == r) {
System.out.println("游戏结束");
System.out.println("您一共猜了" + count + "次");
status = false;
}
}
else {
System.out.println("不要捣乱好吗,请输入数字,游戏结束");
status=false;
}
}
}
}
热心网友 时间:2023-11-03 08:33
package test;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
int b = (int) (Math.random()*10+1);
Scanner input=new Scanner(System.in);
System.out.println("请输入1个10以内的数字:");
int a = input.nextInt();
int i=1;
while( a != b && i<10)
{
System.out.println(a>b?"大了":"小了");
System.out.println("请输入1个10以内的数字:");
a = input.nextInt();
i++;
}
String s = "";
if(i<2)
{
s="你猜对了,你是牛人";
}else if(i<5)
{
s="你猜对了,你的成绩不错";
}else s="10次都猜不中你是脑残";
System.out.println("结果是:"+b+"你猜的次数为:"+i+","+s);
}
}
热心网友 时间:2023-11-03 08:34
import java.awt.Component;
import java.util.Random;
import javax.swing.JOptionPane;
public class Test {
public static int checkNum(int num) {
int _num = new Random().nextInt(20);
System.out.println("正确答案:"+_num);
if(num == _num) return 1;
else if(num>_num) return -1;
else return 0;
}
public static void main(String[] args) {
try {
String str = JOptionPane.showInputDialog("猜数游戏", "请输入您所猜的数字:");
int n = Integer.valueOf(str);
int num = checkNum(n);
switch (num) {
case 1:
JOptionPane.showMessageDialog(null, Component.SOMEBITS, "回答正确", 1);
break;
case -1:
JOptionPane.showMessageDialog(null, "您猜大了");
break;
default:
case 0:
JOptionPane.showMessageDialog(null, "回答小于答案");
break;
}
}catch(Throwable t) {
JOptionPane.showMessageDialog( null, "请输入数字");
}
}
}
热心网友 时间:2023-11-03 08:34
猜0-100的数
public class Use {
String[] name = new String[5];//保存注册的用户名
String[] pad = new String[5];//保存注册的密码
String fName;//用户名
String fPassword;//密码
boolean fond = false;
boolean con = false;
int i = 0;
int[] number = new int[5];//猜的数字
int count = 0;//次数
int random = (int) (Math.random() * 100) + 1;//随机数
Scanner input = new Scanner(System.in);
int select;//选择
//首页的方法
public void souYe() {
System.out.println("*****欢迎进入猜数字游戏*******");
System.out.println("1.注册\n2.登录\n3.退出游戏");
System.out.println("请选择你要的操作:");
String no = input.next();
switch (no) {
case "1":
login();
break;
case "2":
register();
break;
case "3":
System.out.println("停止游戏");
break;
default:
System.out.println("选择输入无效");
break;
}
}
//实现注册功能
public void login() {
System.out.println("****游戏>注册****");
for(i=3;i>=0;i--){
System.out.print("请输入用户名:");
name[i] = input.next();
System.out.print("请输入密码:");
pad[i] = input.next();
if (name[i].length() >= 6 && name[i].length() <= 10 && pad[i].length() >= 6) {
System.out.println("注册成功,用户名是"+name[i]+",密码为"+pad[i]);
break;
} else {
System.out.println("用户名长度在6-10之间,密码不能小于6位,");
System.out.println("请重新输入:");
}
}
fond = true;
System.out.println("是否返回上一层?(1.是/2.否)");
select = input.nextInt();
if (select == 1) {
souYe();
}
}
//实现登录功能
public void register() {
for(int i=0;i<3;i++){
System.out.println("****游戏>登录****");
System.out.print("请输入用户名:");
fName = input.next();
System.out.print("请输入密码:");
fPassword = input.next();
if (fName.equals(name[i]) && fPassword.equals(pad[i])) {
System.out.println("登录成功");
game();
} else {
System.out.println("用户名或密码错误");
System.out.println("请重新输入:");
}
}
con = true;
System.out.print("是否返回上一层?(1.是/2.否)");
select = input.nextInt();
if (select == 1) {
souYe();
}
}
//开始游戏
public void game() {
System.out.println("*****欢迎您进入猜数字游戏****");
for (count = 5; count>=0; count--) {
System.out.println("这里是1-100的数字,请输入你猜的答案:");
Scanner input = new Scanner(System.in);
number[i] = input.nextInt();
if (number[i] < random) {
System.out.println("您猜的数是" + number[i] + "" + "***猜小了***");
} else if (number[i] > random) {
System.out.println("您猜的数是" + number[i] + "" + "***猜大了***");
}else if(number[i]==0){
System.out.println("游戏终止");
}else if(number[i]>100&&number[i]<0){
System.out.println("超出猜数范围。");
}
else{
System.out.println("您猜的数是" + number[i] + "恭喜你,猜对了,你真棒。");
break;
}
System.out.println("您还有" + count+ "次机会");
System.out.print("是否继续?(1.是/2.否)");
int no = input.nextInt();
if (no == 1) {
if ((count - i) == 0) {
System.out.println("很遗憾,你没有机会了,正确数字是:" + random);
System.out.println("是否返回上一层?(1.是/2.否)");
select = input.nextInt();
if (select == 1) {
souYe();
}
} else {
System.out.println("游戏结束,谢谢使用。");
break;
}
}
}
}
}