2016-10-06 81 views
0

我正在处理这组代码,但遇到了“局部变量connat被初始化”的错误。我是在Java中编写代码的新手,我正在尝试学习基础知识。我知道变量需要初始化,但我不知道如何去做。我明白我可以做“int price = 0;”但总是会以$ 0的价格返回。任何帮助将不胜感激。本地变量无法初始化

public static void main(String[] args) { 
Scanner in = new Scanner(System.in); 
int price; 
String customertype; 
double bonus; 
DecimalFormat df = new DecimalFormat("$#,###"); 


System.out.println("Are you a residential (r), commercial (c), Educational (e), or Preferred (p) customer?"); 
customertype = in .next().toLowerCase(); 
System.out.println("Please enter the number of minutes the customer used NkuTel services for the week"); 
double minutes = in .nextInt(); 


//Weekly rate of $5. 10 cents per minute over 60 mins. 
if (minutes <= (0) && (minutes >= 10080)) { 
    System.out.println("Cannot have that amount of minutes. Please try again"); 
    if (customertype.equals("r")) { 

    if (minutes > (60)) price = (int)((5 + .010) - 60); 
    if (minutes <= 60) price = 5; 
    } {} 
    //20 cents per minute for first 300. 15 cents per min after that 
    if (customertype.equals("c")) { 
    if (minutes <= 300) price = (int)(.20 * 300); 
    if (minutes >= 300) price = (int)(minutes * (.15)); 
    if (minutes >= 300) bonus = (price * (-.30)); 
    System.out.println("You get a bonus for being over 300 minutes!"); 
    } 
    //Educational customer charged 18c per min. 
    if (customertype.equals("e")) { 
    price = (int)(.18 * minutes); 
    } 

    if (customertype.equals("p")) 
    if (minutes >= 500) price = (int)((minutes * .04) + 10); 

    if (minutes < 500) price = (int)((minutes * .06) + 10); 

    else 
    System.out.println("Error. Please enter either 'r' or 'c'"); 
} 


/*Preferred customer pays $10 base and 6c per min. 
if <500 then rate is 4c per min. */ 

// else{ 
// System.out.println("Error. Please enter either 'r' or 'c'");} 

System.out.println("Your total minutes is " + minutes + ", your total bill is " + df.format(price)); 
} 
} 
+0

如果'customertype'为'e',则'else'在else块中永远不会被初始化。您必须在所有分支中进行初始化。 – Li357

+0

你在使用什么IDE,给出这样一个严重错误的错误? 'javac'表示*“变量价格可能没有被初始化”*,并且Eclipse说*“局部变量价格可能没有被初始化”*,两者都正确地识别问题,即问题是“有未被初始化“,不像你提到的”无法初始化“。 – Andreas

回答

0

您需要初始化价格,编译器明白你的价格没有被所有的情况(你的条件都满足的情况下,无)初始化,无论是U第初始化它或把一个else和初始化,所以你告诉编译器,您正在初始化它在所有情况下

0

问题是您只设置客户类型为“r”,“c”,“e”或“p”时的价格。对于您的程序,这可能是唯一有效的客户类型。但是编译器不知道 - 如果客户是“z”类型的话?在这种情况下,您无法打印出价格,因为您从未将其设置为任何内容。

要解决此问题,只需声明您的价格从0开始。int price = 0;这不会导致您当前的代码出现问题,因为当客户类型有效时,您将覆盖0值。

更好的解决方案是设置您的代码以处理输入无效客户类型的情况。它看起来像你试图用System.out.println("Error. Please enter either 'r' or 'c'");那样做,但你没有把它做得很对。它可能是这样的:

//changed this line - it should be if minutes less than 0 or greater than 10000, not AND 
if (minutes <= (0) || (minutes >= 10080)) { 
    System.out.println("Cannot have that amount of minutes. Please try again"); 
} 

if (customertype.equals("r")) { 
    if (minutes > (60)) { 
    price = (int)((5 + .010) - 60); 
    }else { 
    price = 5; 
    } 
}else if (customertype.equals("c")) { 
    //20 cents per minute for first 300. 15 cents per min after that 
    if (minutes <= 300) { 
    price = (int)(.20 * 300); 
    } 
    if (minutes >= 300) { 
    price = (int)(minutes * (.15)); 
    bonus = (price * (-.30)); 
    System.out.println("You get a bonus for being over 300 minutes!"); 
    } //fixed this block to include the print only if they actually earned the bonus 
}else if (customertype.equals("e")) { 
    //Educational customer charged 18c per min. 
    price = (int)(.18 * minutes); 
}else if (customertype.equals("p")) 
    if (minutes >= 500) { 
    price = (int)((minutes * .04) + 10); 
    } 
    if (minutes < 500) { 
    price = (int)((minutes * .06) + 10); 
    } 
}else 
    System.out.println("Error. Please enter either 'r' or 'c'"); 
} 

我纠正了一些其他的小错误,但我认为有些可能仍然存在。例如,你计算了超过300分钟的bonus,但是你从来没有做过任何事情。不过,我现在让你处理一些现在我将代码固定到应该可以运行它的地方。

请记住,在if语句块周围使用大括号({})可以更清楚地说明if中包含哪些语句,哪些不是。我强烈建议每个if语句总是使用大括号,尽管您并不总是必须使用大括号,尤其是因为您的原始代码在该区域存在一些混淆。

0

其他答案遇到了问题,“价格”没有被初始化,但由于您是“编写代码的新手”,您应该意识到这些问题可以通过代码的结构以及如何避免结合其他策略。

首先,一定要使用{}各地块,所以这段代码:

if (minutes < 500) price = (int)((minutes * .06) + 10); 

else 
    System.out.println("Error. Please enter either 'r' or 'c'"); 

将成为:

if (minutes < 500) { 
     price = (int)((minutes * .06) + 10); 
    } 
} // From a prior if statement. 
else { 
    System.out.println("Error. Please enter either 'r' or 'c'"); 
} 

一个很好的IDE,比如Eclipse,可以配置为添加那些你。

另一种策略是避免if/else/if“ladder”在一起,并使用开关构造。请注意,在Java开关中使用字符串是一个相对较新的功能。

// Compute price based on customer type 
switch (customertype) { 
    case "e": 
     // Your code here. 
     break; 

    case "c": 
     // Your code here. 
     break; 

    case "r": 
     // Your code here. 
     break; 

    case "p": 
     // Your code here. 
     break; 

    // ALWAYS, ALWAYS, ALWAYS use a default: case in a switch. 
    // ALWAYS. 
    default: 
     System.out.println("Error. Please enter either 'r' or 'c'"); 
     break; 
} 

这个结构使得它非常清楚客户在哪里被处理和变量没有被定义。如果客户类型之间存在共同的处理,那么这可能不是一个好的选择,或者您可以将这些代码放入方法中并根据需要调用它们。