无法初始化局部变量

时间:2016-10-06 19:24:45

标签: java

我正在处理这组代码,但遇到了“本地变量connat be initialized”错误。我是用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));
}
}

3 个答案:

答案 0 :(得分:0)

你需要初始化价格,编译器理解你的价格在所有情况下都没有被初始化(如果没有你的条件是真的),你要先将它初始化或者放一个else并初始化它,这样你就告诉编译器你在所有情况下都在初始化

答案 1 :(得分:0)

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

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

更好的解决方案是设置代码以处理输入无效客户类型的情况。看起来你试图用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'");
 }

我纠正了其他一些小错误,但我认为有些可能仍然存在。例如,您计算bonus超过300分钟,但您从未对其进行任何操作。但是,我会让你处理其中的一些,因为我已经将代码修改到应该能够运行它的位置。

请记住,在if语句块周围使用大括号({})可以更清楚地说明if中包含哪些语句以及哪些语句不包括在内。我强烈建议每个if语句都使用大括号,即使你并不总是这样,特别是因为你原来的代码在那个区域有些混乱。

答案 2 :(得分: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)来为您添加这些IDE。

另一种策略是将if / else / if“梯形图”全部放在一起,并使用switch构造。请注意,在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;
}

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