java 中的 if 语句每次都会覆盖我的计数器
if-statement overwriting my counter everytime in java
我正在尝试编写一个程序来计算火车票的费用。我目前正在执行为当前成本添加任何折扣的方法,我正在为其使用 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;
}
我正在尝试编写一个程序来计算火车票的费用。我目前正在执行为当前成本添加任何折扣的方法,我正在为其使用 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;
}