如何在每行

时间:2016-04-08 17:26:00

标签: php

http://cps276.net/zgambrell/08/index.php

该程序接受用户输入的案例数量,每箱的包数,每包的卡数,以及案例中不常见,罕见和传奇卡的频率数。它类似于“fizzbuzz”测试。

“客户”希望每包卡都输出到自己独立的线路上。这是我目前遇到的问题。

我还不熟悉使用php和面向对象编程。

这是我的所有代码:

<form method="post" action="index.php">
    Total Number of Cases:
    <input type="text" name="totNumCases" value="" /><br/>
    Number of Packs in Each Case:
    <input type="text" name="packsInEachCase" value="" /><br/>
    Base Number of Cards in Each Pack:
    <input type="text" name="baseNumCards" value="" /><br/>
    Frequency of "Uncommon" Cards:
    <input type="text" name="freqUncommon" value="" /><br/>
    Frequency of "Rare" Cards:
    <input type="text" name="freqRare" value="" /><br/>
    Frequency of "Legendary" Cards:
    <input type="text" name="freqLegendary" value="" /><br/>
    <input type="submit" name="submit" value ="Submit" /><br/>
    <hr />

</form>


<?php

$cardsPerPack = $_POST['baseNumCards'];
$packsPerCase = $_POST['packsInEachCase'];

class CommonCards{

    private $card;

    public function __construct($card){
        $this->card = 'COM';
    }

    public function __toString(){
        return $this->card.' ';
    }

    public function process(CardCounter $f){
        $result = $f->increment();
        if($result){
            $this->card = $result;
        }
    }
}

class CardCounter {
    private $uncommonCount = 0;
    private $rareCount = 0;
    private$legendaryCount = 0;

    public function increment(){
        $this->uncommonCount++;
        $this->rareCount++;
        $this->legendaryCount++;
        $out = '';
        if($this->uncommonCount == $_POST['freqUncommon']){
            $this->uncommonCount = 0;
            $out.='UNC';
        }
        if($this->rareCount == $_POST['freqRare']){
            $this->rareCount = 0;
            $out.='RAR';
        }
        if($this->legendaryCount == $_POST['freqLegendary']){
            $this->legendaryCount = 0;
            $out.='LEG';
        }

        return $out;
    }

}

$arr = array();

for($i = 1; $i <= ($cardsPerPack * $packsPerCase); $i++){
    $arr[] = new CommonCards($i);//getting all cards, setting them all equal to COM. "common cards"
}
$f = new CardCounter();

foreach($arr as $commonCards){
    $commonCards->process($f);//doing the 'fizzbuzz' process
    echo $commonCards;// printing out the total number of cards.
}

我一直在尝试使用$ cardsPerPack来帮助解决这个问题。如果我可以将它设置为只是每行打印一个数字它将完全正常工作。

否则我已尝试使用cardsPerPack上的%。

大多数情况下,我一直在关注我的尝试:

foreach($arr as $commonCards){
    $commonCards->process($f);//doing the 'fizzbuzz' process
    echo $commonCards;// printing out the total number of cards.
}

在这里:

public function process(CardCounter $f){
        $result = $f->increment();
        if($result){
            $this->card = $result;
        }
    }

但所有尝试都失败了。

非常感谢任何帮助。

0 个答案:

没有答案