编程的新手。我希望用户在60岁以上(60岁以上)时享受10%的折扣,在55岁以上且等于60岁之间(60 <= age> 55)时获得5%的折扣。我知道我的代码是完全错误的,但是如果可能,我想逐步解决此问题。
import java.util.*;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int price, age;
double tax, payAmount;
double discountRate_56_to_60 = 0.05;
double discountRate_60_above = 0.1;
payAmount = price * tax;
System.out.print("Price?");
price = input.nextInt();
System.out.print("Tax(%)?");
tax = input.nextDouble();
System.out.print("Age?");
age = input.nextInt();
System.out.println("You pay: $");
payAmount = input.nextDouble();
if (age > 55) {
payAmount;
}
else if (age >= 60) {
payAmount;
}
}
}
答案 0 :(得分:4)
您犯了一些错误:
payAmount = price * tax;
行执行得为时过早。在从用户那里获得价格和税款之前,如何计算支付金额?
行payAmount = input.nextDouble();
不应存在。问题应该是输出payAmount
,而不是要求输入。
payAmount
不是语句。
您的if语句似乎是错误的。如果年龄不大于55岁,则永远不能大于或等于60岁。
这是固定代码,更正写在注释中:
Scanner input = new Scanner(System.in);
int price, age;
double tax, payAmount;
double discountRate_56_to_60 = 0.05;
double discountRate_60_above = 0.1;
System.out.print("Price?");
price = input.nextInt();
System.out.print("Tax(%)?");
tax = input.nextDouble();
payAmount = price * tax; // I moved the line here!
System.out.print("Age?");
age = input.nextInt();
// removed the line asking for pay amount
if (age > 60) { // first we check if age is greater than 60. If it is not, then we check if it is greater than 55.
// with a little analysis you will see that this is equivalent to the stated conditions
payAmount -= payAmount * discountRate_60_above; // calculate the payAmount by subtracting the pay amount times discount rate
}
else if (age > 55) {
payAmount -= payAmount * discountRate_56_to_60;
}
System.out.println("You pay: $" + payAmount); // finally output the payAmount
答案 1 :(得分:2)
尝试以下代码
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int price, age;
double payAmount = 0;
System.out.print("Price?");
price = input.nextInt();
System.out.print("Age?");
age = input.nextInt();
if (age > 55 && age <= 60) {
payAmount = price - price * 5 / 100;
} else if (age > 60) {
payAmount = price - price * 10 / 100;
}
System.out.println("You pay: $ " + payAmount);
input.close();
}
答案 2 :(得分:1)
存在多个问题,第一个问题是第二个if语句else if (age >= 60)
从未达到,因为如果年龄超过60岁,该人也将超过55岁。要解决此问题,您应该更改这样的语句:
if(age >+ 60){
// do something
} else if(age >= 55){
// do something different
}
然后,您尝试在初始化价格和税款之前初始化payAmount。您应该在用户输入年龄和价格等之后进行计算。
// User enters all the stuff
payAmount = price * tax;
然后您可以在if语句中应用正确的折扣率。
如果我愿意,我会将整数price
声明为Double,因为价格可能是16.50美元。
此外,您可以检查输入的数字是整数还是双精度数,以避免出现异常。
答案 3 :(得分:1)
对于当前的布尔语句,您应该将else调整为(年龄> = 60)为(年龄> 60),因为正如您所说,您希望在55岁以下(含60岁)享受5%的折扣。此外,您应该先声明(年龄> 60),否则将不会执行(年龄> 50)的检查。
现在您已宣布56至60和60及以上的折扣率,您可以退货
payAmount * specified discount rate,
对于56到60岁的年龄,您的代码如下
payAmount * double discountRate_56_to_60.
但是,就像一般样式的注释一样,除非您在语句之外再次使用折扣率,否则无需在if语句之外声明折扣率。实际上,您只需声明
if (age > 55) {
return payAmount * .05;
}
这将导致代码看起来更简洁。希望这会有所帮助!
答案 4 :(得分:1)
您的代码中有很多错误
您无法在从用户那里获得输入值之前计算payAmount
,因此将该语句移到所有输入语句之后。
您使用错误的公式来计算payAmount
,正确的代码将是:
payAmount =价格+(价格*税/ 100);
因为输入的税是一个整数,并且要使用百分比税,所以将其除以100,然后将税加到价格中。
if else语句中的另一个错误
payAmount=payAmount-(payAmount*discountRate_56_to_60);
//或discountRate_60_above
添加税后,您必须从payAmount
中扣除折扣率。
正确的代码是:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int price, age;
double tax, payAmount;
double discountRate_56_to_60 = 0.05;
double discountRate_60_above = 0.1;
System.out.print("Price?($)\n");
price = input.nextInt();
System.out.print("Tax(%)?\n");
tax = input.nextDouble();
System.out.print("Age?\n");
age = input.nextInt();
payAmount = price +(price* tax/100); //adding the tax to the price
if (age >= 55 && age<60) {
payAmount=payAmount-(payAmount*discountRate_56_to_60); //subtracting the appropriate discount according to age
}
else if (age >= 60) {
payAmount=payAmount-(payAmount*discountRate_60_above);
}
System.out.println("You pay: $" +payAmount);
}
答案 5 :(得分:0)
尝试下面的代码来计算折扣:
if (age > 55 && age <60) {
payAmount = payAmount - (payAmount*5)/100;
}
else if (age >= 60) {
payAmount = payAmount - (payAmount*10)/100;
}
答案 6 :(得分:0)
if(age>60) {
payAmount = payAmount - payAmount*0.10;
System.out.println("Paid Amount is "+ payAmount);
} else if(age>55 && age<=60) {
payAmount = payAmount - payAmount*0.05;
System.out.println("Paid Amount is "+ payAmount);
} else {
System.out.println("Paid Amount is "+ payAmount);
}