while 循环多个条件超出 kotlin 范围

发布于 01-18 11:43 字数 1110 浏览 2 评论 0原文

我想创建一个函数来替换字符串句子“2.x^56+5.x-2.x^3+x^4+2”的特定部分,并且我在 while 循环中遇到了问题,我使用了多个while 循环中的条件,但即使条件匹配它仍然有效,

    fun replacePow() {
    while (newString.contains('^')) {
        for (i in 0 until newString.lastIndex) {
            if (newString[i] == '^') {
                m = i
                while (newString[m] != '.' || newString[m] != '+' || newString[m]!= '-' ) {
                    m -= 1
                }
                if (newString[m] == '.' || newString[m] != '+' || newString[m]!= '-') {
                    previousLim = m
                }

                o = i
                while (newString[o] != '+' || newString[o]!= '-') {
                    o += 1
                }
                if (newString[o] == '+'|| newString[o]== '-') {
                    nextLim = o
                }
                newString = newString.replaceRange(m + 1, o, powred[r].toString())
                r += 1
            }
            if (i == newString.lastIndex){
                break
            }
        }
    }
    println(newString)

}

如果有更好的方法来编写此代码,请将其写在注释中,无论如何谢谢你

I wanna create a function that replaces a specific part of a string sentence "2.x^56+5.x-2.x^3+x^4+2" and I had a problem with the while loop, I used multiple condition in the while loop but it still working even when the condition is match,

    fun replacePow() {
    while (newString.contains('^')) {
        for (i in 0 until newString.lastIndex) {
            if (newString[i] == '^') {
                m = i
                while (newString[m] != '.' || newString[m] != '+' || newString[m]!= '-' ) {
                    m -= 1
                }
                if (newString[m] == '.' || newString[m] != '+' || newString[m]!= '-') {
                    previousLim = m
                }

                o = i
                while (newString[o] != '+' || newString[o]!= '-') {
                    o += 1
                }
                if (newString[o] == '+'|| newString[o]== '-') {
                    nextLim = o
                }
                newString = newString.replaceRange(m + 1, o, powred[r].toString())
                r += 1
            }
            if (i == newString.lastIndex){
                break
            }
        }
    }
    println(newString)

}

if there is a better way to write this code just write it in the comment, And thank you anyway

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文