如何在 Java 的循环内外使用 int/double

How to use a int/double in and out of a loop for Java

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

遇到问题请帮忙。需要打印所有变量的结果,但它不会找到变量........尝试纳税并且这样做更容易大声笑。如果可能的话,到目前为止我的税收是否合适????所以很困惑谢谢。

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

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

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();
}

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

的范围

例如

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

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

所以可以

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

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