循环回到它不应该的地方。 (迅速)

时间:2016-05-26 17:26:08

标签: swift loops while-loop

我的代码存在问题,当" p"或" q"缺少Xcode一遍又一遍地解决它,我甚至得到了结果,但它一直在做。

enter image description here 我错过了什么?

应用程序的要点是它计算三角形的缺失边。 像这样:

enter image description here

    import UIKit
import Darwin


var i = 0
var ch = ""

var ac = false
var bc = false
var cc = false
var pc = false
var qc = false
var mc = false


var a: Double = 0
var b: Double = 0
var c: Double = 0
var p: Double = 0
var q: Double = 0
var m: Double = 0

a
b = 8
c
p
q = 5
m

if (a > 0) {ac = true}
if (b > 0) {bc = true}
if (c > 0) {cc = true}
if (p > 0) {pc = true}
if (q > 0) {qc = true}
if (m > 0) {mc = true}

//sqrt(p)*b/sqrt(q)

while (ac == false && bc == false && cc == false && pc == false && qc == false && mc == false || i < 20){

//------------------------------------------ a
if (ac == false){
/*
 > a = sqrt(p*c)
 > a = sqrt(c) - sqrt(b)
 > a = sqrt(m) + sqrt(p)
*/
    if(pc == true && cc == true){
        a = sqrt(c*p)
        ac = true
        ch = ch + "a.1; "
    } else if (cc == true && bc == true){
        a = sqrt(c) - sqrt(b)
        ac = true
        ch = ch + "a.2; "
    } else if (mc == true && pc == true){
        a = sqrt(m) + sqrt(p)
        ac = true
        ch = ch + "a.3; "
    }


/*    else if (b>0 && p>0 && q>0){
        a = sqrt(p)*b/sqrt(q)
    }*/
}
//------------------------------------------- b
if (bc == false){
/*
 > b = sqrt(q*c)
 > b = sqrt(c) - sqrt(a)
 > b = sqrt(m) + sqrt(q)
*/
    if (qc == true && cc == true){
        b = sqrt(q * c)
        bc = true
        ch = ch + "b.1; "
    } else if (cc == true && ac == true){
        b = sqrt(c) - sqrt(a)
        bc = true
        ch = ch + "b.2; "
    } else if (cc == true && qc == true){
        b = sqrt(m) + sqrt(q)
        bc = true
        ch = ch + "b.3; "
    }
}
//----------------------------------------------- c
if (cc == false){
/*
 > c = q + p
 > c = sqrt(a) + sqrt(b)
 > c = pow(a, 2) / p
 > c = pow(b, 2) / q
*/
    if (pc == true && qc == true){
        c = p + q
        cc = true
        ch = ch + "c.1; "
    } else if (ac == true && bc == true){
        c = sqrt(a) + sqrt(b)
        cc = true
        ch = ch + "c.2; "
    } else if (ac == true && pc == true){
        c = pow(a, 2) / p
        cc = true
        ch = ch + "c.3; "
    } else if (bc == true && qc == true){
        c = pow(b, 2) / q
        cc = true
        ch = ch + "c.4; "
    }
}
//--------------------------------------------- q
if (qc == false){
/*
 > q = c - p
 > q = sqrt(b) - sqrt(m)
 > q = pow(b, 2) / c
*/
    if (pc == true && cc == true){
        q = c - p
        qc == true
        ch = ch + "q.1; "
        qc == true
    } else if (bc == true && mc == true){
        q = sqrt(b) - sqrt(m)
        qc = true
        ch = ch + "q.2; "
    }else if (bc == true && cc == true){
        q = pow(b, 2) / c
        qc = true
        ch = ch + "q.3; "
    }
}
//------------------------------------------------ p
if (pc == false){
/*
 > p = c - q
 > p = sqrt(a) - sqrt(m)
 > p = pow(a, 2) / c
*/
    if (qc == true && cc == true){
        p = c - q
        pc == true
        ch = ch + "p.1; "
    } else if (ac == true && mc == true){
        p = sqrt(a) - sqrt(m)
        pc == true
        ch = ch + "p.2; "
    } else if (ac == true && cc == true){
        p = pow(a, 2) / c
        pc == true
        ch = ch + "p.3; "
    }
}
//------------------------------------------------- m
if (mc == false){
/*
 > m = p * q
 > m = sqrt(a) - sqrt(p)
 > m = sqrt(b) - sqrt(q)
*/
    if (pc == true && qc == true){
        m = p * q
        mc = true
        ch = ch + "m.1; "
    } else if (ac == true && pc == true){
        m = sqrt(a) - sqrt(p)
        mc = true
        ch = ch + "m.2; "
    } else if (bc == true && qc == true){
        m = sqrt(b) - sqrt(q)
        mc = true
        ch = ch + "m.3; "
    }
}
    i += 1
}
i = 0

if (a+b > c && a+c > b && c+b > a){
    a
    b
    c
    p
    q
    m
    m

}else{
    "incorrect triangle"
}

1 个答案:

答案 0 :(得分:0)

而不是

pc == true
你可能想做

pc = true

您在大多数情况下都是正确完成的,pcqc除外。我不能给你行号,但作为一个例子,这将是你犯这个错误的地方之一:

if (qc == true && cc == true){
    p = c - q
    pc == true         //This line should be pc = true
    ch = ch + "p.1; "
}