c# - Adjusting the console apliacation (trycatch and while loop) -
so in code i'm trying make if user inputs value out of range display message:
enter valid input (between 1 , 2^53)
what doing @ moment when input letter message appears, when input number lower 0, resets loop , continues if nothing happened.
//variables double length, width, totalarea, totallength; const double feet = 3.75; //questions console.title = "double glazing window calculator"; console.writeline("double glazing calculator\n"); bool inputfalse = false; { try { { console.write("enter height of of window in meteres "); length = double.parse(console.readline()); console.write("enter width of of window in meteres "); width = double.parse(console.readline()); } while (length < 1 || width < 1); //maths totalarea = length * width * 2; totallength = (length * 2 + width * 2) * feet; console.writeline("the total area of glass required in m^2 (to 2 decinmal places) {0} ", totalarea.tostring("0.##")); console.writeline("the total length of wood required in feet (to 2 decimal places) {0}", totallength.tostring("0.##")); } catch { inputfalse = (true); console.writeline("enter valid input (between 1 , 2^53)"); } } while (true);
instead of catching exception double.parse
better use double.tryparse
. , since want values @ least 1 can use fact double.tryparse
set out
parameters 0 when parsing fails.
double length = 0, width = 0; const double feet = 3.75; //questions console.title = "double glazing window calculator"; console.writeline("double glazing calculator\n"); while (true) { console.write("enter height of of window in meteres "); double.tryparse(console.readline(), out length); console.write("enter width of of window in meteres "); double.tryparse(console.readline(), out width); if (length < 1 || width < 1) { console.writeline("enter valid input (between 1 , 2^53)"); } else { break; } } //maths var totalarea = length * width * 2; var totallength = (length * 2 + width * 2) * feet; console.writeline("the total area of glass required in m^2 (to 2 decinmal places) {0} ", totalarea.tostring("0.##")); console.writeline("the total length of wood required in feet (to 2 decimal places) {0}", totallength.tostring("0.##"));
you split 2 while
loops doesn't let them enter width until enter valid length first.
Comments
Post a Comment