ios 在一定选项范围随机选取选项demo
demo功能:ios 在一定范围随机选取demo,如截屏。在点击按钮的时候,程序会根据按钮选项的范围随机抽取一个选项显示。iphone 6.1测试通过。
demo说明:项目demo中 MainView.m里是主要代码。利用rand()来产生随机数。rand()的作用是随机返回一个类型为int的整数,其范围是0到RAND_MAX。
demo截屏:
demo主要代码:
<STRONG xmlns="http://www.w3.org/1999/xhtml">//按钮响应事件 - (IBAction)abcd { //radn()产生随机数,取4的模。所以 rNumber的结果只可能是0,或1,或2,或3 根据结果将选项信息输入 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; } }</STRONG> //按钮响应事件 - (IBAction)abcd { //radn()产生随机数,取4的模。所以 rNumber的结果只可能是0,或1,或2,或3 根据结果将选项信息输入 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; } }
补充:移动开发 , IOS ,