0

私は学校のプロジェクトを行っていますが、このエラーの理由を見つけることができないようです. 私はプログラミングに非常に慣れていないため、助けに感謝しています。前もって感謝します。

import java.util.Scanner;
 public class Lemonade {

public static void main(String[] args) {
    Scanner user = new Scanner(System.in);
    int lemons_per_pitcher = 12;
    int spoons_per_bag = 1000;
    int spoons_per_pitcher = 50;
    System.out.println("Enter the amount of lemons you have.");
    int lemon = user.nextInt();
    System.out.println("Enter the amount of bags of sugar you have.");
    int bags = user.nextInt();
    int spoons = bags * 1000;
    int sugar = spoons / 50;
    int lemons2 = lemon / 12;
    if( lemons2 > sugar){
        int pitcher = lemons2;
    }else{
        int pitcher = sugar;
    }
    if( lemon < 12 || bags < 1){
        System.out.println("You can make a maximum of 0 pitchers");
    } else{
        System.out.println("This is the maximum amount of pitchers you can         make is: " + pitcher);
    }
}

}

4

2 に答える 2

3

pitcherはローカル値なので、メイン メソッドで定義できます。

これを試して:

public static void main(String[] args) {
        int pitcher;
        Scanner user = new Scanner(System.in);
        int lemons_per_pitcher = 12;
        int spoons_per_bag = 1000;
        int spoons_per_pitcher = 50;
        System.out.println("Enter the amount of lemons you have.");
        int lemon = user.nextInt();
        System.out.println("Enter the amount of bags of sugar you have.");
        int bags = user.nextInt();
        int spoons = bags * 1000;
        int sugar = spoons / 50;
        int lemons2 = lemon / 12;
        if (lemons2 > sugar) {
            pitcher = lemons2;
        } else {
            pitcher = sugar;
        }
        if (lemon < 12 || bags < 1) {
            System.out.println("You can make a maximum of 0 pitchers");
        } else {
            System.out
                    .println("This is the maximum amount of pitchers you can         make is: "
                            + pitcher);
        }
    }

変数は、それを定義したブロックでのみ使用できます。

例えば:

{
    int i = 0;
}
i++; // ERROR : There no i in this block

あなたのコードで:

if( lemons2 > sugar){
    int pitcher = lemons2;
}else{
    int pitcher = sugar;
} // pitcher no more exists after block
于 2013-08-15T22:11:10.970 に答える