✅ How To Return 2 numbers inside of an If/Else statement
In my else part of the if/elif/else statement, I need to be able to start the program over, but where I referenced my class
Program program = new Program();
and then called the function again program.Main();
<- this line is throwing an error CS7036: There is no argument given that corresponds to the required parameter 'args' of 'Program.Main(string[])'
and I'm lost at how to start the program over, or how to give the user another chance to give their response. Thanks in advanced.22 Replies
nevermind. I found a work around
you're looking for a loop
what you're attempting to do is basically recursion and eventually, with enough invalid inputs, your program will crash by overflowing the stack
right, but I found another method of achieving the same goal. I had to break my stuff up into separate functions anyways, so I did it a bit different. I do have a different question though.
I have tried this, I have also tried putting the numbers into
int[] numbers = new int[] {num1, num2};
and I've tried List<int> numbers = new() {num1,num2);
and none of these will let me return the numbers. So how do I need to write this so that it will let me return the numbers?it has to do with your method signature
public int GenerateNumber(string difficulty)
can only ever return a single intyea I just found the stack overflow
right?
if you want to return an int array, make it
int[]
.
if you want to return a tuple with two ints, make it (int x, int y)
(you can change x and y)
if you want to return "something that has 0 or more numbers", return IEnumerable<int>
what is the purpose of
GenerateNumber
to generate numbers based off a difficulty level
as shown here
it's an if/elif/else
based off 5 difficulty levels
I still don't get it, if I provide "easy" as parameter, what do those 2 numbers represent
the range of numbers that the user is guessing?
no. ok. so it's a math game. This is just one piece of it. You select a game to play Addition, Subtraction, Multiplication or Division. Then you select a difficulty Easy, Medium, Hard, Expert, Master and then you tell the game how many questions you want to answer. From there, it then launches the desired game with the wanted difficulty. The difficulty is setup to generate 2 random numbers based on what difficulty level you chose. Easy is 1-20, Medium is 20-40, Hard is 40-60, Expert is 60-80 and Master is 80-100. " It then shows the 2 random numbers generated from the selected difficulty to you with the appropriate math symbol and requests the users answer. User inputs answer and it tells you right or wrong. " It repeats what in the quotes until you've reached the number of questions you wanted to answer. From there, the game ends with the option to play again or not.
you could just call
GenerateNumber
twice
but I guess it's a matter of tasteI edited my above message, and yes this helps me keep track of things more easily as I'm not advanced whatsoever so I keep it as simple as possible, or what I consider to be simple. It's a game geared for kids, grades 1-8. Once I get these 4 basic math operations complete and the game is ready to go for version one, I'll start on version two which will start to incorporate algebra (Various types), derivatives, and more as there are different levels of math and different types of math. As I start with version 2, the game menu will change. I'll be adding in an option for the user to select whether they want elementary, middle, high school math or college (freshman, sophmore, junior, senior, masters, and ph.d) levels as well.
u shouldnt create a new
Random
each time u call the method, store the instance and reuse it
or use Random.Shared
no. it calls new random numbers on each iteration of the for loop which means it calls new random numbers for each question
that gives ya 100 random numbers
I'm currently in a re-write phase as I'm breaking the games up into individual classes, but this is the old write of the current logic. New random numbers are called on each iteration of the for loop which counts up to the number of questions the user wants to answer
understandable, and I appreciate the code example, but I'm no where near advanced enough to even know that existed. I'm literally a 3 day in beginner, and I'm following a class and this is the first project
for that its looking good already 🙂
ty ❤️
Hi. Question. I currently have
but this is telling me that I can't put an operator '<-' cannot be applied to operands of type 'bool' and 'int' but the users input is
int userDesiredQuestionAmount = Convert.ToInt32(Console.ReadLine());
so I'm confused. How am I supposed to write this if statement?
because I can get away with it if I write it like this
so I'm lostthe logical or is
||
|
is a bitwise or, so 0b0010 | 0b0001
would result in 0b0011
(0b...
is the bitwise notation of an integer number, u could also say 1 | 2
would result in '3')
same principle for the ands '&' and '&&'so how would I get it to understand that I'm wanting to run one check. If
0 < userInput < 100
so like if I chose to answer 5 questions, the if statement would read if (0 < 5 < 100)
??almost like ur second approach,
if (userDesiredQuestionAmount > 0 && userDesiredQuestionAmount < 100)
ok bet. tyvm