改述:我有一个字符串“1234”和四个整数:dif1,dif2,dif3和dif4。我想要实现的是使用其他字符更改字符串中的字符,具体取决于以下代码:
var string = "1234"
var dif1: Int = it1 - it5
var dif2: Int = it2 - it6
var dif3: Int = it3 - it7
var dif4: Int = it4 - it8
// Negative values are not allowed
if dif1 < 0 {
dif1 = -dif1;
}
if dif2 < 0 {
dif2 = -dif2;
}
if dif3 < 0 {
dif3 = -dif3;
}
if dif4 < 0 {
dif4 = -dif4;
}
var change = 0
var minimum = min(dif1,dif2,dif3,dif4)
// The first change that can happen for first character
if dif1 < 13 && dif1 == minimum {
change = 1
// change the first character in string with another one depending on case!
}
else if dif2 < 13 && dif2 == minimum {
change = 1
...
}
else if dif3 < 13 && dif3 == minimum {
change = 1
...
}
else if dif4 < 13 && dif4 == minimum {
change = 1
...
}
// The second change
if (dif1 + dif2) < 13 {
// If we have one change replace the second character
if (change == 1) {
...
}
// else replace the first and record this as one change
change = 2;
}
else if ((dif2 + dif3) < 13) {
if (change == 1) {
}
change = 2;
}
else if ((dif3 + dif4) < 13) {
if (change == 1) {
}
change = 2;
}
else if ((dif1 + dif4) < 13) {
if (change == 1) {
}
change = 2;
}
// The third change
if ((dif1 + dif2 + dif3) < 13) {
// If we have the first change replace the second character
if (change == 1) {
}
// If we also have the second change replace the third character
else if (change == 2) {
}
// Else replace the first character and record as one change
change = 3;
}
else if ((dif2 + dif3 + dif4) < 13) {
if (change == 1) {
}
else if (change == 2) {
}
change = 3;
}
else if ((dif3 + dif4 + dif1) < 13) {
if (change == 1) {
}
else if (change == 2) {
}
change = 3;
}
else if ((dif4 + dif1 + dif2) < 13) {
if (change == 1) {
}
else if (change == 2) {
}
change = 3;
}
if dif1 < 13 && dif1 != minimum {
if (change == 1) {
}
else if (change == 2) {
}
else if (change == 3) {
}
change = 4;
}
else if dif2 < 13 && dif2 != minimum {
if (change == 1) {
}
else if (change == 2) {
}
else if (change == 3) {
}
change = 4;
}
else if dif3 < 13 && dif3 != minimum {
if (change == 1) {
}
else if (change == 2) {
}
else if (change == 3) {
}
change = 4;
}
else if dif4 < 13 && dif4 != minimum {
if (change == 1) {
}
else if (change == 2) {
}
else if (change == 3) {
}
change = 4;
}
那么我怎么能根据上面的代码修改字符串四次呢?
答案 0 :(得分:0)
奇怪,您在这里尝试做的事情,但请查看以下比较功能与您需要的示例。 在游乐场:
var dif1: Float = 52
var dif2: Float = 41.24
var dif3: Float = 141.1
var dif4: Float = 14.124
let allValues = [dif1, dif2, dif3, dif4]
func isDifferent(num: Float, value: Float, values: [Float]?, minimalCompareValue: Bool = true) -> Bool {
if num > value {
if values != nil && !values!.isEmpty {
var minValue = values![0]
let counts = values!.count
for value in 1..<counts {
minValue = min(minValue, values![value])
}
return (value != minValue) == minimalCompareValue
}
return true
}
return false
}
println(isDifferent(53, dif1, allValues)) // true