1

创建具有以下输出的程序:

a 00000000
b 00000000
c 00000X00
d 00000000
e 00000000
f 000X0000
g 00000000
h 00000X00
12345678

用户在程序开始时输入二维数组。基于这个序列(坐标)在桌子上绘制 X。

条件:必须使用数组和循环。

我 18 岁,两周前开始学习 php。我在完成这项任务时遇到了麻烦。谁能帮我 ?谢谢!

对不起!

这就是我所做的:

<?php
$input = array(2 => array(5),5 => array(3),7 => array(6));
$range = array('a','b','c','d','e','f','g','h');
$length = 8;

$output = '';
foreach($range as $index => $letter)
{
$output .= "$letter ";

for($i = 0; $i < $length; ++$i)
{
$output .= (array_key_exists($index, $input) && in_array($i, $input[$index])) ? 'X' : '0';
}

$output .= "\n";
}

echo $output;
?>

我得到了这个输出:

a 00000000
b 00000000
c 00000X00
d 00000000
e 00000000
f 000X0000
g 00000000
h 000000X0

而不是这个:

a 00000000
b 00000000
c 00000X00
d 00000000
e 00000000
f 000X0000
g 00000000
h 00000X00
12345678

我在哪里犯错?

4

2 回答 2

1

7 => array(6)应该7 => array(5)

http://sandbox.onlinephpfunctions.com/code/423262c6a4bcdb8693c179dc620966d779b65a51

于 2013-04-06T17:12:34.220 回答
0
<?php
$input = array(2 => array(5),5 => array(3),7 => array(5));
$range = array('a','b','c','d','e','f','g','h');
$length = 8;

$output = '';
$x='';
foreach($range as $index => $letter)
{
    $output .= "$letter ";
    for($i = 0; $i < $length; ++$i)
    {
        $output .= (array_key_exists($index, $input) && in_array($i, $input[$index])) ? 'X' : '0';
    }

    $output .= "\n";
    $x.= 1+$index;
}

$output .= $x;
echo $output;

?>

这将输出

a 00000000
b 00000000
c 00000X00
d 00000000
e 00000000
f 000X0000
g 00000000
h 00000X00
12345678
于 2016-03-16T04:35:11.017 回答