如何在Java循环中使用int / double

时间:2015-04-14 03:44:22

标签: java loops percentage

public class TaxesV2Wendt {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        final double TAX = .1;
        final double TAX1 = .15;
        final double TAX2 = .25;
        final double TAX3 = .28;
        final double TAX4 = .33;
        final double TAX5 = .35;
        final double TAX6 = .396;

        final int INCOME = 9075;
        final int INCOME1 = 36900;
        final int INCOME2 = 89350;
        final int INCOME3 = 186350;
        final int INCOME4 = 405100;
        final int INCOME5 = 406750;

        for (int i = 0; i < 5; i++) {
            System.out.println("");
            System.out.println("Enter your yearly income");
            double year_Income = in.nextDouble();
            double all = 0;
            if (year_Income <= 9074) {
                System.out.println("Your tax is 10%");
                double taxes = ((double) year_Income * TAX);
                System.out.println("You Owe" + taxes);
                all = +taxes;
            } else if (year_Income >= INCOME && year_Income <= INCOME1) {
                System.out.println("Your tax is 15%");
                double taxes = ((double) year_Income * TAX1);
                System.out.println("You Owe" + taxes);
                all = +taxes;
            } else if (year_Income >= INCOME1 && year_Income <= INCOME2) {
                System.out.println("Your tax is 25%");
                double taxes = ((double) year_Income * TAX2);
                System.out.println("You Owe" + taxes);
                all = +taxes;
            } else if (year_Income >= INCOME2 && year_Income <= INCOME3) {
                System.out.println("Your tax is 28%");
                double taxes = ((double) year_Income * TAX3);
                System.out.println("You Owe" + taxes);
                all = +taxes;
            } else if (year_Income >= INCOME3 && year_Income <= INCOME4) {
                System.out.println("Your tax is 33%");
                double taxes = ((double) year_Income * TAX4);
                System.out.println("You Owe" + taxes);
                all = +taxes;
            } else if (year_Income >= INCOME4 && year_Income <= INCOME5) {
                System.out.println("Your tax is 35%");
                double taxes = ((double) year_Income * TAX5);
                System.out.println("You Owe" + taxes);
                all = +taxes;
            } else {
                System.out.println("Your tax is 39.6");
                double taxes = ((double) year_Income * TAX6);
                System.out.println("You Owe" + taxes);
                all = +taxes;
            }
        }
        System.out.println("Total Taxes");
        System.out.println(all);
    }
}

遇到麻烦请帮忙。需要打印所有变量的结果,但它不会找到变量........尝试做税和它这么容易做这个大声笑。如果可能的话,我的税收到目前为止也是如此????非常感谢。

3 个答案:

答案 0 :(得分:2)

all变量是一个块变量。它不能在if / else块之外看到。 如果要在块外部访问它(但在for循环内),则必须将all变量的声明移动到for循环的范围

e.g。

for(int i = 0; i < 5; i++){
    double all = 0.0;
    ..

但根据你的逻辑,我认为你必须在for循环之外定义它。 (因为您要为all)的现有值增加值

所以它可以是

double all = 0.0;
for(int i = 0; i < 5; i++){
        ..

答案 1 :(得分:1)

在循环中定义all变量。这样你就可以在print语句中使用这个变量。

在您的情况下,修改后的代码将是:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    final double TAX = .1;
    final double TAX1 = .15;
    final double TAX2 = .25;
    final double TAX3 = .28;
    final double TAX4 = .33;
    final double TAX5 = .35;
    final double TAX6 = .396;

    final int INCOME = 9075;
    final int INCOME1 = 36900;
    final int INCOME2 = 89350;
    final int INCOME3 = 186350;
    final int INCOME4 = 405100;
    final int INCOME5 = 406750;

    double all = 0;
    for (int i = 0; i < 5; i++) {
        System.out.println("");
        System.out.println("Enter your yearly income");
        double year_Income = in.nextDouble();

        if (year_Income <= 9074) {
            System.out.println("Your tax is 10%");
            double taxes = ((double) year_Income * TAX);
            System.out.println("You Owe" + taxes);
            all += taxes;
        } else if (year_Income >= INCOME && year_Income <= INCOME1) {
            System.out.println("Your tax is 15%");
            double taxes = ((double) year_Income * TAX1);
            System.out.println("You Owe" + taxes);
            all += taxes;
        } else if (year_Income >= INCOME1 && year_Income <= INCOME2) {
            System.out.println("Your tax is 25%");
            double taxes = ((double) year_Income * TAX2);
            System.out.println("You Owe" + taxes);
            all += taxes;
        } else if (year_Income >= INCOME2 && year_Income <= INCOME3) {
            System.out.println("Your tax is 28%");
            double taxes = ((double) year_Income * TAX3);
            System.out.println("You Owe" + taxes);
            all += taxes;
        } else if (year_Income >= INCOME3 && year_Income <= INCOME4) {
            System.out.println("Your tax is 33%");
            double taxes = ((double) year_Income * TAX4);
            System.out.println("You Owe" + taxes);
            all += taxes;
        } else if (year_Income >= INCOME4 && year_Income <= INCOME5) {
            System.out.println("Your tax is 35%");
            double taxes = ((double) year_Income * TAX5);
            System.out.println("You Owe" + taxes);
            all += taxes;
        } else {
            System.out.println("Your tax is 39.6");
            double taxes = ((double) year_Income * TAX6);
            System.out.println("You Owe" + taxes);
            all += taxes;
        }
    }
    System.out.println("Total Taxes : "+all);

    in.close();
}

答案 2 :(得分:1)

您在大括号内定义所有。一旦你的大括号结束,所有的范围就会结束。所以你的编译器将无法找到 all 。 你必须在for循环之外定义 all 。如果你在循环中定义它,那么它也会在每次循环开始时被覆盖。