[C#] '*' przy pisaniu w konsoli 4082 2

O temacie

Autor MajkeI

Zaczęty 1.11.2014 roku

Wyświetleń 4082

Odpowiedzi 2

MajkeI

MajkeI

Użytkownicy
Front End Developer
posty698
Propsy169
Profesjabrak
  • Użytkownicy
  • Front End Developer

MajkeI

[C#] '*' przy pisaniu w konsoli
2014-11-01, 18:06(Ostatnia zmiana: 2014-11-01, 18:29)
Wymyśliłem sobie, żeby napisać, który zamiast normalnie znaków w konsoli wyświetla tylko '*'. Jak przy podawaniu hasła. Myślałem, żeby zrobić coś w tym stylu:
using System;

namespace ConsoleApplication {
    class Program {
        static void Main(string[] args) {
            ConsoleKeyInfo Key;
            int numberOfChars = 0;

            Console.WriteLine("Input username");
            string userName = Console.ReadLine();

            Console.WriteLine("Input password");
            string passWord = Console.ReadLine();

            Console.Clear();
            Console.WriteLine("Username: {0}", userName + "\nEnter a password");


            do {

                Key = Console.ReadKey(true);
                if ((Key.Key == ConsoleKey.A)) { numberOfChars++; }

                Console.Clear();
                Console.WriteLine("Username: {0}", userName + "\nEnter a password");

                for (int i = 0; i < numberOfChars; i++) {
                    Console.Write("*");
                }
            }
            while (Key.Key != ConsoleKey.Enter);
        }
    }
}

Tylko, że teraz:
  • reaguje tylko na wciśnięcie 'A' (to tak dla przykładu), i nie mam pojęcia co zrobić, żeby reagowało na wszystkie znaki z klawiatury,
  • jeszcze muszę zapisać jakoś do stringa to podane hasło, a również nie mam pomysłu jak to zrobić

Chyba, że ktoś ma lepszy pomysł jak coś takiego napisać?
Z góry dzięki za pomoc

EDIT: Okej, trochę pomyślałem, i mam: using System;

namespace ConsoleApplication {
    class Program {
        static void Main(string[] args) {
            ConsoleKeyInfo Key;
            int numberOfChars = 0;

            Console.WriteLine("Input username");
            string userName = Console.ReadLine();

            Console.WriteLine("Input password");
            string passWord = Console.ReadLine();

            Console.Clear();
            Console.WriteLine("Username: {0}", userName + "\nEnter a password");

            Key = Console.ReadKey(true);

            if(Key.Key == ConsoleKey.Enter) {
                return;
            } else {
                while (Key.Key != ConsoleKey.Enter)
                {
                    Key = Console.ReadKey(true);
                    numberOfChars++;

                    Console.Clear();
                    Console.WriteLine("Username: {0}", userName + "\nEnter a password");

                    for (int i = 0; i < numberOfChars; i++)
                    {
                        Console.Write("*");
                    }
                }
            }
        }
    }
}
Jednak dalej to nie wszystko i nie działa idealnie... :/
 

RafalBudzis

RafalBudzis

Użytkownicy
posty1967
Propsy808
ProfesjaSkrypter
  • Użytkownicy
W pętli pobierasz znak po znaku i zapisujesz jaki problem ?

http://msdn.microsoft.com/pl-pl/library/471w8d85(v=vs.110).aspx

a stringa zapisujesz normalnie
string s0 = '';
[...]
var klawisz = Console.ReadKey(false);
s0 = s0 + klawisz.Key.ToString();
[...]

MajkeI

MajkeI

Użytkownicy
Front End Developer
posty698
Propsy169
Profesjabrak
  • Użytkownicy
  • Front End Developer
Ok, teraz już zapisuje, ale tylko w wielkich literach
using System;
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string inputed = "";
            int length = 0;
            Console.WriteLine("Input username");
            string userName = Console.ReadLine();
            Console.WriteLine("Input password");
            string passWord = Console.ReadLine();
            Console.Clear();
            Console.WriteLine("Username: {0}", userName + "\nEnter a password");
            while (true)
            {
                var klawisz = Console.ReadKey(false);
                Console.Clear();
                Console.WriteLine("Username: {0}", userName + "\nEnter a password");
                if (klawisz.Key == ConsoleKey.Enter)
                {
                    break;
                }
                inputed += klawisz.Key.ToString();
                length++;
                for(int i = 0; i < length; i++)
                {
                    Console.Write("*");
                }
            }
            Console.Clear();
            Console.WriteLine(inputed);
            Console.Read();
        }
    }
}

Co z tym zrobić?
 


0 użytkowników i 1 Gość przegląda ten wątek.
0 użytkowników
Do góry