需要帮助!java中的数字向导?
我只是想让NumberWizard
在java上工作,但我没能完全理解。或者你可以不用java吗?我被卡住了,因为我只能猜一次
我正在使用Processing 3,我的代码如下所示:
int max = 1000, min = 1, guess = 500;
void setup() {
size(400, 400);
background(0);
println("welcome to NumberGuesser ");
println("In this game you're gonna think of a ");
println("number and im gonna guess it as fast as possible ");
println(" \n ");
println("Now pick of a number between " + min + "and" + max);
println("Great! now that you have picked a number ");
println("Press the up-arrow if its more");
println("and the down-arrow if it's less ");
println("is it less or more than "+ guess + "? \nif more press up-arrow, if less press down-arrow");
}
void draw() {
if (key == CODED) {
if (keyCode == 38) { // up-arrow
min = guess;
nextGuess();
} else if (keyCode == 40) { //down-arrow
max = guess;
nextGuess();
} else if (keyCode == 13) { //return
win();
}
}
}
void nextGuess() {
frameRate(1);
guess = (max + min) /2;
println("Is it less or more than "+ guess + "? \nif more press up-arrow, if less press down-arrow ");
}
void win() {
println("that was'nt hard at all, ezz pezz! ");
noLoop();
}
# 1 楼答案
请注意
draw()
函数每秒触发60次。即使在调用frameRate(1)
之后,draw()
函数每秒也会触发一次。进一步注意,所有逻辑都在draw()
函数中。所以猜测会在一段时间内发生您也没有检查当前是否按下了该键。请注意,
key
和keyCode
变量包含最近按下的键。您仍然需要检查当前是否按下了该键。您可以使用keyPressed
变量或keyPressed()
函数来实现这一点如果我是你,我会修改我的程序,使用
keyPressed()
函数来检测用户输入,而不是在draw()
函数中轮询用户输入此外,你需要养成debugging your code的习惯。尝试将问题隔离到一行特定的代码上,这些代码的行为与您预期的不同,而不是发布完整的程序并说它不起作用