条件分岐

diceが1だった場合だけ「OK牧場!!」を表示

[php]
-(void)viewDidAppear:(BOOL)animated{
srand((unsigned int)time(NULL));

int dice = rand() % 6 + 1;
if(dice == 1){
[self showString:@"OK牧場!!"];
}
}
[/php]

diceが1か6のときに「OK牧場!!」を表示

[php]
-(void)viewDidAppear:(BOOL)animated{
srand((unsigned int)time(NULL));

int dice = rand() % 6 + 1;
if(dice == 1 || dice == 6){
[self showString:@"OK牧場!!"];
}
}
[/php]

dice1とdice2両方が“1”ときに「OK牧場!!」を表示

[php]
-(void)viewDidAppear:(BOOL)animated{
srand((unsigned int)time(NULL));

int dice1 = rand() % 6 + 1;
int dice2 = rand() % 6 + 1;
if(dice1 == 1 && dice2 == 1){
[self showString:@"OK牧場!!"];
}
}
[/php]

dice1とdice2両方が“1”かdice1とdice2両方が“6”ときに「OK牧場!!」を表示

[php]
-(void)viewDidAppear:(BOOL)animated{
srand((unsigned int)time(NULL));

int dice1 = rand() % 6 + 1;
int dice2 = rand() % 6 + 1;
if(dice1 == 1 && dice2 == 1 || dice1 == 6 && dice2 == 6){
[self showString:@"OK牧場!!"];
}
}
[/php]

dice1が“1 か 6”で、dice2が“6”ときに「OK牧場!!」を表示
※カッコ()が優先になる

[php]
-(void)viewDidAppear:(BOOL)animated{
srand((unsigned int)time(NULL));

int dice1 = rand() % 6 + 1;
int dice2 = rand() % 6 + 1;
if((dice1 == 1 || dice1 == 6) && dice2 == 6){
[self showString:@"OK牧場!!"];
}
}
[/php]

条件が満たされないときの処理を実行する

dice1が1のとき”OK牧場!!” そうでなければ”NOだぜ”

[php]
-(void)viewDidAppear:(BOOL)animated{
srand((unsigned int)time(NULL));

int dice1 = rand() % 6 + 1;
int dice2 = rand() % 6 + 1;
if(dice1 == 1){
[self showString:@"OK牧場!!"];
} else {
[self showString:@"NOだぜ"];
}
}
[/php]

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です