Hallo,
Ik ben op dit moment bezig met opdracht voor school en er is 1 ding waar ik echt niet uitkom. het gaat om het volgende.
ik moet een output creëren als het volgende:
YOOOOO
OYOOOO
OOYOOO
OOOYOO
OOOOYO
OOOOOY
OOOOYO
OOOYOO
OOYOOO
OYOOOO
YOOOOO
op dit moment heb ik de volgende output:
YOOOOO
OYOOOO
OOYOOO
OOOYOO
OOOOYO
OOOOOY
OOOOOO
OOOOOO
OOOOOO
OOOOOO
OOOOOO
code:
Ik moet dus nog aftellen graag heb ik jullie input hoe ik dit kan doen. Rechtstreekse antwoorden mogen maar graag met een uitleg erbij.Code:<!-- <!-- Naam: Studentnummer: --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Ster opdracht</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> </head> <body> <?php // ($x=10; $ster>=1; $ster--) { for ($y = 0; $y<=10; $y++) { for ($x = 0; $x<=5; $x++) { if ($x ==(0+$y)) { echo "Y"; } else{ echo "O"; } } echo "<br />"; } ?> </body> </html>
- For lus php probleem
-
31-03-2011, 14:52 #1
- Berichten
- 197
- Lid sinds
- 17 Jaar
For lus php probleem
-
-
31-03-2011, 15:09 #2
- Berichten
- 359
- Lid sinds
- 18 Jaar
Re: For lus php probleem
Ik ben zelf geen programmeur, maar het onderstaande werkte voor mij prima... Tuurlijk kan het efficienter, want ik doe in principe twee keer hetzelfde, maar misschien is de aanvliegroute (een standaard string en telkens een teken vervangen) een leuke insteek voor jouw oplossing...
<?php
$string = "000000";
for ($i=0; $i<6;$i++) {
$string2 = substr_replace ($string, "Y", $i,1);
echo $string2."<br>";
}
for ($i=0; $i<6;$i++) {
$string2 = substr_replace ($string, "Y", 5-$i,1);
echo $string2."<br>";
}
?>
-
31-03-2011, 15:39 #3
- Berichten
- 603
- Lid sinds
- 16 Jaar
Re: For lus php probleem
if ($x ==(0+$y))
{
echo "Y";
}
dit statement klopt niet. Y gaat boven de 5 uitkomen.. die loopt namelijk tot 10. en
PHP Code:<?php
for ($y = 0; $y<=10; $y++)
{
for ($x = 0; $x<=5; $x++)
{
$help = $y;
if($help > 5){
$help = 10-$help;
}
if ($x ==(0+$help))
{
echo "Y";
}
else{
echo "O";
}
}
echo "<br />";
}
?>Laatst aangepast door Johan Vliegenthart : 31-03-2011 om 15:50
-
31-03-2011, 15:57 #4
- Berichten
- 92
- Lid sinds
- 14 Jaar
Re: For lus php probleem
Of korter:
PHP Code:for($x=1,$up = true; $x<7 && $x>0; $x=($x<6 && $up==true ? $x+1 : $x-1),$up=($x<6 && $up==true ? true : false)){
echo str_repeat('O',$x-1).'Y'.str_repeat('O',6-$x).'<br />';
}
-
31-03-2011, 18:36 #5
- Berichten
- 359
- Lid sinds
- 18 Jaar
Re: For lus php probleem
Cool! Daarom ben jij een programmeur en ik niet ... :-)
NB hij kan nog korter... door de { en de } weg te halen... ;-)
Plaats een
- + Advertentie
- + Onderwerp
Marktplaats
Webmasterforum
- Websites algemeen
- Sitechecks
- Marketing
- Domeinen algemeen
- Waardebepaling
- CMS
- Wordpress
- Joomla
- Magento
- Google algemeen
- SEO
- Analytics
- Adsense
- Adwords
- HTML / XHTML
- CSS
- Programmeren
- PHP
- Javascript
- JQuery
- MySQL
- Ondernemen algemeen
- Belastingen
- Juridisch
- Grafisch ontwerp
- Hosting Algemeen
- Hardware Info
- Offtopic