Ios randomly selects the option demo in a certain range of options

Source: Internet
Author: User

Demo function: ios randomly selects a demo in a certain range, such as screenshots. When you click a button, the program will randomly select an option based on the range of button options. Iphone 6.1 passed the test.

Demo Description: MainView. m is the main code in the project demo. Use rand () to generate random numbers. Rand () is used to return an integer of the int type at random, ranging from 0 to RAND_MAX.

Demo screenshot:

 

 


Demo main code:

<STRONG xmlns = "http://www.w3.org/1999/xhtml"> // button RESPONSE event-(IBAction) abcd {// radn () generates a random number, modulo 4. Therefore, the result of rNumber can only be 0, 1, 2, or 3. enter int rNumber = rand () % 4; switch (rNumber) {case 0: result. text = @ "A"; break; case 1: result. text = @ "B"; break; case 2: result. text = @ "C"; break; case 3: result. text = @ "D"; break; default: break; }}- (IBAction) agreeDisagree {int rNumber = rand () % 2; switch (rNumber) {case 0: result. text = @ "Agree"; break; case 1: result. text = @ "Disagree"; break; de Fault: break ;}}</STRONG> // The RESPONSE event of the button-(IBAction) abcd {// radn () generates a random number, and the modulo of 4 is used. Therefore, the result of rNumber can only be 0, 1, 2, or 3. enter int rNumber = rand () % 4; switch (rNumber) {case 0: result. text = @ "A"; break; case 1: result. text = @ "B"; break; case 2: result. text = @ "C"; break; case 3: result. text = @ "D"; break; default: break; }}- (IBAction) agreeDisagree {int rNumber = rand () % 2; switch (rNumber) {case 0: result. text = @ "Agree"; break; case 1: result. text = @ "Disagree"; break; default: break ;}}

 

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.