在java中,if语句每次都覆盖我的计数器

在java中,if语句每次都覆盖我的计数器,java,if-statement,counter,Java,If Statement,Counter,我想写一个程序来计算火车票的价格。我现在使用的方法是在当前成本的基础上增加任何折扣,我使用if语句,但每次它都会覆盖我以前的计算。我该怎么阻止这一切 这是我到目前为止的方法。。。current是当前成本,amount是用户想要的票证数量 public static double calcDiscounts(double a, double[] b) { double counter = 0; double current = b[1]; double amount =

我想写一个程序来计算火车票的价格。我现在使用的方法是在当前成本的基础上增加任何折扣,我使用if语句,但每次它都会覆盖我以前的计算。我该怎么阻止这一切

这是我到目前为止的方法。。。current是当前成本,amount是用户想要的票证数量

public static double calcDiscounts(double a, double[] b) {

    double counter = 0;
    double current = b[1];
    double amount = b[0];

        System.out.println("You may be valid for discounts. Please answer these questions :"
                + "\nDo you have any under 10's travelling with you?\nType 1 for Yes\nType 2 for no");
        if (input.nextInt() == 1) {
            System.out.println("How many under 10's will be travelling with you?");
            int under = input.nextInt();
            if (under > amount - 1) {
                System.out.println("Invalid input - there must be at least one adult travelling alongside.");
            }
            double temp = a * under;
            counter += current - temp;


            System.out.println("The cost of your ticket(s) is now " + counter);
        }
        else {
            counter  = current;
        }

        System.out.println("Is anybody travelling with you under 16?\nType 1 for Yes\nType 2 for no");
        if (input.nextInt() == 1) {
            System.out.println("How many under 16's will be travelling with you?");
            int sixteen = input.nextInt();
            if (sixteen > amount) {
                System.out.println("Invalid input - you don't have this many tickets.");
            } else {
                double temp = a * 0.50;
                double next = temp * sixteen;
                counter = current - next;
                System.out.println("The cost of your ticket(s) is now " + counter);
            }
        }
        else {
            counter  = current;
        }

        System.out.println("Is anybody travel a student?\nType 1 for Yes\nType 2 for no");
        if (input.nextInt() == 1) {
            System.out.println("How many students will be travelling?");
            int student = input.nextInt();
            if (student > amount) {
                System.out.println("Invalid input - you don't have this many tickets.");
            } else {
                double temp = a * 0.25;
                double next = temp * student;
                counter = current - next;

                System.out.println("The cost of your ticket(s) is now " + counter);
            }
        }
        else {
            counter  = current;
        }

        System.out.println("Is anybody travelling over 60?\nType 1 for Yes\nType 2 for no");
        if (input.nextInt() == 1) {
            System.out.println("How many over 60's will be travelling?");
            int sixty = input.nextInt();
            if (sixty > amount) {
                System.out.println("Invalid input - you don't have this many tickets.");
            } else {
                double temp = a * 0.60;
                double next = temp * sixty;
                counter = current - next;

                System.out.println("The cost of your ticket(s) is now " + counter);
            }
        }
        else {
            counter  = current;
        }
    return counter;
}

您将覆盖每个
if else
子句中计数器的值,而且也不需要使用
counter
,因为
current
就足够了

考虑以下代码:

public static double calcDiscounts(double a, double[] b) {

    double current = b[1];
    double amount = b[0];

    Scanner input = new Scanner(System.in);
    System.out.println("You may be valid for discounts. Please answer these questions :"
            + "\nDo you have any under 10's travelling with you?\nType 1 for Yes\nType 2 for no");
    if (input.nextInt() == 1) {
        System.out.println("How many under 10's will be travelling with you?");
        int under = input.nextInt();
        if (under > amount - 1) {
            System.out.println("Invalid input - there must be at least one adult travelling alongside.");
        }
        double temp = a * under;
        current -= temp;

        System.out.println("The cost of your ticket(s) is now " + current);
    }

    System.out.println("Is anybody travelling with you under 16?\nType 1 for Yes\nType 2 for no");
    if (input.nextInt() == 1) {
        System.out.println("How many under 16's will be travelling with you?");
        int sixteen = input.nextInt();
        if (sixteen > amount) {
            System.out.println("Invalid input - you don't have this many tickets.");
        } else {
            double temp = a * 0.50;
            double next = temp * sixteen;
            current -=  next;
            System.out.println("The cost of your ticket(s) is now " + current);
        }
    }

    System.out.println("Is anybody travel a student?\nType 1 for Yes\nType 2 for no");
    if (input.nextInt() == 1) {
        System.out.println("How many students will be travelling?");
        int student = input.nextInt();
        if (student > amount) {
            System.out.println("Invalid input - you don't have this many tickets.");
        } else {
            double temp = a * 0.25;
            double next = temp * student;
            current -= next;

            System.out.println("The cost of your ticket(s) is now " + current);
        }
    }

    System.out.println("Is anybody travelling over 60?\nType 1 for Yes\nType 2 for no");
    if (input.nextInt() == 1) {
        System.out.println("How many over 60's will be travelling?");
        int sixty = input.nextInt();
        if (sixty > amount) {
            System.out.println("Invalid input - you don't have this many tickets.");
        } else {
            double temp = a * 0.60;
            double next = temp * sixty;
            current -= next;

            System.out.println("The cost of your ticket(s) is now " + current);
        }
    }
    return current;
}