gpt4 book ai didi

java - 为什么这段java代码没有产生我期望的结果?

转载 作者:行者123 更新时间:2023-12-01 16:42:18 25 4
gpt4 key购买 nike

该计划需要根据客户的年龄性别发布正确的票价。

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 方法未给出正确的输出。对于每个票价,输出给出的金额是其一半。 calculateAmount 函数中的 if-else 条件未正确执行。

我需要将其更改为什么才能获得我期望的结果?

测试用例

名称不相关且基本票价为 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 |
+-----|--------|-------------------|-----------------------------+

最佳答案

问题是当你这样做时你会覆盖 person :

public void setPerson(Person p){
p =new Person();
person=p;
}

将此 setter 替换为:

public void setPerson(Person p){
this.person=p;
}

关于java - 为什么这段java代码没有产生我期望的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61103635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com