c# - Creating an int inside an if/else and using it later -
I am playing with the stuff here and I am having trouble with anything.
I have an input; It can be either a number or a letter I have to check whether it is a number or a letter. So I used a if
for it, if input is a number, then my code should be an int. If this is a letter, then it should be made of a different int but I can not use the integer later for some reasons. Any way to solve this?
console.light line ("length (ms)"); String I = console Readline (); Int I2 = Int32.Parse (I); Console.light line ("height: r for random"); String L = console Readline (); // so it asks for an input, for which I want to check here what it is (L! = "R") {int he = int32.Parse (L); } Else {Random Hi = New Random (); Int he = hi Next (1, 50); } // ---------------------- I want to use ints here while (true) {random R = new random (); Random R2 = New Random (); Int H = R2 Next (1, he); Int RH = H * 100; Console; light line ("height {0}", h); Console.Beep (RH, I2);
You need to adjust the scope of int
Take it out of conditional blocks.
int he; If (l! = "R") {he = int 32.pers (l); } Else {Random Hi = New Random (); He = Hi Next (1, 50); }
You can also use the code in this example, which will be better as a matter of style.
int he = l! = "R"? Int32.Parse (L): (new random ()) Next (1, 50);
One thing that is remarkable, about both the above versions, it is that by creating many exceptions based on the format of Int32.Parse
String L
which you might want to handle using statements or by using the method
Comments
Post a Comment