C
C#2y ago
Mekasu0124

✅ How To Return 2 numbers inside of an If/Else statement

using System;
using System.Threading;

namespace MeksMathGame
{
class Program
{

public void Main(string[] args)
{
List<string> welcomeText = new()
{
"Welcome To Mek's Math Game!",
"Some Rules Of Thumb To Remember While Playing:",
"1) All Answers Are In The Form Of An Integer.",
"2) If Your Division Answer Gives You A Decimal Like: 0.86, Then Your Answer Is 0",
"3) This Game Has 5 Difficulties: Easy, Medium, Hard, Expert, and Master.",
"4) None Of The Numbers You Will Encounter On Any Difficulty Will Go Higher Than 100.",
"\n",
"When You Are Ready To Begin, Enter 'Yes' To Start, Or 'No' To Exit."
};

for (int i = 0; i < welcomeText.Count; i++)
{
Console.WriteLine(welcomeText[i]);
};

string userReady = Console.ReadLine();

if (userReady.ToLower() == "yes")
{
MainMenu mainMenu = new MainMenu();
mainMenu.GameSelection();
}
else if (userReady.ToLower() == "no")
{
Console.WriteLine("Exiting Game. Please Wait.");
Thread.Sleep(2000);
Environment.Exit(0);
}
else
{
Console.WriteLine("Invalid Operation. Please Enter 'Yes' To Start The Game or Enter 'No' To Exit The Game.");
Thread.Sleep(1000);
Program program = new Program();
program.Main();
}
}
}
}
using System;
using System.Threading;

