在此程序中,用户必须考虑一个数字,然后由计算机进行猜测。
计算机将向用户询问范围,
计算机将猜测边界之间的数字,
用户将键入“较高”或“较低”以使计算机猜测正确的数字,
如果计算机猜对了答案,则用户将输入“是”,并打印正确的声明。
下面的代码是我正在处理的代码,但是,我陷入了一些问题。请帮帮我!谢谢!
我发现的问题:
1。。我不了解如何在类内部但在主函数外部定义randomInt函数。
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
答案 0 :(得分:0)
我阅读了您的代码,但做了些改动。我认为我的更改将反映出您可以做的更好的事情,但是请注意以下几点:
SecureRandom
类,这要好得多。您必须使用nextInt
来获取随机数,而我只能创建该类型的一个对象in.next()
,因为在读取字符串之前会忽略空格。如果您使用nextLine()
,则nextInt()
仅读取数字,而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();
}
}
希望对您有帮助。