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

时间:2017-11-17 03:15:20

标签: java if-statement counter

我正在尝试编写一个计算火车票成本的程序。我目前正在使用为当前成本添加任何折扣的方法,我使用if语句但每次都覆盖我以前的计算。我该如何阻止它?

这是我到目前为止的方法...当前是当前费用,金额是用户想要的门票数量。

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

1 个答案:

答案 0 :(得分:0)

您在每个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;
}