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

后端 未结 8 1124
半阙折子戏
半阙折子戏 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条回答
  •  隐瞒了意图╮
    2020-11-27 23:48

    Even though you know one of the 3 branches of the comparison with numberOfWords will be visited, the compiler doesn't know that. It will wrongly assume that it is possible to enter the else clause and the textCost variable will remain unitiliazed.

    Similarly with the switch (advPay). Even though you know that one of the two will be visited, the compiler doesn't.

    Suggestion: Remove the else if (numberOfWords > 35) make it just an else.

    As for the switch (advPay), add a default case. Inside you can put a throw new AssertionError();.

提交回复
热议问题