[英]how can I assign the same random number to 2 variables in swift?
I am building an app that the user selects a multiplication table.我正在构建一个用户选择乘法表的应用程序。 Then it gives random numbers to multiplicate with the number they select.然后它给出随机数乘以它们的数字 select。 for example, if the user selects "1".例如,如果用户选择“1”。 the questions shown would be "1 x 1", or "1 x 8".显示的问题将是“1 x 1”或“1 x 8”。 The problem is that I need to assign the same random number to 2 variables.问题是我需要为 2 个变量分配相同的随机数。 The one that will be shown on the question and the one used to calculate the result.将在问题上显示的一个和用于计算结果的一个。 I thought of something like this, but the random number is different on each variable.我想到了这样的事情,但是每个变量的随机数都不同。 What can I do to use the same random number generated on 2 variables?如何使用在 2 个变量上生成的相同随机数?
func game() -> (String, Int) {
let randomNumber = multiplicate.randomElement()
switch selectedQuestion {
case 0:
return ("1 × \(randomNumber!)", 1 * randomNumber!)
default:
return ("", 0)
}
}
Not exactly sure what you're asking but you could do something like this:不完全确定你在问什么,但你可以做这样的事情:
let number = Int.random(in: 0..<10)
let number2 = number
but I don't think there is a need to create a new variable for this.但我认为没有必要为此创建一个新变量。 The whole idea of variables is that you can save some value and then use it later so there isn't really a need to create number2
here.变量的整个想法是您可以保存一些值,然后在以后使用它,因此实际上不需要在这里创建number2
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.