dimanche 29 septembre 2019

Rolling a dice in swift using a while function and printing results until the result is 1

I'm really new to Swift and I have a task asking me to create a while loop that simulates rolling a 6-sided dice repeatedly until a 1 is rolled. After each roll, print the value.

In just about every iteration I've tried over the last 2 hours I keep ending in an infinite loop that explodes Xcode.

Any help would be fantastic!

var dieRoll = Int.random(in: 1...6)

while dieRoll <= 6 { print (dieRoll) if dieRoll == 1 { print ("You win!") } }

This is about the best one I've done because when it manages to hit a one on the first roll it prints you win, otherwise if it rolls any other number first it keeps that number and crashes.




Aucun commentaire:

Enregistrer un commentaire