samedi 22 février 2020

How can I use the same randomly generated number twice? - SwiftUI

I'm trying to build a very simple multiplication practice app where the user picks the multiplication table they want to practice and the app poses multiplication problems on that table using a randomly generated integer between 1 and 20. I then want to give the user the answer.

The problem that I'm having is that the randomly generated number changes between my code for showing the question and my code for showing the answer. I'm sure there's a simple solution to this, but I'm not seeing it.

How do I store a randomly generated number for use in two places in my code? Alternatively, how do I selectively stop the randomization from happening?

Here's my current code:

    struct test: View {

  @State private var multiplicationTable = 1

  var body: some View {
    VStack {
      Spacer()
      Text("Pick the multiplication table you'd like to practice!")
        .bold()
        .fontWeight(.bold)
        .font(.title)

        .multilineTextAlignment(.center)
      Stepper(value: $multiplicationTable, in: 1...12, step: 1) {
        Text("\(multiplicationTable)'s")
      }
      Text("Let's go!")
      Spacer()
      Text("Question #1: \(numberForText(multiplier1: multiplicationTable).text1)")
      Text("Answer: \(numberForText(multiplier1: multiplicationTable).text2)")
      Spacer()
    }
    .padding(.leading, 20)
    .padding(.trailing, 20)
  }

  func numberForText(multiplier1: Int) -> (text1: String, text2: String) {
    let multiplier2 = Int.random(in: 1..<21)
    return ("What is \(multiplier1) times \(multiplier2)?", "\(multiplier1) times \(multiplier2) = \(multiplier1 * multiplier2)")
  }
}

Thank you.




Aucun commentaire:

Enregistrer un commentaire