Visualizzazione dei risultati da 1 a 5 su 5

Discussione: Numero magico

  1. #1

    Numero magico

    Ciao,
    devo fare un programma che consiste nell'indovinare il numero creato a random.
    Il problema è che non riesco neanche a farlo partire, potreste gentilmente dirmi dove si trova il problema, e dove si può migliorare.

    Vi ringrazio in anticipo.
    codice:
    <!-- Indovinare un numero -->
    <html>
    <head>
    </head>
    <body>
     <script language="javascript">
      var num, nc, i=1;
      num=prompt("Inserire un numero: ");
      nc=Math.floor((Math.random()*100)+1); 
      while (num!=nc)
      {
       if (num<nc)
        document.write("</br>Il numero e' piu\' grande");
       if (num>nc)
        document.write("</br> Il numero e' piu\' piccolo");
        num=prompt("Inserire un numero: ");
        i++;
      }
      if (i==1)
       document.write(+i" tentativo: Mitico");
      if (i>=2 && ntent<=4)
       document.write(+i " tentativi: Bravissimo");
      if (i>=5 && ntent<=10)
       document.write(+i" tentativi: Insomma");
      if (i>=10)
       document.write(+i" tentativi: No Comment");
      document.write("<br> Il gioco e' finito.");
     </script>
    <body>
    </html>
    Ciao e grazie.
    Your time is limited, so don't waste it living someone else's life. Stay hungry, stay foolish. (Steve Jobs)

  2. #2
    Ho messo anche un ciclo while per controllare l'inserimento, ma non parte lo stesso.
    codice:
    <!-- Indovinare un numero -->
    <html>
    <head>
    </head>
    <body>
     <script language="javascript">
      var num, nc, i=1;
      num=prompt("Inserire un numero: ");
      while(num<1 || num > 100)
      {
       num=prompt("Il numero e' minore di 1 o maggiore di 100, reinseriscilo: ");
      }
      nc=Math.floor((Math.random()*100)+1); 
      while(num!=nc)
      {
       if (num<nc)
        document.write("</br>Il numero e' piu\' grande");
       if (num>nc)
        document.write("</br> Il numero e' piu\' piccolo");
        num=prompt("Inserire un numero: ");
        i++;
      }
      if (i==1)
       document.write(+i" tentativo: Mitico");
      if (i>=2 && ntent<=4)
       document.write(+i " tentativi: Bravissimo");
      if (i>=5 && ntent<=10)
       document.write(+i" tentativi: Insomma");
      if (i>=10)
       document.write(+i" tentativi: No Comment");
      document.write("<br> Il gioco e' finito.");
     </script>
    <body>
    </html>
    Ciao e grazie.
    Your time is limited, so don't waste it living someone else's life. Stay hungry, stay foolish. (Steve Jobs)

  3. #3
    Utente di HTML.it
    Registrato dal
    Feb 2014
    residenza
    Voltati
    Messaggi
    913
    codice:
    document.write(+i" tentativo: Mitico");
    (e gli altri simili) devono diventare
    codice:
    document.write(i+" tentativo: Mitico");
    codice:
    if (i>=2 && ntent<=4)
    cos'è ntent? Non dovrebbe essere i?
    Ultima modifica di tampertools; 25-03-2014 a 23:34
    No

  4. #4
    Ciao,
    ho seguito il tuo consiglio e ho apportato delle modifiche al codice.
    Adesso funziona bene.
    codice:
    <!-- Indovinare un numero -->
    <html>
    <head>
    </head>
    <body>
     <script language="javascript">
      var num, nc, i=1;
      document.write("Benventuto: Prova ad indovinare il numero tra 1 e 100 compresi</br></br>"); //Frase di benvenuto
      num=prompt("Inserire un numero: ");
      while(num<1 || num > 100) //Controllo sull'inserimento del numero
      {
       num=prompt("Il numero e' minore di 1 o maggiore di 100, reinseriscilo: ");
      }
      nc=Math.floor((Math.random()*100)+1); 
      while(num!=nc)
      { 
       if (num<nc)
        document.write("</br>Il numero e' piu\' grande di " +num);
       else
        document.write("</br> Il numero e' piu\' piccolo di " +num);
       num=prompt("Inserire un altro numero: ");
       i++; //Numero di tentativi
      }
      switch(i) 
      {
       case 1:
        document.write("</br>" +i+ " tentativo: Sniper");
        break;
       case 2:
       case 3:
       case 4:
        document.write("</br>" +i+ " tentativi: Not Bad");
        break;
       case 5:
       case 6:
       case 7:
       case 8:
       case 9:
       case 10:
        document.write("</br>" +i+ " tentativi: Insomma");
        break;
       default:
        document.write("</br>" +i+ " tentativi: No Comment");
      }
      document.write("</br></br> Il gioco e' finito.");
     </script>
    <body>
    </html>
    Ciao e Grazie ancora.
    Your time is limited, so don't waste it living someone else's life. Stay hungry, stay foolish. (Steve Jobs)

  5. #5
    Utente di HTML.it L'avatar di vic53
    Registrato dal
    Oct 2010
    residenza
    Fonte Nuova (Roma)
    Messaggi
    592
    io invece ti ho corretto il tuo codice...senza usare switch...
    codice:
    <html>
    <head>
    </head>
    <body>
    test<br>
     <script language="javascript" type="text/javascript">
      var num, nc, i=0;ntent=0;
       nc=Math.floor((Math.random()*100)+1); 
      // alert(nc); 
      while (num!=nc)  
       {
      i++;
      document.write("<br>i="+i);
      
      num=prompt("Inserire un numero: ");
       if (num<nc)
        document.write("</br>Il numero e' piu\' grande");
       if (num>nc) {
        document.write("</br> Il numero e' piu\' piccolo");
       }
      if (num==nc) {
        if  (i==1)
         document.write("<br>"+i+ " tentativo: Mitico");
        if (i>=2 && ntent<=4)
         document.write("<br>"+i+ " tentativi: Bravissimo");
        if (i>=5 && ntent<=10)
         document.write("<br>"+i + " tentativi: Insomma");
        if (i>=10){
         document.write("<br>"+i+" tentativi: No Comment");
         document.write("<br> Il gioco e' finito.");
         }
        } 
      }
     </script>
    <body>
    </html>
    ciao
    Vic53

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.