Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Losowanie 'randomowego znaku z ciągu nazwy
7ohn
post
Post #1





Grupa: Zarejestrowani
Postów: 9
Pomógł: 0
Dołączył: 24.11.2015

Ostrzeżenie: (0%)
-----


Witam, mam problem z pewnym prostym skryptem, lecz ja coś mi nie wychodzi.
Skrypt polega na losowaniu znaku z ciągu nazwy.
Tu jest fragment kodu

  1. <?php
  2. // Use rand() to print a random number to the screen
  3. ?>
  4. </p>
  5. <p>
  6. <?php
  7.  
  8.  
  9. $name = "Johnny";
  10.  
  11. // Use your knowledge of strlen(), substr(), and rand() to
  12. print ("Długość znaków ". strlen($name)."<br>");
  13. print ("Wyodrębnienie poszczególnych ciągów ". substr($name, 0, strlen($name)))."<br>";
  14.  
  15.  
  16. print ("Losowanie randomowego znaku z nazwy ". ));
  17. //??
  18. // print a random character from your name to the screen.
  19.  
  20. ?>


Zadanie to pochodzi z platformy e-learningowej codecademy.com

Sugerują mi użyć do tego substr, strlen. Próbowałem kilka sposobów, lecz wywala mi cały czas błąd.

Proszę o pomoc, i wyjaśnienie tego rozwiązania.
Pozdrawiam
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
7ohn
post
Post #2





Grupa: Zarejestrowani
Postów: 9
Pomógł: 0
Dołączył: 24.11.2015

Ostrzeżenie: (0%)
-----


ok, stworzyłem taki oto kod
  1. print ("Losowanie randomowego indeksu znaku z nazwy ". rand(0,strlen($name) - 1))."<br>";
  2.  
  3. $length = rand(0,strlen($name) - 1);
  4. print ("Losowany znak z nazwy to: ".substr($name,$length ,1));


Teoretycznie wszystko działa, lecz nadal wywala błąd.

Ten pseudo interpreter wyświetla komunikat: The random output you printed from your name should be just one character.

Ten post edytował 7ohn 24.11.2015, 14:46:03
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 6.10.2025 - 10:03