return (this.mostSigBits < val.mostSigBits ? -1 : (this.mostSigBits > val.mostSigBits ? 1 : (this.leastSigBits < val.leastSigBits ? -1 : (this.leastSigBits > val.leastSigBits ? 1 : 0))));将这个嵌套的三元运算提取到一个独立的语句中。
时间: 2024-02-10 20:14:40 浏览: 23
好的,这个嵌套的三元运算可以拆分成以下独立语句:
```
int result = 0;
if (this.mostSigBits < val.mostSigBits) {
result = -1;
} else if (this.mostSigBits > val.mostSigBits) {
result = 1;
} else {
result = this.leastSigBits < val.leastSigBits ? -1 : (this.leastSigBits > val.leastSigBits ? 1 : 0);
}
return result;
```
这样就可以更加清晰地看到代码的执行流程。
相关问题
return (this.mostSigBits < val.mostSigBits ? -1 : (this.mostSigBits > val.mostSigBits ? 1 : (this.leastSigBits < val.leastSigBits ? -1 : (this.leastSigBits > val.leastSigBits ? 1 : 0)))); 把这段代码拆开到独立的语句中
Sure! 这段代码可以拆分成以下独立语句:
```
if (this.mostSigBits < val.mostSigBits) {
return -1;
} else if (this.mostSigBits > val.mostSigBits) {
return 1;
} else if (this.leastSigBits < val.leastSigBits) {
return -1;
} else if (this.leastSigBits > val.leastSigBits) {
return 1;
} else {
return 0;
}
```
Line 3: Char 26: error: none of the following functions can be called with the arguments supplied: public infix fun <T, R> Array<out String>.zip(other: Array<out ???>): List<Pair<String, ???>> defined in kotlin.collections public infix fun <T, R> Array<out String>.zip(other: Iterable<???>): List<Pair<String, ???>> defined in kotlin.collections val pairs = names.zip(heights) // 合并为 Pair 类型的数组 ^ Line 4: Char 34: error: unresolved reference: it pairs.sortByDescending { it.second } // 按照身高降序排序 ^ Line 5: Char 28: error: unresolved reference: it return pairs.map { it.first }.toTypedArray() // 提取排序后的 names
这个错误是因为在第三行中,你尝试将一个字符串数组(names)与一个未知类型的数组(heights)合并成一个Pair类型的数组。你需要将heights的类型指定为与names相同的字符串数组类型。
修改第三行为:
```
val pairs = names.zip(heights as Array<out String>) // 将 heights 转换为 String 数组类型
```
第四行和第五行中的错误是因为你引用了一个未定义的变量it。你需要将it替换为lambda表达式参数中的参数名。
修改第四行为:
```
pairs.sortByDescending { pair -> pair.second } // 使用参数名 pair 替换 it
```
修改第五行为:
```
return pairs.map { pair -> pair.first }.toTypedArray() // 使用参数名 pair 替换 it
```
完整的代码如下:
```kotlin
fun sortByHeight(names: Array<String>, heights: Array<Int>): Array<String> {
val pairs = names.zip(heights as Array<out String>)
pairs.sortByDescending { pair -> pair.second }
return pairs.map { pair -> pair.first }.toTypedArray()
}
```