How to make number Count in loop in c#? -
this simple begginer program setter , getter concept have make first enter user name , password welcomed , if enter wrong info should display invalid , 5 tries left if again enter wrong info should display 4 tries left , on , when tries on should hang program or lock screen or
using system; namespace consoleapplication4 { class program { static void main(string[] args) { demo obj = new demo(); string uname, pass; console.foregroundcolor = consolecolor.green; label1: console.clear(); console.writeline("enter username"); uname = console.readline(); console.writeline("enter password"); pass = console.readline(); obj.setname(uname); obj.setpass(pass); if (obj.getname() == "niit") { if (obj.getpass() == "1234") { console.writeline("welcome"); } } else { console.clear(); console.writeline("invalid"); console.writeline("\n \n \n try again enter y"); int n = 5; string yes = console.readline(); if (yes == "y") { while (n >= 1) { console.write(n + " tries left"); goto label1; n = --n; } } } console.readkey(); } } class demo { private string name, pass; public void setname(string name) { this.name = name; } public string getname() { return name; } public void setpass(string pass) { this.pass = pass; } public string getpass() { return pass; } } }
please suggest simple begginers code make loop work , make count down
a while loop should suffice. using boolean detect successful password entry. when entered, break out of loop. invalid attempts decrement attemptsleft int. note: haven't tried in visual studio, logic should sound, recommend debugging , stepping through test if meets criteria.
static void main(string[] args) { demo obj = new demo(); string uname, pass; console.foregroundcolor = consolecolor.green; label1: console.clear(); console.writeline("enter username"); uname = console.readline(); console.writeline("enter password"); bool successfulpassword = false; int attemptsleft = 5; while(!successfulpassword && attemptsleft > 0){ pass = console.readline(); obj.setname(uname); obj.setpass(pass); if (obj.getname() == "niit") { if (obj.getpass() == "1234") { console.writeline("welcome"); successfulpassword = true; } } else { attemptsleft--; console.clear(); console.writeline("invalid"); console.writeline("\n \n \n try again enter y"); int n = 5; string yes = console.readline(); if (yes == "y") { console.write(attemptsleft + " tries left"); } } console.readkey(); } }
Comments
Post a Comment