Wątek przeniesiony 2016-11-14 12:27 z C# i .NET przez ŁF.

Jak powrócić do 1 linkijki Ifa, while

0
      namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string tekst;
            string tekst1;

            Console.WriteLine("siemka, podaj jak masz na imię");
            tekst = Console.ReadLine();
            tekst1 = "lol";
            Console.WriteLine(tekst+" " + "Witam CIę");
            Console.WriteLine("Zrób to jeszcze raz");
            tekst1 = Console.ReadLine();

                            if (tekst == tekst1)
                            {
                                Console.WriteLine("Wpisałeś dwa razy coś innego");
                            }
                            else
                            {
                                Console.WriteLine("test");
                            }
            
              

            Console.ReadLine();
                   Console.ReadLine();

        }
    }
}
    } 

Witam, mam pytanie jak zrobić aby gdy wpiszę 2 imię or ciąg znaków (tekst1) błędnie aby program wracał do 1 linijki Ifa, próbowałem coś stworzyć z While lecz niestety nie mam pojęcia jak to zrobić, są to moje początki więc prosiłbym o łatwe wytłumaczenie co robię nie tak.
Gdy próbowałem zrobić coś z while wyglądało to tak :

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

                Console.WriteLine("siemka, podaj jak masz na imię");
                tekst = Console.ReadLine();
                tekst1 = "lol";
                Console.WriteLine(tekst+" " + "Witam CIę");
                Console.WriteLine("Zrób to jeszcze raz");
               

                while (tekst == tekst1)
                {
                    tekst1 = Console.ReadLine();
                        Console.WriteLine("Wpisałeś dwa razy coś innego");
                
                }

                
                Console.ReadLine();
                       Console.ReadLine();

            }
        }
    }
0
 
            string tekst;
            string tekst1;
            Console.WriteLine("siemka, podaj jak masz na imię");
            tekst = Console.ReadLine();
            tekst1 = "lol";
            Console.WriteLine(tekst + " " + "Witam CIę");
            Console.WriteLine("Zrób to jeszcze raz");
            do
            {
                tekst1 = Console.ReadLine();
                if (tekst == tekst1)
                {
                    Console.WriteLine("Imiona się zgadzają.");
                }
                else
                {
                    Console.WriteLine("Wpisałeś coś innego, imiona muszą się zgadzać.\nSpróbuj jeszcze raz:");
                }
            } while (tekst1 != tekst);
            Console.ReadLine();

Nie wiem czy o coś takiego chodzi, pętla do...while na pewno się wykona raz czyli będziesz musiał wpisać drugi raz imię, jeśli imiona z tekst i tekst1 się będą zgadzać to wyświetli się instrukcja z if'a i koniec programu a jeśli się nie będą zgadzać wyświetli się else i prośba o podanie imienia jeszcze raz po enterze (\n).

0

Zrobiłem to w taki sposób przed chwilą udało mi się :)

       class Program
        {
            static void Main(string[] args)
            {
                string tekst;
                string tekst1;
                tekst1 = " ";

                Console.WriteLine("siemka, podaj jak masz na imię");
                tekst = Console.ReadLine();
               
                Console.WriteLine(tekst+" " + "Witam CIę");
                while (tekst  != tekst1)
                {
                    Console.WriteLine("podaj imie drugi raz");
                    tekst1 = Console.ReadLine();

                
                }

                Console.WriteLine("BRAWO!");
2

Instrukcja powrotu to "goto":

    
    using System;
    public class Test
    {
    	public static void Main()
    	{
                    string tekst;
                    string tekst1;
                    tekst1 = " ";
     
                    Console.WriteLine("siemka, podaj jak masz na imię");
                    tekst = Console.ReadLine();
     
                    if (tekst == null || tekst.Length == 0) {
                       goto koniec;
                    }
     
                    Console.WriteLine(tekst+" " + "Witam CIę");
     
                powtorz:
                    Console.WriteLine("podaj imie drugi raz");
                    tekst1 = Console.ReadLine();
     
                    if (tekst1 == null) 
                      goto koniec;
                    else if (tekst  != tekst1 && tekst1.Trim().Length>0) {
                      Console.WriteLine("Niepoprawnie: ["+tekst1+"]");	 
                      goto powtorz;
                    }
     
                    Console.WriteLine("BRAWO!");	
     
                koniec:    
                    Console.WriteLine("");	
    	}
     
    }

http://ideone.com/tKgZ9a

więcej o tym: https://msdn.microsoft.com/pl-pl/library/13940fs2.aspx

1 użytkowników online, w tym zalogowanych: 0, gości: 1