Débutant : Boucle for...

arthur148 Messages postés 5 Date d'inscription dimanche 29 août 2004 Statut Membre Dernière intervention 14 novembre 2007 - 13 nov. 2007 à 22:46
cs_mathmax Messages postés 403 Date d'inscription vendredi 28 octobre 2005 Statut Membre Dernière intervention 31 août 2008 - 15 nov. 2007 à 15:17
Voila j'ai un petit problème, je dois realiser un sapin  qui s'affiche dans une console de cette facon :
     **
    ***
   ****
  ******
 ********
**********

C'est l'utilisateur qui tape le nombre de ligne du sapin, mais je bloque, je dois faire ca avec deux boucles l'une dans l'autre, des boucles for, voila mon code

using System;

using System.Collections.Generic;

using System.Text;

 

namespace ConsoleApplication2

{

    classProgram

    {

        staticvoid Main(string[]
args)

        {

 

 

            int
nombreligne;

            int
i;

            int i2;

            int
y;

            string
ligne ;

 

            ligne = "*";

 

            Console.WriteLine("Combien de lignes voulez vous ?");

            nombreligne = Convert.ToInt32(Console.ReadLine());

                  y= nombreligne ;

                for (i = 1; i <= nombreligne; i = i + 1)

                {

                  for
(i2=1;i<y;i2++)

                  {

                  Console.write("
");

                  }

                    ligne = ligne + "**";

                  nombreligne-- ;

                          Console.WriteLine(ligne);

 

 

            }

 

        }

         

 

        }

   

}

11 réponses

cs_mathmax Messages postés 403 Date d'inscription vendredi 28 octobre 2005 Statut Membre Dernière intervention 31 août 2008
14 nov. 2007 à 00:18
2 erreurs :
<strike> for (i2=1;i<y;i2++) </strike> =>   for (i2=1;i<y2;i2++)
et

pas <strike>nombreligne--;</strike>  => y--;

Et ton code marchera.

Après tu aurais quelques trucs à améliorer pour la beauté du code.

Mathmax

****************************************http://www.postsharp.org/
0
cs_mathmax Messages postés 403 Date d'inscription vendredi 28 octobre 2005 Statut Membre Dernière intervention 31 août 2008
14 nov. 2007 à 01:44
Ah et puis j'oubliais, avec ce code, tu ne dessine pas le haut du sapin (premier caractère).

Après tu peux te faire une classe "sapin". Comme ça tu pourras créer facilement autant de sapins que tu veux.

Voici un exemple :

    class Program
    {
        static void Main(string[] args)
        {
            Sapin sapin1 = new Sapin(5, char.Parse("*"));
            sapin1.Draw();

            Sapin sapin2 = new Sapin(10, char.Parse("+"));
            sapin2.Draw();

            Sapin sapin3 = new Sapin(8, char.Parse("°"));
            sapin3.Draw();
        }
    }

    public class Sapin
    {
        private int _NombreLigne;
        public int NombreLigne
        {
            get { return _NombreLigne; }
            set { _NombreLigne = value; }
        }

        private string _Motif;

        public char Motif
        {
            get { return char.Parse(_Motif); }
            set { _Motif = value.ToString(); }
        }

        public Sapin(int nombreLigne, char motif)
        {
            NombreLigne = nombreLigne;
            Motif = motif;
        }

        public void Draw()
        {
            StringBuilder ligne = new StringBuilder(_Motif);

            Console.WriteLine(Center(_Motif));

            for (int i = 1; i < NombreLigne; i++)
            {
                ligne.Append(Motif, 2);
                Console.WriteLine(Center(ligne.ToString()));
            }
        }

        private string Center(string value)
        {
            return value.PadLeft(NombreLigne + value.Length / 2, char.Parse(" "));
        }
    }

Mathmax

****************************************http://www.postsharp.org/
0
arthur148 Messages postés 5 Date d'inscription dimanche 29 août 2004 Statut Membre Dernière intervention 14 novembre 2007
14 nov. 2007 à 18:37
Heu j'ai encore un soucil avec y2 qui ne peut pas fonctionner, ca correspond a quoi ?
0
cs_mathmax Messages postés 403 Date d'inscription vendredi 28 octobre 2005 Statut Membre Dernière intervention 31 août 2008
14 nov. 2007 à 20:08
Désolé i2.

Mathmax

****************************************http://www.postsharp.org/
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
arthur148 Messages postés 5 Date d'inscription dimanche 29 août 2004 Statut Membre Dernière intervention 14 novembre 2007
14 nov. 2007 à 21:08
Merci Math ! Malheuresement je n'est toujours qu'un côté du sympa. Quel foutu exo idiot, je te remet le code corrigé par tes soins :)
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{


int nombreligne;
int i;
int i2;
int y;
string ligne;

ligne = "*";

Console.WriteLine("Combien de lignes voulez vous ?");
nombreligne = Convert.ToInt32(Console.ReadLine());
y = nombreligne;
for (i = 1; i <= nombreligne; i++)
{
for (i2 = 1; i < i2; i2++)
{
Console.Write(" ");
}
ligne = ligne + "**";
y--;
Console.WriteLine(ligne);


}

}


}

}
0
cs_mathmax Messages postés 403 Date d'inscription vendredi 28 octobre 2005 Statut Membre Dernière intervention 31 août 2008
14 nov. 2007 à 21:23
C'est exo pour l'école, c'est ça ?

Mathmax

****************************************http://www.postsharp.org/
0
arthur148 Messages postés 5 Date d'inscription dimanche 29 août 2004 Statut Membre Dernière intervention 14 novembre 2007
14 nov. 2007 à 21:26
Ouicht, en fete on a un cours, qui s'appel autonomie, et il nous donne 15 énoncés et faut en faire le plus possible, malheuresement je coince sur celui la... Ca m'enerve parce que le C# j'adore, contrairement au VB !
0
cs_mathmax Messages postés 403 Date d'inscription vendredi 28 octobre 2005 Statut Membre Dernière intervention 31 août 2008
14 nov. 2007 à 21:35
Ben là il est résolu non ?

Mathmax

****************************************http://www.postsharp.org/
0
arthur148 Messages postés 5 Date d'inscription dimanche 29 août 2004 Statut Membre Dernière intervention 14 novembre 2007
14 nov. 2007 à 21:36
Non :'( Mon sapin s'affiche commme ca :

*
**
***
****
*****

au lieu de

*
**
****
******
0
Nikoui Messages postés 794 Date d'inscription vendredi 24 septembre 2004 Statut Membre Dernière intervention 19 août 2008 13
15 nov. 2007 à 12:04
Un cours qui s'apelle "autonomie", et tu fais faire tes exos sur le net... Y'a que moi que ça choc ?

<hr size="2" width="100%" />
Working as designed
www.nikoui.fr
0
cs_mathmax Messages postés 403 Date d'inscription vendredi 28 octobre 2005 Statut Membre Dernière intervention 31 août 2008
15 nov. 2007 à 15:17
Mathmax






****************************************http://www.postsharp.org/
0
Rejoignez-nous