我使用 while 循环制作了一个数字游戏,但有人告诉我我应该使用 do while 来代替。我想知道如何将它从这种格式转移到做while。
import java.util.Scanner;
public class SS_Un6As2 {
public static void main(String[] args) {
int guess = 0;
int randomNumber = (int) (Math.random() *100) +1;
Scanner scan = new Scanner(System.in);
boolean running = true;
while(running){
System.out.println("Guess your number");
guess = scan.nextInt();
if(guess == randomNumber) {
System.out.println("You are correct");
break;
}
else if (guess > randomNumber){
System.out.println("Too high.");
}
else if (guess < randomNumber){
System.out.println("Too low");
}
else {
System.out.println("Try again");
}
}
System.out.println("Thanks for playing");
}
}