简体   繁体   English

如何在if语句中声明值? (在Java中)

[英]How can i declare a value in an if statement? (In java)

So this is what i have so far and i have no clue why the program is not responding the way i want it to. 因此,这就是我到目前为止所掌握的信息,我不知道为什么该程序没有按照我想要的方式进行响应。 Keeps showing up that "avg2 might not have been initialized". 不断显示“ avg2可能尚未初始化”。 Any ideas?? 有任何想法吗??

if (a < b && a < c) {
    System.out.println("The lowest score was: " + a);
    System.out.println("The average without the lowest score is: " + ((b + c) / 2));
    avg2 = ((b + c) / 2);
}

if (b < a && b < c) {
    System.out.println("The lowest score was: " + b);
    System.out.println("The average without the lowest score is: " + ((a + c) / 2));
    avg2 = ((a + c) / 2);
}

if (c < a && c < b) {
    System.out.println("The lowest score was: " + c);
    System.out.println("The average without the lowest score is: " + ((a + b) / 2));
    avg2 = ((a + b) / 2);
}

I suppose you have declared avg2 like this: double avg2; 我想您已经这样声明avg2: double avg2; with no initial value. 没有初始值。 The problem is that if a == b == c for example, none of your if conditions will be true and avg2 will not be initialised. 问题是,例如,如果a == b == c ,则if条件都不成立,并且avg2不会被初始化。

  • Solution 1: initialise avg2: double avg2 = 0; 解决方案1:初始化avg2: double avg2 = 0;
  • Solution 2 (better): instead of a succession of ifs, use an if / else if / else syntax. 解决方案2(更好):使用if / else if / else语法代替连续的ifs。 If there is an else, the compiler will be satisfied that avg2 will always be initialised. 如果还有其他情况,编译器将对avg2始终被初始化感到满意。

The problem is that it appears to the compiler as though there is a possible route through the code in which none of the if statement conditions are met. 问题在于,对于编译器而言,好像有一条遍历代码的可能途径,其中没有满足if语句的条件。 If that is the case, then no assignment will ever be made to avg2 . 如果是这种情况,那么将不会对avg2进行任何分配。

Because compiler cannot know that one of the if conditions will evaluate to true and therefor avg2 would have been assigned. 因为编译器无法知道if条件之一将评估为true,因此将分配avg2 One work around is to put an unconditional else : 一种解决方法是无条件地设置else

if (c < a && c < b) {
 ....
} else {
   throw new RuntimeException("Cannot be here");
}

// now use can use avg2 here

To fix the 'not initialized' error you should rearrange your 3 if statements into one if-else block. 要解决“未初始化”错误,您应该将3个if语句重新排列到一个if-else块中。 The compiler isn't going to test whether the condition for an if statement will be true or not, but if you assign your variable inside each part of the if-else block (which must include the "if all else fails" else), it would be inevitably initialized. 编译器不会测试if语句的条件是否成立,但是如果您在if-else块的每个部分(必须包含“如果所有其他方法都失败了” else中)中分配变量,它将不可避免地被初始化。

double avg2;
if (a < b && a < c)
{
    System.out.println("The lowest score was: " + a);
    System.out.println("The average without the lowest score is: " + ((b + c) / 2));
    avg2 = ((b + c) / 2);
}else if (b < a && b < c)
{
    System.out.println("The lowest score was: " + b);
    System.out.println("The average without the lowest score is: " + ((a + c) / 2));
    avg2 = ((a + c) / 2);
}else {
    System.out.println("The lowest score was: " + c);
    System.out.println("The average without the lowest score is: " + ((a + b) /2));
    avg2 = ((a + b) / 2);
}

A better way to perform this procedure: 执行此过程的更好方法:

double avg2;
double lowest = a;
if(lowest > b)
    lowest = b;
if(lowest > c)
    lowest = c;
System.out.println("The lowest score was: " + lowest);
avg2 = (a + b + c - lowest)/2;
System.out.println("The average without the lowest score is: " + avg2);

That code has hurt my eyes so much, that I felt obliged to post this answer :) 该代码严重伤害了我的眼睛,以至于我不得不发布此答案:)

double lowest = Math.min(Math.min(a, b), c);
double avg2 = (a + b + c - lowest) / 2;
System.out.println("The lowest score was: " + lowest);
System.out.println("The average without the lowest score is: " + avg2);

Just 4 lines instead of 15, and this code will not generate that warning because variables are being initialized. 只有4行而不是15行,并且此代码将不会生成该警告,因为正在初始化变量。

Probably the warning is because the variable is being initialized inside an if statement, which eventually may not occur... 警告可能是因为该变量正在if语句中初始化,最终可能不会发生...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM