java - 为什么这个 java 代码没有产生我期望的结果?
问题描述
age
程序需要根据客户的情况开出正确的票价gender
。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Person p1 = new Person();
System.out.println("Enter the passenger name:");
String name = sc.next();
p1.setName(name);
System.out.println("Enter the gender:");
char gen = sc.next().charAt(0);
p1.setGender(gen);
System.out.println("Enter the age:");
int age = sc.nextInt();
p1.setAge(age);
BusTicket b1 = getTicketDetails();
b1.setPerson(p1);
b1.calculateTotal();
System.out.println("Ticket no:"+b1.getTicketno());
System.out.println("Passenger Name:"+p1.getName());
System.out.println("Price of a ticket:"+b1.getTicketprice());
System.out.println("Total Amount:"+b1.getTotalamt());
}
public static BusTicket getTicketDetails() {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the ticket no:");
int no=sc.nextInt();
BusTicket t1=new BusTicket();
t1.setTicketno(no);
System.out.println("Enter the ticket price:");`enter code here`
float cost=sc.nextFloat();
t1.setTicketprice(cost);
return t1;
}
}
public class Person {
private String name;
private char gender;
private int age;
public void setName(String name){
this.name=name;
}
public String getName() {
return this.name;
}
public void setGender(char gender){
this.gender = gender;
}
public char getGender() {
return this.gender;
}
public void setAge(int age){
this.age = age;
}
public int getAge() {
return this.age;
}
}
public class BusTicket {
private int ticketNo;
private float ticketPrice;
private float totalAmount;
private Person person;
public void setTicketno(int no) {
ticketNo = no;
}
public int getTicketno() {
return this.ticketNo;
}
public void setTicketprice(float price){
ticketPrice = price;
}
public float getTicketprice(){
return this.ticketPrice;
}
public void setTotalamt(float amt) {
totalAmount=amt;
}
public float getTotalamt() {
return this.totalAmount;
}
public void setPerson(Person p) {
p=new Person();
person=p;
}
public Person getPerson() {
return this.person;
}
public void calculateTotal() {
if(16 > person.getAge())
this.totalAmount = ticketPrice - (float) (ticketPrice * 0.50);
else if(person.getAge() < 60)
this.totalAmount = ticketPrice - (float) (ticketPrice * 0.25);
else if (person.getGender() == 'f' || person.getGender() == 'F')
this.totalAmount = ticketPrice - (float) (ticketPrice * 0.10);
else
this.totalAmount = ticketPrice;
}
}
该calculateTotal
方法没有给出正确的输出。对于每个票价,输出给出的金额是它的一半。函数中的 if-else 条件calculateAmount
未正确执行。
为了给我期望的结果,我需要将其更改为什么?
测试用例
其中 Name 无关紧要,基本票价为$100.00
。
+-----|--------|-------------------|-----------------------------+
| Age | Gender | Expected Discount | Expected total ticket price |
|-----|--------|-------------------|-----------------------------|
| 15 | M | 0.50 | $50.00 |
| 15 | F | 0.50 | $50.00 |
| 4 | M | 0.50 | $50.00 |
| 12 | F | 0.50 | $50.00 |
|-----|--------|-------------------|-----------------------------|
| 61 | M | 0.25 | $75.00 |
| 61 | F | 0.25 | $75.00 |
| 99 | M | 0.25 | $75.00 |
| 75 | F | 0.25 | $75.00 |
|-----|--------|-------------------|-----------------------------|
| 16 | F | 0.10 | $90.00 |
| 27 | F | 0.10 | $90.00 |
| 48 | F | 0.10 | $90.00 |
| 60 | F | 0.10 | $90.00 |
|-----|--------|-------------------|-----------------------------|
| 16 | M | 0.00 | $100.00 |
| 27 | M | 0.00 | $100.00 |
| 48 | M | 0.00 | $100.00 |
| 60 | M | 0.00 | $100.00 |
+-----|--------|-------------------|-----------------------------+
解决方案
问题是您在执行以下操作时正在覆盖人:
public void setPerson(Person p){
p =new Person();
person=p;
}
将此设置器替换为:
public void setPerson(Person p){
this.person=p;
}
推荐阅读
- python-3.x - 将任意数量的参数传递给python中的函数
- python - 是否有解决方案在第一次输入后退出而不继续第二次输入?
- javascript - 类型错误:无法读取未定义的属性“forEach”(节点 js、条纹、快递)
- algorithm - 需要图形问题的方法或解决方案
- javascript - 在 Gatsby.js 中限制过滤的 Graphql 项目
- r - 从向量中打印元音
- swift - 使用 for in 循环计算两个数字的总和
- docker - shell 脚本在 cron 作业中不起作用 | 码头工人
- python - 如何将两个模型合并为一个表单?
- reactjs - React Hooks & Typescript - 表单不受控制?