How to avoid 'the local variable may not have been initialized'?

后端 未结 8 1121
半阙折子戏
半阙折子戏 2020-11-27 23:00
/*This is a program that calculates Internet advertising rates based on what features/options you choose.
 * 
 *  
 */

import java.util.Scanner;

public class Inter         


        
8条回答
  •  萌比男神i
    2020-11-27 23:40

    Eclipse is warning you because your initializations are happening inside conditionals. If none of the conditions are satisfied, textCost will be uninitialized.

    if (numberOfWords <= 25)
        {
                //assign a value to textCost
        }
        else if (numberOfWords <= 35)
        {
                //assign a value to textCost
        }
        else if (numberOfWords > 35)
        {
                //assign a value to textCost
        }
    

    Eclipse probably isn't recognizing that (numberOfWords <= 35) and (numberOfWords > 35) cover all possibilities.

    You could either initialize it to 0 on declaration, or include an additional else {} which sets it to zero.

    Similar explanation for the other variable.

提交回复
热议问题