C# How do I get out of this loop? -
how out of loop?
i wrote program checks see if corresponding places in numbers same total. console output should true or false. wrote that, , added top part interacts user, , stuck in loop. how out of it?
using system; namespace deliverablepart1 { class deliverablepart1 { static void main(string[] args) { string gameanswer; bool repeat1 = true, repeat2 = true; while (repeat1 == true) { repeat2 = true; console.writeline("would compare 2 numbers see if corresponding place same total?"); gameanswer = console.readline(); while (repeat2 == true) { if (gameanswer == "yes" || gameanswer == "yes" || gameanswer == "yes") { console.writeline("please enter 3 digit whole number"); string firstvalue = console.readline(); int firstnumber = validinteger(firstvalue); console.writeline("please enter second 3 digit whole number"); string secondvalue = console.readline(); int secondnumber = validinteger(secondvalue); repeat1 = false; repeat2 = false; } else if (gameanswer == "no" || gameanswer == "no" || gameanswer == "no") { console.writeline("okay, exiting now"); repeat1 = false; repeat2 = false; } else { console.writeline("i not understnad have said"); repeat2 = false; } void add(int firstnumber, int secondnumber) { int length1 = firstnumber.tostring().length; int length2 = secondnumber.tostring().length; string userinput; if (length1 == length2) { string answer = convert.tostring(compare(firstnumber, secondnumber, length1)); } else { userinput = "invalid user input - check number of digits next time."; console.writeline(userinput); console.readkey(); } console.readkey(); } int validinteger(string digit1) { int value = 0; string notint = "this not integer."; { bool successfullyparsed = int.tryparse(digit1, out value); if (successfullyparsed) { int firstnumber = convert.toint32(value); return value; } else { console.writeline(notint); console.readkey(); environment.exit(0); return value; } } } string compare(int a, int b, int c) { int lastdigita; int lastdigitb; lastdigita = (a % 10); lastdigitb = (b % 10); int sumstatic = lastdigita + lastdigitb; { lastdigita = (a % 10); lastdigitb = (b % 10); = / 10; b = b / 10; c--; int sumcompare = lastdigita + lastdigitb; if (sumcompare != sumstatic) { console.writeline("false"); return "false"; } } while (c != 0); console.writeline("true"); return "true"; } } } } } }
from understanding, looks want user enter in 3 ints (with input validation), , put 3 ints through compare()
function. if function returns true, "true" console, , if it's false "false" console. if that's case, refactored code (and doesn't stuck in loop):
using system; namespace deliverablepart1 { internal class deliverablepart1 { private static void main(string[] args) { console.writeline("would compare 2 numbers see if corresponding place same total?"); var shouldcontinue = console.readline(); if (shouldcontinue != null && shouldcontinue.equals("yes", stringcomparison.currentcultureignorecase)) { var firstnum = getintfromuser("please enter 3 digit whole number"); var secondnum = getintfromuser("please enter 3 digit whole number"); var thirdnum = getintfromuser("please enter 3 digit whole number"); console.writeline(compare(firstnum, secondnum, thirdnum) ? "true" : "false"); } else { console.writeline("exiting"); } // waits user press key exit app, can see result console.readkey(); } /// <summary> /// makes user enter in 3 digit number, , exits if "no" /// </summary> /// <param name="msg">the message prompt ask integer</param> /// <returns>system.int32., or exit</returns> public static int getintfromuser(string msg) { while (true) { console.writeline(msg); var valfromuser = console.readline()?.trim(); if (valfromuser != null) { int result; if (int.tryparse(valfromuser.trim(), out result) && valfromuser.length == 3) { return result; } if (valfromuser.equals("no", stringcomparison.currentcultureignorecase)) { console.writeline("exiting."); environment.exit(0); } } console.writeline("hmm, that's not 3 digit number. try again."); } } public static bool compare(int a, int b, int c) { var lastdigita = % 10; var lastdigitb = b % 10; var sumstatic = lastdigita + lastdigitb; { lastdigita = % 10; lastdigitb = b % 10; = / 10; b = b / 10; c--; var sumcompare = lastdigita + lastdigitb; if (sumcompare != sumstatic) { return false; } } while (c != 0); return true; } } }
Comments
Post a Comment