namespace MeksMathGame
{
class Program
{

public void Main(string[] args)
{
List<string> welcomeText = new()
{
"Welcome To Mek's Math Game!",
"Some Rules Of Thumb To Remember While Playing:",
"1) All Answers Are In The Form Of An Integer.",
"2) If Your Division Answer Gives You A Decimal Like: 0.86, Then Your Answer Is 0",
"3) This Game Has 5 Difficulties: Easy, Medium, Hard, Expert, and Master.",
"4) None Of The Numbers You Will Encounter On Any Difficulty Will Go Higher Than 100.",
"\n",
"When You Are Ready To Begin, Enter 'Yes' To Start, Or 'No' To Exit."
};

for (int i = 0; i < welcomeText.Count; i++)
{
Console.WriteLine(welcomeText[i]);
};

string userReady = Console.ReadLine();

if (userReady.ToLower() == "yes")
{
MainMenu mainMenu = new MainMenu();
mainMenu.GameSelection();
}
else if (userReady.ToLower() == "no")
{
Console.WriteLine("Exiting Game. Please Wait.");
Thread.Sleep(2000);
Environment.Exit(0);
}
else
{
Console.WriteLine("Invalid Operation. Please Enter 'Yes' To Start The Game or Enter 'No' To Exit The Game.");
Thread.Sleep(1000);
Program program = new Program();
program.Main();
}
}
}
}
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
Mekasu0124
Mekasu0124OP2y ago
nevermind. I found a work around
Jimmacle
Jimmacle2y ago
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
Mekasu0124
Mekasu0124OP2y ago
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.
class NumberGenerator
{
public int GenerateNumber(string difficulty)
{
Random numberGen = new Random();
// this won't let me return both numbers
if (difficulty == "easy")
{
int num1 = numberGen(1,21);
int num2 = numberGen(1,21);
return num1, num2;
}
}
}
class NumberGenerator
{
public int GenerateNumber(string difficulty)
{
Random numberGen = new Random();
// this won't let me return both numbers
if (difficulty == "easy")
{
int num1 = numberGen(1,21);
int num2 = numberGen(1,21);
return num1, num2;
}
}
}
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?
Pobiega
Pobiega2y ago
it has to do with your method signature public int GenerateNumber(string difficulty) can only ever return a single int
Mekasu0124
Mekasu0124OP2y ago
yea I just found the stack overflow
public (int, int) GenerateNumber(difficulty)
{
return (num1, num2);
}
public (int, int) GenerateNumber(difficulty)
{
return (num1, num2);
}
right?
Pobiega
Pobiega2y ago
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>
SG97
SG972y ago
what is the purpose of GenerateNumber
Mekasu0124
Mekasu0124OP2y ago
to generate numbers based off a difficulty level as shown here it's an if/elif/else based off 5 difficulty levels
SG97
SG972y ago
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?
Mekasu0124
Mekasu0124OP2y ago
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.
SG97
SG972y ago
you could just call GenerateNumber twice but I guess it's a matter of taste
Mekasu0124
Mekasu0124OP2y ago
I 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.
cap5lut
cap5lut2y ago
u shouldnt create a new Random each time u call the method, store the instance and reuse it or use Random.Shared
Mekasu0124
Mekasu0124OP2y ago
no. it calls new random numbers on each iteration of the for loop which means it calls new random numbers for each question
cap5lut
cap5lut2y ago
public class Program
{
private static readonly Random rng = new();

public static void Main()
{
for (var i = 0; i < 100; i++)
{
Console.WriteLine(rng.Next(1, 21));
}
}
}
public class Program
{
private static readonly Random rng = new();

public static void Main()
{
for (var i = 0; i < 100; i++)
{
Console.WriteLine(rng.Next(1, 21));
}
}
}
that gives ya 100 random numbers
Mekasu0124
Mekasu0124OP2y ago
static void Game(string difficulty, int numOfQuestions, string gameType)
{
Random numberGen = new Random();

switch (difficulty)
{
case "easy":
for (int i = 0; i < numOfQuestions; i++)
{
int num1easy = numberGen.Next(0, 21);
int num2easy = numberGen.Next(0, 21);

switch (gameType)
{
case "+":
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("What Is The Summation Of: " + num1easy + "+" + num2easy + "\n");

int userSolutionAdd = Convert.ToInt32(Console.ReadLine());

CheckSolution(num1easy, gameType, num2easy, userSolutionAdd);
break;

case "-":
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("What Is The Difference Of: " + num1easy + "-" + num2easy + "\n");

int userSolutionSub = Convert.ToInt32(Console.ReadLine());

CheckSolution(num1easy, gameType, num2easy, userSolutionSub);
break;
static void Game(string difficulty, int numOfQuestions, string gameType)
{
Random numberGen = new Random();

switch (difficulty)
{
case "easy":
for (int i = 0; i < numOfQuestions; i++)
{
int num1easy = numberGen.Next(0, 21);
int num2easy = numberGen.Next(0, 21);

switch (gameType)
{
case "+":
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("What Is The Summation Of: " + num1easy + "+" + num2easy + "\n");

int userSolutionAdd = Convert.ToInt32(Console.ReadLine());

CheckSolution(num1easy, gameType, num2easy, userSolutionAdd);
break;

case "-":
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("What Is The Difference Of: " + num1easy + "-" + num2easy + "\n");

int userSolutionSub = Convert.ToInt32(Console.ReadLine());

CheckSolution(num1easy, gameType, num2easy, userSolutionSub);
break;
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
cap5lut
cap5lut2y ago
for that its looking good already 🙂
Mekasu0124
Mekasu0124OP2y ago
ty ❤️ Hi. Question. I currently have
if (0 < userDesiredQuestionAmount <= 100)
{
// do something
}
if (0 < userDesiredQuestionAmount <= 100)
{
// do something
}
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
if (userDesiredQuestionAmount > 0 | userDesiredQuestionAmount < 100)
{
// do something
}
if (userDesiredQuestionAmount > 0 | userDesiredQuestionAmount < 100)
{
// do something
}
so I'm lost
cap5lut
cap5lut2y ago
the 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 '&&'
Mekasu0124
Mekasu0124OP2y ago
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)??
cap5lut
cap5lut2y ago
almost like ur second approach, if (userDesiredQuestionAmount > 0 && userDesiredQuestionAmount < 100)
Mekasu0124
Mekasu0124OP2y ago
ok bet. tyvm
Want results from more Discord servers?
Add your server