このコードセットを処理していますが、「ローカル変数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));
}
}
'customertype'が' e'の場合、 'price'はelseブロックで決して初期化されません。すべてのブランチで初期化する必要があります。 – Li357
このような間違った名前のエラーを表示するには、どのIDEを使用していますか? 'javac'は*"可変価格は初期化されていないかもしれません "*とEclipseは言う*"ローカル変数の価格は初期化されていないかもしれません "*、両方とも問題を正しく識別します。初期化されていません "、"初期化できません "とは異なります。 – Andreas