c# - ReadLine(); between values -


i made dice game , few moments ago asked solution here, got. made new problem , in cant seem find answer.

heres code.

using system; using system.collections.generic; using system.linq; using system.text;  namespace noppapeli {     class program     {         static void main(string[] args)         {             int pyöräytys;             int satunnainen;             int luku = 0;              random noppa = new random((int)datetime.now.ticks);              console.writeline("anna arvaus");             int.tryparse(console.readline(),out pyöräytys);              console.writeline("haettava numero on: " + pyöräytys);             console.readline();                         {                 luku++;                 satunnainen = noppa.next(1, 7);                 console.writeline("numero on: " + satunnainen);                 if (satunnainen == pyöräytys)                 {                     satunnainen = pyöräytys;                 }             } while (pyöräytys != satunnainen);              console.writeline("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");             console.writeline("haettu numero: " + pyöräytys);             console.writeline("pyöräytetty numero: " + satunnainen);             console.write("kesti " + luku + " nopan pyöräytystä saada tulos!");             console.readline();         }     } } 

the problem int.tryparse(console.readline(),out pyöräytys); needs take values between 1-6. if put 7 in there game on loop find 7 d6. there easy solution or should make dices bigger.

you need add kind of loop ensure value valid , continue looping until valid value provided.

pyöräytys = -1; // set invalid trigger loop  while (pyöräytys < 1 || pyöräytys > 6) {    console.writeline("anna arvaus");    int.tryparse(console.readline(),out pyöräytys);     if (pyöräytys < 1 || pyöräytys > 6)    {        console.writeline("invalid value, must 1-6"); // error message    } } 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -