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; } }