本文介绍了制作四个独立的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建四个不同的变量,因此 a 和 b 不同,a 和 c 不同,b 和 c 不同等等.这是四个变量.

I am trying to make four separate variables that are all different so that a and b are different, a and c are different, b and c are different and so on so forth. Here are the four variables.

var a = Int(arc4random_uniform(5))
var b = Int(arc4random_uniform(5))
var c = Int(arc4random_uniform(5))
var d = Int(arc4random_uniform(5))

推荐答案

let a = Int(arc4random_uniform(5))
var b = Int(arc4random_uniform(5))
while b == a {
    b = Int(arc4random_uniform(5))
}
var c = Int(arc4random_uniform(5))
while c == a || c == b {
    c = Int(arc4random_uniform(5))
}
var d = Int(arc4random_uniform(5))
while d == c || d == b || d == a {
    d = Int(arc4random_uniform(5))
}
println(a)  // 2
println(b)  // 0
println(c)  // 4
println(d)  // 3

这篇关于制作四个独立的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 08:31