Today I was teaching software project management to students, and I found that the students wanted to sleep. I had an idea and wrote a small game for everyone, let’s guess together~
use using using using ‐ ‐ ’ off ‐ ‐ ‐ ‐‐‐
view plaincopy to clipboardprint?
Guess <script> <br>//Randomly generate a random integer <br>// Rounding Random number (0-1) <br>var num = Math.round(Math.random()*1000); <br>//Define a flag variable <br>var falg=true; <br>//Define a variable to count the number of guesses <br>var count=1; <br> <br>document.write(" <br><br><br><br><br><br><br><br>"); <br> <br>//Execute while loop <br>while(falg){ <br> if(count<=10){ <br> Compare the input content with the generated random number Document. write("This is your "+count+" chance. The number you entered this time is "+inputnum+", the number you entered is too big<br>"); Situation: 2) The input number is smaller than the generated random number. The prompt is "too small" <br> The number entered this time is "+inputnum+", the number you entered is too small<br>"); <br> count++; Set the flag position as false; <br> if (inputnum == NUM) {<br> if (count & lt; = 3) {<br> // Output a sentence, <br> Alert ("You super cow X, guess! "); Correct! "); <br> ){<br>//Output a sentence, <br> Then, t ALERT ("I dizzy, I didn't guess 10 times, how can I do it?!"); <br></head> <br> <br><body> <br> <br></body> <br></html> <br><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: //www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br><html xmlns="http://www.w3.org/1999/xhtml"><br><head> <br><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><br><title>Guess it</title><br><script><br>// Randomly generate a random integer<br>// Rounding random number (0-1)<br>var num = Math.round(Math.random()*1000);<br>//Define a flag variable<br>var falg=true;<br>//Definition A variable, counting the number of guesses<br>var count=1;<br><br>document.write("<br><br><br><br><br><br><br>< br>");<br></p>
<p>//Execute the while loop<br>while(falg){<br> <br> if(count<=10){<br> //The first step is to accept the content input by the user<br> var inputnum=prompt("Please enter the number you want to guess!" ,1);<br> <br> //The second step is to compare the content input by the user with the generated random number <br> <br> //Three situations: 1) The input number is greater than the generated random number and the prompt is "too big" <br> if(inputnum> ;num){<br> //Output a sentence, <br> document.write("This is your "+count+" chance, the number you entered this time is "+inputnum+", the number you entered is too big<br> ");<br> Count++;<br> }<br> <br> //Three situations: 2) The input number is less than the generated random number. The prompt is "too small" <br> if(inputnum<num){<br> } //Output a sentence, <br> document.write( "This is your "+count+" chance. The number you entered this time is "+inputnum+". The number you entered is too small<br>");<br> Count++;<br> }<br> //Three situations: 1 )The number entered is equal to the random number generated. Prompt "Right", set the flag to false;<br> if(inputnum==num){<br> if(count<=3){<br/> //Output a sentence, <br/> alert("You Awesome, guessed it right! ");<br/> }<br/> if(count<=6&&count>3){<br> if(count<=10&&count>6){<br> ; <br> S}} else {<br> <br> // Output a sentence, <br> Alert ("I am dizzy, I didn't guess the right 10 times, how can I do it?!"); <br>}<br><br></script>
Interested students can study it~