C pointer principle (71)-C Quick Start (5)

Source: Internet
Author: User

(5) repeatedly accept player input, only until the number is guessed

The C program uses the else if statement block to implement a set of conditional statement blocks with multiple different conditional statements. Its usage is shown as follows:

If (condition 1 ){

.........

}

Else if (condition 2 ){

..........

}

Else if (Condition 3 ){

.........

}

......

......

Else if (condition n ){

.........

}

Else {

.........

}

All contents of the good AI Park blog is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/

This statement block structure is used to implement this step.

# Include

# Include

# Include

Int getnumber (){

Srand (int) time (0 ));

Return rand () % 499 + 1;

}

Int main (){

Int mynum;

Int ispass = 0;

Int guessnum = getnumber ();

While (1 ){

While (1 ){

Printf ("hello, please enter a number :");

Scanf ("% d", & mynum );

If (mynum> 500 | mynum <1 ){

Printf ("numbers are limited to \ n between 1-500 ");

}

Else {

Printf ("\ n the number you entered is: % d \ n", mynum );

Break;

}

}

If (mynum> guessnum ){

Printf ("the number is big! \ N ");

}

Else if (mynum

Printf ("the number is small! \ N ");

}

Else {

Printf ("Congratulations, you guessed it! \ N ");

Break;

}

}

}

Dp @ dp :~ /Cquick % gcc guessnum. c-o myguess

Dp @ dp :~ /Cquick %./myguess

Hello, please enter a number: 55

The number you entered is: 55

The number is small!

Hello, please enter a number: 280

The number you entered is: 280

The number is small!

Hello, please enter a number: 350

The number you entered is: 350

The number is small!

Hello, please enter a number: 400

The number you entered is: 400

The number is small!

Hello, please enter a number: 488

The number you entered is: 488

The number is big!

Hello, please enter a number: 420

The number you entered is: 420

The number is small!

Hello, please enter a number: 450

The number you entered is: 450

The number is big!

Hello, please enter a number: 440

The number you entered is: 440

The number is big!

Hello, please enter a number: 430

The number you entered is: 430

Congratulations! You guessed it!

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.