Develop a small game with simple JavaScript knowledge ~, Ultra-simple

Source: Internet
Author: User
Today, I told my students about software project management. I found that students want to go to bed and have the opportunity to give them a small game to guess about it ~ Simple: Example source code: viewplaincopytoclipboardprint? & Lt ;! DOCTYPEhtmlPUBLIC & quot;-// W3C // DTDXHTML1.0. .. SyntaxHi

Today, I told my students about software project management. I found that students want to go to bed and have the opportunity to give them a small game to guess about it ~

Simple:

Sample source code:

View plaincopy to clipboardprint? 
 
 
 
Guess 
Script
// Generate a random integer
// Rounding a random number (0-1)
Var num = Math. round (Math. random () * 1000 );
// Define a flag variable
Var falg = true;
// Define a variable to count the number of guesses
Var count = 1;
 
Document. write ("







");
 
// Execute the while LOOP
While (falg ){

If (count <= 10 ){
// Step 1: accept the content entered by the user
Var inputnum = prompt ("Enter the number you want to guess! ", 1 );

// Step 2: Compare the user input content with the generated Random Number

// Three cases: 1) if the input number is greater than the random number, the system prompts "large"
If (inputnum> num ){
// Output a sentence,
Document. write ("this is your" + count + "opportunity. The number you entered this time is" + inputnum + ". The number you entered is large.
");
Count ++;
}

// Three cases: 2) if the number of inputs is smaller than the number of random numbers, the system prompts "smaller"
If (inputnum // Output a sentence,
Document. write ("this is your" + count + "opportunity. The number you entered this time is" + inputnum + ". The number you entered is smaller.
");
Count ++;
}
// Three cases: 1) if the number of inputs is equal to the number of random numbers, the system prompts "yes" and sets the flag to false;
If (inputnum = num ){
If (count <= 3 ){
// Output a sentence,
Alert ("You are amazing, you guessed it! ");
}
If (count <= 6 & count> 3 ){
// Output a sentence,
Alert ("You can, but you guessed it! ");
}
If (count <= 10 & count> 6 ){
// Output a sentence,
Alert ("barely, that's right! ");
}
// Set the flag to false.
Falg = false;
}




} Else {

// Output a sentence,
Alert ("I'm dizzy. I haven't guessed it for 10 times. Why ?! ");
// Set the flag to false.
Falg = false;
}

}
 
Script
 
 
 
 
 
 
 
 




Guess
Script
// Generate a random integer
// Rounding a random number (0-1)
Var num = Math. round (Math. random () * 1000 );
// Define a flag variable
Var falg = true;
// Define a variable to count the number of guesses
Var count = 1;

Document. write ("







");

// Execute the while LOOP
While (falg ){
 
If (count <= 10 ){
// Step 1: accept the content entered by the user
Var inputnum = prompt ("Enter the number you want to guess! ", 1 );

// Step 2: Compare the user input content with the generated Random Number

// Three cases: 1) if the input number is greater than the random number, the system prompts "large"
If (inputnum> num ){
// Output a sentence,
Document. write ("this is your" + count + "opportunity. The number you entered this time is" + inputnum + ". The number you entered is large.
");
Count ++;
}

// Three cases: 2) if the number of inputs is smaller than the number of random numbers, the system prompts "smaller"
If (inputnum // Output a sentence,
Document. write ("this is your" + count + "opportunity. The number you entered this time is" + inputnum + ". The number you entered is smaller.
");
Count ++;
}
// Three cases: 1) if the number of inputs is equal to the number of random numbers, the system prompts "yes" and sets the flag to false;
If (inputnum = num ){
If (count <= 3 ){
// Output a sentence,
Alert ("You are amazing, you guessed it! ");
}
If (count <= 6 & count> 3 ){
// Output a sentence,
Alert ("You can, but you guessed it! ");
}
If (count <= 10 & count> 6 ){
// Output a sentence,
Alert ("barely, that's right! ");
}
// Set the flag to false.
Falg = false;
}



 
} Else {

// Output a sentence,
Alert ("I'm dizzy. I haven't guessed it for 10 times. Why ?! ");
// Set the flag to false.
Falg = false;
}
 
}

Script




 

If you are interested, study it ~

Author: "liweiw.job column"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.