我是编码新手。我创建了一个猜谜游戏,并且运行良好,但是,我想知道如何制作它,以便在用户尝试对数字进行3次猜测之后,他们会得到提示,我将其放在最后一行,但目前无法实现,而且我不知道如何在train = df.loc[perm[:train_end]]
test = df.loc[perm[train_end:]]
#include "C:\Users\CLOS\Desktop\New folder\Reg9s12.H"
TEMP DC.B 1
RAMEnd EQU $1001
MAIN:
LDS #RAMEnd+1
BSET INTCR,%1100000
CLR TEMP
CLI
BSET DDRB,%00010000
BCLR PORTB,%00010000
HERE BRA HERE
IRQ_EDGE
LDAA TEMP
EORA #%00010000
STAA TEMP
RTI
ORG $FFF2
DC.W IRQ_EDGE
ORG $FFFE
DC.W MAIN
循环中使语句可访问。我目前陷入困境。谢谢
do
答案 0 :(得分:0)
程序中存在一些流程问题,但这是一种修复程序的简单方法。
首先,当您从guess
方法中返回run()
的值时,实际上并没有使用它,因此可以将其删除。
此外,在这种情况下,您不想使用do/while
循环,而只想使用while
。您要一直重复提示,直到用户正确猜出为止。因此,添加boolean
可以检查他们是否赢了:
boolean correct = false;
-我们将其设置为false,因为他们还没有赢。
现在,不要在每次猜测后再次调用run()
(每次都会重置tries
计数,只需让while
循环执行其工作并重复一次即可。因此,我们需要将提示输入移至while
循环中。
以下是更改的完整代码清单:
import java.util.Scanner;
public class guessing_game {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
desc();
run(kb);
//int nun = 0;
//for (int i = 0; i < nun; nun ++)
}
public static void desc() {
System.out.println("This is a guessing game.");
System.out.println();
System.out.println("Let's see how many tries it takes you to guess the right number!");
System.out.println();
System.out.println();
System.out.println();
}
// Change the return type to void as you never use the value returned
public static void run(Scanner kb) {
int num = 44;
// Add a boolean to determine if the game is won
boolean correct = false;
int tries = 0;
while (!correct) {
System.out.println("Please enter a number between 1-100");
int guess = kb.nextInt();
if (guess < num) {
System.out.println("Oooh. Your guess is too low. Try again.");
System.out.println();
} else if ((guess > 100) || (guess < 0)) {
System.out.println("That isn't between 1-100 is it?");
System.out.println();
} else if (guess > num) {
System.out.println("Aaah. Your guess is too high. Try again.");
System.out.println();
} else if (guess == num) {
// Flag the guess as correct; this will exit the loop after this run
correct = true;
System.out.println("Bingo!!! Nice guess bud.");
System.out.println("Tell a friend to play! Wanna try again? (y or n)");
String choice = kb.next();
if (choice.equalsIgnoreCase("y")) {
run(kb);
} else if (choice.equalsIgnoreCase("n")) {
System.exit(0);
}
}
tries++;
}
}
}
答案 1 :(得分:0)
1。您递归地调用run()方法,每次调用此方法时,都会创建一个新的变量try并将其初始化为零。 2.您的递归调用在条件检查之前,并且由于同样的原因,逻辑可能永远也不会到达条件检查。
要以最小的更改使它起作用,可以使用以下代码。但这不是最好的方法,因为它不能解决上述缺点
import java.util.Scanner;
公共类的guessing_game {
static int tries = 0;
public static void main (String[] args)
{
Scanner kb = new Scanner(System.in);
desc();
run(kb);
//int nun = 0;
//for (int i = 0; i < nun; nun ++)
}
public static void desc()
{
System.out.println("This is a guessing game.");
System.out.println();
System.out.println("Let's see how many tries it takes you to guess the right number!");
System.out.println();
System.out.println();
System.out.println();
}
public static int run(Scanner kb)
{
System.out.println("Please enter a number between 1-100");
int guess = kb.nextInt();
int num = 44;
do{
tries++;
if(tries>=3) break;
if (guess < num)
{
System.out.println("Oooh. Your guess is too low. Try again.");
System.out.println();
run(kb);
}
else if ((guess > 100) || (guess < 0))
{
System.out.println("That isn't between 1-100 is it?");
System.out.println();
run(kb);
}
else if (guess > num)
{
System.out.println("Aaah. Your guess is too high. Try again.");
System.out.println();
run(kb);
}
else if(guess == num)
{
System.out.println("Bingo!!! Nice guess bud.");
System.out.println("Tell a friend to play! Wanna try again? (y or n)");
String choice = kb.next();
if (choice.equalsIgnoreCase("y"))
{
run(kb);
}
else if (choice.equalsIgnoreCase("n"))
{
System.exit(0);
}
}
}while( tries < 3);
{
System.out.print("Here's a hint the lucky number is 4");
}
return guess;
}
}