你好我有这个代码,我想在if语句中使用变量“mid”但我不能因为它是私有的,它只能在较小的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 */
}
}
答案 0 :(得分:1)
刚刚初始化mid
。现在问题已修复。
int mid=0
您当前的代码
int mid; // not initialize
int high = Math.max(Math.max(R1,G1),B1);
// rest of the code
当你来else
时,mid
未初始化。
答案 1 :(得分:0)
它不接受它,因为它进入NOONE你的if条件...... 如果你没有初始化它并且乱码中的代码如果没有执行条件,那么它将没有与0不同的值。