在这个程序中,用户必须想出一个数字,然后让计算机来猜。
2.我试过这个程序,但电脑的猜测很奇怪。计算机不会根据我的指南猜出一个数字。
public class Guess
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Random rand = new Random();
//ask about the lower bound,
TextIO.putln("Hey there! Welcom to the game!\n"
+ "I'm going to guess a number, and you have to pick it\n"
+ "and you get to decide the bounds.\n" +
"What should the lower bound be?");
int lowerlimit = TextIO.getInt();
//if the user enter the number lower than 0,
while ((lowerlimit < 0))
{
TextIO.putln("ERROR Please enter a number that greater than 0");
lowerlimit = TextIO.getInt();
}
//ask about the upper bound,
TextIO.putln("Great! How about the upper bound?");
int upperlimit = TextIO.getInt();
//if the user enter the number lower than 0,
while ((upperlimit <= lowerlimit))
{
TextIO.putln("ERROR Please enter a number "
+ "that greater than the lower bound.");
upperlimit = TextIO.getInt();
}
//Print the range and instruction,
TextIO.putln("Ok. In the range between " + lowerlimit +
" and " + upperlimit + ":" + "\nPlease enter 'lower'/'higher' "
+ "when the number I picked is not correct\n"
+ "Enter 'yes' when I picked the right number"
);
//Generate the random number between the range,
int randNum = (int) ((Math.random() * ((upperlimit - lowerlimit) + 1)) + lowerlimit);
String feedback = "";
while (!feedback.equals("yes"))
{
TextIO.putln("I think it is " + randNum);
feedback = in.nextLine();
//When the computer need to pick a lower number,
if (feedback.equals("lower"))
{
upperlimit = randNum - 1;
randNum = (int) ((Math.random() * ((upperlimit - lowerlimit) + 1)) + lowerlimit);
}
//When the computer need to pick a higher number,
else if (feedback.equals("higher"))
{
lowerlimit = randNum + 1;
randNum = (int) ((Math.random() * ((upperlimit - lowerlimit) + 1)) + lowerlimit);
}
}
{
//When the user guesses the correct number,
TextIO.putln("Yes! Correct!:)");
}
in.close();
}
public static int randomInt(int min, int max) {
return (int) ((Math.random() * ((max - min) + 1)) + min);
}
} // end of Guess class
我看了你的代码,我改变了一点。我认为我的变化将反映出你可以做些什么来变得更好,但有些注释:
securerandom
类,这样要好得多。您必须使用NextInt
来获取随机数,我只创建一个该类型的对象,因为在读取字符串之前会忽略空间。如果使用nextline()
,则nextint()
只读取number,而nextline()
将首先捕获零字符串。
它是改进的代码:
public class Guess
{
public static SecureRandom random = new SecureRandom();
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//ask about the lower bound
System.out.println("Hey there! Welcome to the game!\n"
+ "I'm going to guess a number, and you have to pick it\n"
+ "and you get to decide the bounds.\n" +
"What should the lower bound be?");
int lower = in.nextInt();
//if the user enter the number lower than 0
while (lower < 0)
{
System.out.println("ERROR Please enter a number that greater than 0");
lower = in.nextInt();
}
//ask about the upper bound
System.out.println("Great! How about the upper bound?");
int upper = in.nextInt();
//if the user enter the number lower than 0
while (upper <= lower)
{
System.out.println("ERROR Please enter a number "
+ "that greater than the lower bound.");
upper = in.nextInt();
}
//Print the range and instruction
System.out.println("Ok. In the range between " + lower +
" and " + upper + ":" + "\nPlease enter 'lower'/'higher' "
+ "when the number I picked is not correct\n"
+ "Enter 'yes' when I picked the right number"
);
//Generate the random number between the range
String feedback = "";
while (!feedback.equals("yes"))
{
int randNum = lower + random.nextInt(upper - lower + 1);
System.out.println("I think it is " + randNum);
feedback = in.next();
//When the computer need to pick a lower number,
if (feedback.equals("lower"))
upper = randNum - 1;
// When the computer need to pick a higher number,
if (feedback.equals("upper"))
lower = randNum + 1;
}
//When the user guesses the correct number,
System.out.println("Yes! Correct!");
in.close();
}
}
我希望这能帮到你。
每次产生一个随机数。 用法 Your browser does not support the video tag. 案例:掷骰子 功能:设置随机数范围1-6,每按一下按钮,产生一个随机数 工作原理 当输入由no变为yes时,一个随机数将会被传送到输出。你可以通过配置改变随机数的范围 例如:一个随机变色的灯
给定一个随机数生成器random(7),它可以以相等的概率生成数字1,2,3,4,5,6,7(即每个数字出现的概率为1/7)。现在我们要设计一个随机数(5),它能以相等的概率(1/5)生成1,2,3,4,5。 有一种方法:每次我们随机运行(7),只有当它生成1-5时才返回。如果是6或7,再运行一次,直到它是1-5。 我有点困惑。第一个问题是: 如何用数学方法证明每个数字发生的概率是1/5?例如,假
问题内容: 我正在寻找一个随机数,并将其发布到特定user_id的数据库表中。问题是,相同的数字不能使用两次。有上百万种方法可以做到这一点,但是我希望对算法非常热衷的人能够以一种优雅的解决方案巧妙地解决问题,因为它满足以下条件: 1)最少查询数据库。2)通过内存中的数据结构进行的爬网次数最少。 本质上,这个想法是要执行以下操作 1)创建一个从0到9999999的随机数 2)检查数据库以查看该数字是
我正在制作一个2人迷宫跑者游戏,我遇到了一些键盘事件的麻烦。如果两个玩家同时击中一个键,只有玩家1移动,因为我的代码首先测试的是Player1的事件。在python和pygame中是否有一种方法可以同时检查这两个事件?下面是我的player1课的一部分: 很抱歉出现了代码块,但这对您理解它是如何工作的都是必要的。我为Player2提供了一个几乎相同的类,但使用了不同的键盘控制(WASD而不是箭头键
Go的math/rand包提供了伪随机数的生成。 package main import "fmt" import "math/rand" func main() { // 例如`rand.Intn`返回一个整型随机数n,0<=n<100 fmt.Print(rand.Intn(100), ",") fmt.Print(rand.Intn(100)) fmt.Pri
numpy.random 模块对 Python 内置的 random 进行了补充,增加了一些用于高效生成符合多种概率分布的样本值的函数。这一小节将详述如何用 Numpy 快速创建随机数矩阵。 1. 创建符合均匀分布的随机数组 1.1 numpy.random.rand 函数 numpy.random.rand 函数通常用来创建一个服从 “0~1” 均匀分布的随机浮点数(组),随机样本取值范围是[0