while 循环多个条件超出 kotlin 范围
我想创建一个函数来替换字符串句子“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论