Package Practicego;import java.util.scanner;/* * 6, the computer randomly generated a number of 0~9, the user to guess the number. The value is correct until it is guessed correctly. */public class Cto {public static void main (string[] args) {Scanner sc = new Scanner (system.in), while (true) {System.out.pri NTLN ("Guess number:"), int pernum = Sc.nextint (), int sysnum = (int) (Math.random () *10), if (Sysnum = = Pernum) {System.out.println (" Congratulations, you guessed it!! "); break;}}}
Test results:
Guess number: 9 Please guess the number: 9 Please guess the number: 9 Please guess the number: 9 Please guess the number: 9 Please guess the number: 9 Please guess the number: 9 Please guess: 9 Please guess the number: 9 Please guess the number: 9 Please guess the number: 9 Please guess the number: 9.
Java loop exercise: guessing games