Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 责任链:余数算法?_Java_Algorithm_Handler_Chain Of Responsibility - Fatal编程技术网

Java 责任链:余数算法?

Java 责任链:余数算法?,java,algorithm,handler,chain-of-responsibility,Java,Algorithm,Handler,Chain Of Responsibility,在以下方法中,我尝试执行以下操作: 如果您至少有20英镑: 算出20英镑纸币的数目 计算出剩下的剩余部分–将其传递给下一个处理程序 如果少于20英镑,请致电下一位处理人 注:该程序适用于ATM自动取款机,根据用户需要的金额分配纸币20、10、5 下面是我到目前为止的解决方案,我需要帮助纠正算法 @Override public void issueNotes(int amount) { //work out amount of twenties needed if(amount

在以下方法中,我尝试执行以下操作:

如果您至少有20英镑:

算出20英镑纸币的数目

计算出剩下的剩余部分–将其传递给下一个处理程序

如果少于20英镑,请致电下一位处理人

注:该程序适用于ATM自动取款机,根据用户需要的金额分配纸币20、10、5

下面是我到目前为止的解决方案,我需要帮助纠正算法

@Override
public void issueNotes(int amount) {
    //work out amount of twenties needed
    if(amount >= 20) {
        int dispenseTwenty;
        int remainder;
        dispenseTwenty = amount%20;
        remainder = amount = //call next handler?
    }
    else {
        //call next handler (as amount is under 20)
    }
}

不要介意语法,请参见以下内容:

public static final denominators = [20,10,5]

// recursive function

// you can start calling this recursive function with denom_index=0, thus using denominator 20

public void issueNotes(int amount, int denom_index)

{

   int currentDenominator = denominators[denom_index];

   if(amount ==0) return; // no more dispensing

   if(denom_index >2) // remainder drop to below 5

    {

       throwException (" remaining amount not dispensable ");

    }


   if (amount < currentDenominator) // amount less than current denominator

    {

        issueNotes(amount, denom_index+1);

    }

   else

   {

        dispenseNotes(amount/currentDenominator, currentDenominator);

        // call the handler with remainder of the amount and next denominator

        issueNotes(amount%currentDenominator, denom_index+1);

   }

}

责任链模式取决于是否能够提供处理请求消息的行为,以及是否有可能处理请求消息。如果处理程序无法处理请求,它将调用下一个en封装处理程序

这两个核心组成部分将是一个接口和具体部分

interface IMoneyHandler {
    void issueNotes(int money);
    void setNext(IMoneyHandler handler);
}
具体实施的一个例子可以是-

class TwentyMoneyHandler implements IMoneyHandler {
    private IMoneyHandler nextHandler;

    @Override
    public void issueNotes(int money) {
        int handlingAmount = 20;
        // Test if we can handle the amount appropriately, otherwise delegate it
        if(money >= handlingAmount) {
            int dispenseNotes = money / handlingAmount;
            System.out.println(dispenseNotes + " £20s dispenses");
            int remainder = money % handlingAmount;
            // Propagate the information to the next handler in the chain
            if(remainder > 0) {
                callNext(remainder);
            }
        } else {
            // call the next handler if we can not handle it
            callNext(money);
        }
    }

    // Attempts to call the next if there is money left
    private void callNext(int remainingMoney) {
        // Note, there are different ways of null handling
        // IE throwing an exception, or explicitly having a last element
        // in the chain which handles this scenario
        if(nextHandler != null) {
            nextHandler.issueNotes(remainingMoney);
        }
    }

    @Override
    public void setNext(IMoneyHandler handler) {
        this.nextHandler = handler;
    }
}

注意:在现实世界中,您可能会为此提供一个抽象实现,以避免代码重复。

您的类中应该有另一个处理程序as字段来处理下一个工作。是的,但这是算法,我需要帮助计算注释数量,然后计算下一个处理程序的剩余数量……嗯,想象一下你在现实生活中会怎么做,然后找出算法并对其进行编码。注意,你将给出的20的数字是预期的amount/20整数除法,传递给下一个处理程序的数量始终是amount%20首先考虑你想要的输出。您希望您的方法如何返回每个值的注释数?阵列?几种方法,每种类型一种?