while loop - C# Code Repeating User Input Prompt After Calling Method -
i'm trying make program asks user inputs , gives option of restarting if user chooses unhappy choices. program works if select options , choose yes @ end if choose no, select attributes again, , select yes user prompted choose if accept again. program reads:
there attribute points!
do accept these? type 1 yes, type 2 no. if no can choose again:
1
do accept these? type 1 yes, type 2 no. if no can choose again:
1
press key continue...
here code:
while (true) { console.write("do accept these? type 1 yes, type 2 no. if no can choose again: "); areyouhappywithchoices = console.readline(); if (!uint32.tryparse(areyouhappywithchoices, out validchoices)) console.writeline("please try again. enter 1 yes , 2 no"); else if (validchoices > 2 || validchoices < 1) console.writeline("please try again. enter 1 yes , 2 no"); else if (areyouhappywithchoices == "2") chooseattributepoints(); //this method contains whole routine else break; }
so, tested code , worked me fine. seems have additional console.readline() in chooseattributepoints() method , therefore have enter choice twice after calling chooseattributepoints(). please check make sure problem.
remarks:
i restructured code bit, more readable , has less redundancies.
int validchoices; { console.writeline("do logic ......"); // replace line chooseattributepoints() method ... console.write("do accept these? type 1 yes, type 2 no. if no can choose again: "); // read input until user gets right , enters valid number ... var areyouhappywithchoices = console.readline(); while (!int.tryparse(areyouhappywithchoices, out validchoices) || validchoices < 1 || validchoices > 2) { console.writeline("please try again. enter 1 yes , 2 no"); areyouhappywithchoices = console.readline(); } } while (validchoices != 1);
Comments
Post a Comment