gpt4 book ai didi

java - 我需要在 if 语句之外使用 if 语句的输出,但出现错误

转载 作者:太空宇宙 更新时间:2023-11-04 06:31:33 27 4
gpt4 key购买 nike

你好,我有这段代码,我想在 if 语句中使用变量“mid”,但我不能,因为它是私有(private)的,它只能在较小的 if 语句中使用。

    int mid;
int high = Math.max(Math.max(R1, G1), B1);
int low = Math.min(Math.min(R1, G1), B1);
if (high == R1) {
if (low == G1)
mid = B1;
else
mid = G1;
System.out.println("mid is: " + mid);
} else if (high == G1) {
if (low == R1)
mid = B1;
else
mid = R1;
System.out.println("mid is: " + mid);
} else if (high == B1) {
if (low == G1)
mid = R1;
else
mid = G1;
System.out.println("mid is: " + mid);
}
System.out.println("High is: " + high);
System.out.println("low is: " + low);
int v = high;
int s;
int h;
if (v == 0) {
s = 0;
h = 0;
} else {
s = (high - low) / high;
if (s == 0) {
h = 0;
} else {
double alpha;
alpha = 60 * (mid - low) / (high - low);
/* problem here in mid variable above, it's not taking the output from if statements */
}
}

最佳答案

只需初始化mid。现在问题已解决。

 int mid=0

您当前的代码

 int mid; // not initialize 
int high = Math.max(Math.max(R1,G1),B1);
// rest of the code

当您到达最后一个else时,您的mid尚未初始化。

关于java - 我需要在 if 语句之外使用 if 语句的输出,但出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26057568/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com