返回错误值的Java类和方法

返回错误值的Java类和方法,java,methods,Java,Methods,我正在用java编写代码,模拟以非常低的代码级别进入马戏团。它只是主类Customer下的几个类 我的代码只是主类及其方法,然后是一些print语句,我承认的方法对所有值都返回false,如果它们能够支付票证的话,它应该返回true。代码如下: class Customerrr { String name; int age; float money; public Customerrr(String initName, int initAge) {

我正在用java编写代码,模拟以非常低的代码级别进入马戏团。它只是主类Customer下的几个类

我的代码只是主类及其方法,然后是一些print语句,我承认的方法对所有值都返回false,如果它们能够支付票证的话,它应该返回true。代码如下:

class Customerrr {
    String name;
    int age;
    float money;

    public Customerrr(String initName, int initAge) {
        name = initName;
        age = initAge;
    }

    public Customerrr(String initName, int initAge, float initMoney) {
        name = initName;
        age = initAge;
        money = initMoney;
    }

    public Customerrr() {
    }

    public double computeFee() {
        //adult fee $12.75 anyone 18+
        //3 and under no fee
        //65 or oldr %50
        //4 to 17 $8.50
        double result;

        if (age < 3) {
            result = 0.0;
        }

        else if (age < 17){
            result = 8.50;
        }
        else if (age < 64) {
            result = 12.75;
        }
        else {
            result = 12.75/2;
        }
        return result;
    }
    public boolean spend(float amount) {
        boolean checker;
        if (amount > money) {
            amount = (float) (amount - computeFee());

            checker = true;
        }
        else {
            amount = amount;
            checker = false;
        }
        return checker;
    }

    public boolean hasMoreMoneyThan(Customerrr c){
        boolean result;
        if (money > c.money) {
            result = true;
        }
        else {
            result = false;
        }
        return result;
    }
    public boolean payAdmission() {
        //System.out.println(name + " has paid $" + computeFee() + " for admission");
        boolean result;
        if (spend(money)) {
            result = true;
        }
        else {
            result = false;
        }
        return result;
    }

    public boolean admitted () {
        boolean result;
        if (payAdmission()) {
            result = true;
        }
        else {
            result = false;
        }
        return result;
    }
}



public class AddingMethods {
    public static void main(String args[]) {
        Customerrr c1, c2, c3, c4;
        c1 = new Customerrr("Bob", 17, 100);
        c2 = new Customerrr("Dottie", 3, 1000);
        c3 = new Customerrr("Jane", 24, 40);
        c4 = new Customerrr("Sam", 72, 5);

        System.out.println(" Bob has been admitted ... " + c1.admitted());
        System.out.println(" Dottie has been admitted ... " + c2.admitted());
        System.out.println(" Jane has been admitted ... " + c3.admitted());
        System.out.println(" Sam has been admitted ... " + c4.admitted());
        c1.payAdmission();
        System.out.println("Bob has been admitted ... " + c1.admitted());
        c2.payAdmission();
        System.out.println("Dottie has been admitted ... " + c2.admitted());
        c3.payAdmission();
        System.out.println("Jane has been admitted ... " + c3.admitted());
        c4.payAdmission();
        System.out.println("Sam has been admitted ... " + c4.admitted());
        System.out.println(" Bob has $" + c1.money);
        System.out.println(" Dottie has $" + c2.money);
        System.out.println(" Jane has $" + c3.money);
        System.out.println(" Sam has $" + c4.money);


    }
}


如果您看到支出方法,那么金额和金钱总是相等的,因为您在支出方法中将PayAcmission方法中的金钱作为金额传递。它将始终导致返回false。因此,isadmitt始终为false,您需要在spend方法中更改此处的逻辑

 public boolean spend(float amount) {
            boolean checker;
            if (amount > money) {
                amount = (float) (amount - computeFee());
    
                checker = true;
            }
            else {
                amount = amount;
                checker = false;
            }
            return checker;
        }

 public boolean payAdmission() {
        //System.out.println(name + " has paid $" + computeFee() + " for admission");
        boolean result;
        if (spend(money)) {
            result = true;
        }
        else {
            result = false;
        }
        return result;
    }

如果您看到支出方法,那么金额和金钱总是相等的,因为您在支出方法中将PayAcmission方法中的金钱作为金额传递。它将始终导致返回false。因此,isadmitt始终为false,您需要在spend方法中更改此处的逻辑

 public boolean spend(float amount) {
            boolean checker;
            if (amount > money) {
                amount = (float) (amount - computeFee());
    
                checker = true;
            }
            else {
                amount = amount;
                checker = false;
            }
            return checker;
        }

 public boolean payAdmission() {
        //System.out.println(name + " has paid $" + computeFee() + " for admission");
        boolean result;
        if (spend(money)) {
            result = true;
        }
        else {
            result = false;
        }
        return result;
    }

金额和金钱变量是一样的,我有更新的代码;或者可以声明不同的变量。这取决于你的逻辑

    public boolean spend(float amount) {
        boolean checker;
        if (amount >= money) {
            amount = (float) (amount - computeFee());

            checker = true;
        }
        else {
            amount = amount;
            checker = false;
        }
        return checker;
    }

金额和金钱变量是一样的,我有更新的代码;或者可以声明不同的变量。这取决于你的逻辑

    public boolean spend(float amount) {
        boolean checker;
        if (amount >= money) {
            amount = (float) (amount - computeFee());

            checker = true;
        }
        else {
            amount = amount;
            checker = false;
        }
        return checker;
    }
你的问题在于花钱的方法。下面是您的代码,注释显示了每行发生的情况:

public boolean spend(float amount) {
    boolean checker;
    if (amount > money) { //if the customer cannot afford the fee
        amount = (float) (amount - computeFee()); // set the local variable "amount" (which is never used after this)

        checker = true;
    }
    else { //if the customer has enough money
        amount = amount; //do absolutely nothing
        checker = false;
    }
    return checker; //returns true if the customer couldn't afford the fee, false otherwise
}
我相信你想要更像这样的东西:

public boolean spend(float amount) {
    if (money > amount) { //if the customer has enough money
        money -= (float) computeFee(); //subtract the fee from the instance variable "money"
        return true; //returning true because the customer does have enough money
    }

    //if the customer does not have enough money
    return false; //returns false and does not change the customer's "money" variable
}
你的问题在于花钱的方法。下面是您的代码,注释显示了每行发生的情况:

public boolean spend(float amount) {
    boolean checker;
    if (amount > money) { //if the customer cannot afford the fee
        amount = (float) (amount - computeFee()); // set the local variable "amount" (which is never used after this)

        checker = true;
    }
    else { //if the customer has enough money
        amount = amount; //do absolutely nothing
        checker = false;
    }
    return checker; //returns true if the customer couldn't afford the fee, false otherwise
}
我相信你想要更像这样的东西:

public boolean spend(float amount) {
    if (money > amount) { //if the customer has enough money
        money -= (float) computeFee(); //subtract the fee from the instance variable "money"
        return true; //returning true because the customer does have enough money
    }

    //if the customer does not have enough money
    return false; //returns false and does not change the customer's "money" variable
}

你试过一步一步地调试吗?Inside-spend方法你有一个无用的任务:amount=amount你试过一步一步地调试吗?Inside-spend方法你有一个无用的任务:amount=amount谢谢,我完全忘记了谢谢,我完全忘记了。非常感谢你的帮助。我现在明白我错在哪里了非常感谢你的帮助。我知道我现在错在哪里了