2016-10-06 5 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'の場合、 'price'はelseブロックで決して初期化されません。すべてのブランチで初期化する必要があります。 – Li357

+0

このような間違った名前のエラーを表示するには、どのIDEを使用していますか? 'javac'は*"可変価格は初期化されていないかもしれません "*とEclipseは言う*"ローカル変数の価格は初期化されていないかもしれません "*、両方とも問題を正しく識別します。初期化されていません "、"初期化できません "とは異なります。 – Andreas

答えて

0

、コンパイラはどちらか、あなたの価格は(場合には、あなたの条件のいずれも真でない)すべての場合に初期化されていないことを理解しているuがそれを最初に初期化するか、他を入れて、あなたので、それを初期化しますコンパイラーにすべてのケースで初期化していることを伝えてください

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'"); 
} 

、Eclipseなどの優れたIDE、あなたのためにそれらを追加するように構成することができます。

もう一つの戦略は、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; 
} 

この構成では、顧客の処理場所と定義されていない変数が明確になります。顧客のタイプ間で共通の処理がある場合、これは適切な選択ではないかもしれません。あるいは、そのようなコードをメソッドに入れ、必要に応じて呼び出すことができます。

関連する問題