Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
This is basically my first little C# program. I built in a "rule" so you only have 20 tries to guess the correct number. I was wondering if there is a more beautiful way to implement this "rule".
This is my code:
bool run = true;
while (run == true) // Main While Schleife
{
Random rndnr = new Random();
int rnd;
rnd = rndnr.Next(1, 100);
int guess;
int trys;
trys = 0;
guess = 0;
Console.WriteLine("Can you guess my number? I am thinking about a number between 1 and 100!");
guess = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(rnd);
while (guess != rnd)
{
if (trys > 20)
{
Console.Clear();
Console.WriteLine("You only have 20 trys. You failed! \n");
run = false;
Console.WriteLine("Do you wann play again? Press R + Enter to play again!");
string rerun = Console.ReadLine().ToUpper();
if (rerun == "R")
{
run = true;
trys = 0;
Console.Clear();
break;
}
else
{
run = false;
break;
}
}
if (guess > rnd)
{
Console.WriteLine("My number is lower than" + guess + "\n guess again >>");
guess = Convert.ToInt32(Console.ReadLine());
}
if (guess < rnd)
{
Console.WriteLine("My number is higher than" + guess + "\n guess again >>");
guess = Convert.ToInt32(Console.ReadLine());
}
if (guess == rnd)
{
Console.Write("You guessed right my number is" + rnd + "and you needed " + trys + "trys");
run = false;
Console.WriteLine("Do you wann play again? Press R + Enter to play again!");
string rerun = Console.ReadLine().ToUpper();
if (rerun == "R")
{
run = true;
trys = 0;
Console.Clear();
}
else
{
run = false;
break;
}
}
trys++;
}
}
Console.Clear();
Console.WriteLine("Thanks for palying see you next time!");
Console.ReadLine();
}
}
I made a few bug fixes and generally tried to improve the code a bit. Hope you find it helpful:
bool run = true;
int maxTrys=20;
Random random = new Random();
while (run)
{
int rnd=random.Next(1, 101);
Console.WriteLine("Can you guess my number? I am thinking about a number between 1 and 100!");
int trys=0;
bool solved=false;
while (!solved && trys<maxTrys)
{
trys++;
int guess;
bool numericGuess;
do {
numericGuess=int.TryParse(Console.ReadLine(), out guess);
if (!numericGuess) {
Console.WriteLine("Please enter a number");
}
} while (!numericGuess);
if (guess > rnd)
{
Console.WriteLine(string.Format("My number is lower than {0}\n guess again >>", guess));
}
else if (guess < rnd)
{
Console.WriteLine(string.Format("My number is higher than {0}\n guess again >>", guess));
}
else if (guess == rnd)
{
solved=true;
Console.WriteLine(string.Format("You guessed right my number is {0} and you needed {1} trys", rnd, trys));
}
}
if (!solved) {
Console.Clear();
Console.WriteLine(string.Format("You only have {0} trys. You failed! \n", maxTrys));
}
Console.WriteLine("Do you wanna play again? Press R + Enter to play again!");
string rerun = Console.ReadLine().ToUpper();
if (!rerun.Equals("R"))
{
run = false;
}
}
Console.Clear();
Console.WriteLine("Thanks for playing see you next time!");
Console.ReadLine();
Random r = new Random();
int secretNumber = r.Next(1, 11);
int i = 0;
int chance = 5;
Console.WriteLine("Game: guess number");
while (i < chance)
{
Console.WriteLine("Your turn");
if (secretNumber == int.Parse(Console.ReadLine()))
{
Console.WriteLine("You won!");
break;
}
i++;
}
if (i == chance)
{
Console.WriteLine("You lost!");
Console.WriteLine("The secret number is " + secretNumber);
}
Simple way
Related
I'm new at C# and am having trouble with nested loops. I'm working on a Grading program with a menu. The first menu asks the user how many grades would they like to enter. Then, the user enters the grades. The Second menu figures out the average and grade. I'm having trouble with a nested loop where it would ask you a given number of time to enter grades.
Once that is done, I'm also having trouble with how I would pass that info to the second menu to get the average. I done something like this better in Java, but there we had a set number of grades; then, we made a variable for each grade. Finally we summed them and divided by a set number.
bool exit = false;
do
{
Console.WriteLine("1. Enter Grades");
Console.WriteLine("2. Get Average");
Console.WriteLine("3. My program");
Console.WriteLine("4. exit");
string input = Console.ReadLine();
Console.WriteLine("");
if (input == "1")
{
int totalGrades = 0;
double grades;
double grade, finalGrade = 0;
//User Input
Console.WriteLine("How many grades do you want to enter? ");
//While loop for TryParse
while(!int.TryParse(Console.ReadLine(),out totalGrades))
{
Console.WriteLine("Please enter a valid number");
}
while (totalGrades < 1)
{
Console.WriteLine("Enter Grade: ");
string input = Console.ReadLine();
for (int i = 0; i<= totalGrades; totalGrades++)
Console.WriteLine(totalGrades);
}
Console.ReadLine();
}
else if (input == "2")
{
double average = 0;
if (average >= 90)
{
Console.WriteLine($"The average is a {average} which is an A.");
}
else if (average >= 80)
{
Console.WriteLine($"The average is a {average} which is an B.");
}
else if (average >= 70)
{
Console.WriteLine($"The average is a {average} which is an C.");
}
else if (average >= 60)
{
Console.WriteLine($"The average is a {average} which is an D.");
}
else
{
Console.WriteLine($"The average is a {average} which is an E.");
}
}
else
{
exit = true;
}
}
while (exit == false);
I've changed a few things in order to make the code easier to understand.
private static void Main(string[] args)
{
ProgramLoop();
}
private static void ProgramLoop()
{
var grades = new List<double>();
double average;
var exit = false;
do
{
System.Console.WriteLine("1. Enter Grades");
System.Console.WriteLine("2. Get Average");
System.Console.WriteLine("3. My program");
System.Console.WriteLine("4. exit");
var input = System.Console.ReadLine();
System.Console.WriteLine("");
switch (input)
{
case "1":
grades = EnterGrades();
break;
case "2":
average = GetAverage(grades);
break;
case "3":
MyProgram();
break;
case "4":
exit = true;
break;
default:
System.Console.WriteLine($"'{input}' is not a valid choice.");
break;
}
}
while (exit == false);
}
private static List<double> EnterGrades()
{
int numberOfGrades = 0;
var grades = new List<double>();
System.Console.WriteLine("How many grades do you want to enter? ");
// Read number of grades
while (!int.TryParse(System.Console.ReadLine(), out numberOfGrades) || numberOfGrades < 1)
{
System.Console.WriteLine("Please enter a valid number");
}
while (grades.Count != numberOfGrades)
{
// Read grade
System.Console.WriteLine("Enter Grade: ");
double grade;
while (!double.TryParse(System.Console.ReadLine(), out grade) || grade < 0 || grade > 100)
{
System.Console.WriteLine("Please enter a valid grade between 0.0 and 100.0");
}
grades.Add(grade);
}
return grades;
}
private static double GetAverage(IList<double> grades)
{
var average = grades.Average();
if (average >= 90)
{
System.Console.WriteLine($"The average is {average}, which is an A.");
}
else if (average >= 80)
{
System.Console.WriteLine($"The average is {average}, which is an B.");
}
else if (average >= 70)
{
System.Console.WriteLine($"The average is {average}, which is an C.");
}
else if (average >= 60)
{
System.Console.WriteLine($"The average is {average}, which is an D.");
}
else
{
System.Console.WriteLine($"The average is {average}, which is an E.");
}
return average;
}
I would recommend you to split your code into methods. The code will be easier to understand, and it's a great practice to not cram too much code together.
If you plan on adding more functionality and write more code, I would also recommend you to look into how you can apply object oriented programming to this, i.e. writing classes like a GradeCard.
So there are a couple things I'd like to highlight and I also added notes to the code. You want to keep 2 variables throughout your program/method to keep track of everything entered. They are the 2 pieces of average (1) Total and (2) Count of input numbers. See the code and read it line by line and see the comments. Hope this helps you. Feel free to ask questions if something doesn't make sense.
bool exit = false;
// added variables outside of loop so they are available everywhere in the method
double grades = 0;
int gradesCount = 0;
do
{
Console.WriteLine("1. Enter Grades");
Console.WriteLine("2. Get Average");
Console.WriteLine("3. exit");
string input = Console.ReadLine();
Console.WriteLine("");
if (input == "1")
{
int totalGrades = 0;
//User Input
Console.WriteLine("How many grades do you want to enter? ");
//While loop for TryParse
while (!int.TryParse(Console.ReadLine(), out totalGrades))
{
Console.WriteLine("Please enter a valid number");
}
// increment the count of grades by the number of grades the user wants to add
gradesCount += totalGrades;
// variable to keep a count and avoid infinite loop
int addedGradesCount = 0;
// while loop works like a for loop using our variable to keep count of grades we add
while (addedGradesCount < totalGrades)
{
Console.WriteLine("Enter Grade: ");
// variable to store entered grade
double newGrade = 0;
//Reusing code from while loop above for TryParse
while (!double.TryParse(Console.ReadLine(), out newGrade))
{
Console.WriteLine("Please enter a valid number");
}
// increment running total of grades with the user input number
grades += newGrade;
// output to user - got rid of loop through totalGrades
Console.WriteLine("You entered: " + newGrade + " - Total: " + grades);
// increment variable to keep count! if this is not here, you will have infinite loop
addedGradesCount++;
}
// Console.ReadLine(); // not needed
}
else if (input == "2")
{
// calculate average using the method variables we initialized at the beginning
double average = (grades / gradesCount);
if (average >= 90)
{
Console.WriteLine($"The average is a {average} which is an A.");
}
else if (average >= 80)
{
Console.WriteLine($"The average is a {average} which is an B.");
}
else if (average >= 70)
{
Console.WriteLine($"The average is a {average} which is an C.");
}
else if (average >= 60)
{
Console.WriteLine($"The average is a {average} which is an D.");
}
else
{
Console.WriteLine($"The average is a {average} which is an E.");
}
}
else
{
exit = true;
}
} while (exit == false);
Console.ReadKey();
this seems to work
static void Main(string[] args)
{
bool exit = false;
List<float> grades = new List<float>();
do
{
Console.WriteLine("1. Enter Grades");
Console.WriteLine("2. Get Average");
Console.WriteLine("3. My program");
Console.WriteLine("4. exit");
Console.WriteLine("");
string input = Console.ReadLine();
Console.WriteLine("");
if (input == "1")
{
int totalGrades = 0;
//User Input
Console.WriteLine("How many grades do you want to enter? ");
while (true)
{
try
{
totalGrades = Convert.ToInt32(Console.ReadLine());
break;
}
catch (FormatException)
{
Console.WriteLine("This is not a valid number");
continue;
}
}
Console.WriteLine("");
while (totalGrades > 0)
{
while (true)
{
try
{
grades.Add(Convert.ToInt32(Console.ReadLine()));
totalGrades--;
break;
}
catch (FormatException)
{
Console.WriteLine("This is not a valid number");
continue;
}
}
}
Console.WriteLine("");
}
else if (input == "2")
{
double average = grades.Average();
if (average >= 90)
{
Console.WriteLine($"The average is a {average} which is an A.");
}
else if (average >= 80)
{
Console.WriteLine($"The average is a {average} which is an B.");
}
else if (average >= 70)
{
Console.WriteLine($"The average is a {average} which is an C.");
}
else if (average >= 60)
{
Console.WriteLine($"The average is a {average} which is an D.");
}
else
{
Console.WriteLine($"The average is a {average} which is an E.");
}
Console.WriteLine("");
}
else if (input == "4")
{
exit = true;
} else
{
Console.WriteLine("This is not an option");
}
}
while (exit == false);
}
I am writing a C# program that prints "true" if number <= 20 and "false" if number > 20 using a while loop but the program keeps on executing.
I want to break the program if it reaches certain number e.g. number > 26.
The code for the program is:
public static void Main()
{
Console.WriteLine("Please enter a number");
int numnber = Convert.ToInt32(Console.ReadLine());
while (numnber <= 20)
{
Console.WriteLine("True");
Console.ReadLine();
int number1 = numnber++;
while (numnber > 20)
{
Console.WriteLine("False");
Console.ReadLine();
}
}
}
you can just use break; Try this:
Console.WriteLine("Please enter a number");
int numnber = Convert.ToInt32(Console.ReadLine());
while (numnber <= 20)
{
Console.WriteLine("True");
Console.ReadLine();
int number1 = numnber++;
while (numnber > 20)
{
Console.WriteLine("False");
Console.ReadLine();
}
break;
}
But this programming is not good at all because you are breaking loop after all for this simply you can use if else like this..
Console.WriteLine("Please enter a number");
int numnber = Convert.ToInt32(Console.ReadLine());
if(numnber <= 20)
{
Console.WriteLine("True");
}
else
{
Console.WriteLine("False");
}
According to your description, your major issue should be
the program keeps on executing
However, it's not going to reach 26 as #King King mentioned.
It stuck inside the inner while which should be replaced by if condition.
Lastly, if you have to exec the number check at least one time you can use do while
do
{
if (numnber <= 20)
{
Console.WriteLine("True");
}
else if (numnber > 20)
{
Console.WriteLine("False");
}
numnber++;
Console.WriteLine("number: {0}", numnber);
} while (numnber < 26);
Console.ReadLine();
Thank you all but the solution to the problem would be. The code is as follows:
public static void Main()
{
int number;
do
{
Console.WriteLine("Please enter a number");
number = Convert.ToInt32(Console.ReadLine());
do
{
Console.WriteLine("True value. True value is supplied");
Console.ReadLine();
number++;
while (number >= 21 && number < 27)
{
Console.WriteLine("False value. False value is supplied");
Console.ReadLine();
number++;
}
} while (number <= 20);
} while (number > 27);
}
I'm on my first week of studying C#.
I guess there should be some way to use 1 "While()" condition instead of 2 in the code below. Is there any way to make my code more simple:
/* ask the user to guess a number.
any number between 10 and 20 is the RIGHT choice,
any other number outside of that scope is WRONG. */
int num;
Console.WriteLine("[Q] Quit or make your choice");
string answer = Console.ReadLine();
if (answer == "Q" || answer == "q")
Console.WriteLine();
else
{
num = Convert.ToInt32(answer);
while (num < 10)
{
Console.WriteLine("Wrong, try again");
num = Convert.ToInt32(Console.ReadLine());
}
while (num > 20)
{
Console.WriteLine("Wrong, try again");
num = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Your number is {0} and it's RIGHT", num);
Console.ReadKey();
}
You can use the OR operator to combine both conditions:
/* ask the user to guess a number.
any number between 10 and 20 is the RIGHT choice,
any other number outside of that scope is WRONG. */
int num;
Console.WriteLine("[Q] Quit or make your choice");
string answer = Console.ReadLine();
if (answer == "Q" || answer == "q")
Console.WriteLine();
else
{
num = Convert.ToInt32(answer);
while (num < 10 || num > 20)
{
Console.WriteLine("Wrong, try again");
num = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Your number is {0} and it's RIGHT", num);
Console.ReadKey();
}
I have written a guess the number game between 1-100.
This is my code..
class Program
{
static void Main(string[] args)
{
while (true)
{
int randno = Newnum(1, 101);
int count = 1;
while (true)
{
Console.Write("Guess a number between 1 and 100, or press 0 to quit: ");
int input = Convert.ToInt32(Console.ReadLine());
if (input == 0)
return;
else if (input < randno)
{
Console.WriteLine("Unlucky, that number is too low - have another go!");
++count;
continue;
}
else if (input > randno)
{
Console.WriteLine("Unlucky, that number is too high - have another go!");
++count;
continue;
}
else
{
Console.WriteLine("Well done - you guessed it! The number was {0}.", randno);
Console.WriteLine("It took you {0} {1}.\n", count, count == 1 ? "attempt" : "attempts to guess it right");
break;
}
}
}
}
static int Newnum(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
}
How can I edit it so that if a user gets close to the number, say within 5 numbers, they are greeted with a message saying they're close?
You can use Math.Abs:
int diff = Math.Abs(input - randno);
if(diff <= 5)
{
// say him that he's close
}
Basically I've created a times table app. At the end of the app, I've been able to say how many times I got it wrong, and how many times I got it right.
However I want it to say:
You got:
6 x 4 wrong 6 times.
5 x 2 wrong 9 times.
etc...
Rather than you got 20 correct, and 8 wrong.
So I can see the specific multiplications I got wrong. I know where I need to add the code (under the else statement). But not sure how to go about this.
I thought the best solution would be storing all the wrongs as a string in an array, then counting identical strings and outputting a number. But I have no idea how to do this.
Here's my code:
namespace TimesTablesGame
{
class multiplication
{
Random rng = new Random();
int randomNumber;
int randomNumberTables;
int c = 0;
int w = 0;
int numberOfGos = 0;
int minRangeTables;
int maxRangeTables;
int minRange;
int maxRange;
public multiplication()
{
start:
Console.Clear();
Console.WriteLine("\nPlease enter the lowest number range you would like to practice your times tables on: ");
minRangeTables = int.Parse(Console.ReadLine());
Console.WriteLine("\nPlease enter the higest number range you would like to practice your times tables on: ");
maxRangeTables = int.Parse(Console.ReadLine());
Console.WriteLine("\nPlease enter the number of times you would like play: ");
numberOfGos = int.Parse(Console.ReadLine());
Console.WriteLine("\nPlease enter the minimum range you would like to multiply by: ");
minRange = int.Parse(Console.ReadLine());
Console.WriteLine("\nPlease enter the maximum range you would like to multiply by: ");
maxRange = int.Parse(Console.ReadLine());
repeat:
Console.Clear();
for (int i = 1; i <= numberOfGos; i++)
{
randomNumberTables = rng.Next(minRangeTables, maxRangeTables + 1);
randomNumber = rng.Next(minRange, maxRange + 1);
Console.Write("\n\n{0}: {1} x {2} = ", i, randomNumberTables, randomNumber);
if (randomNumberTables * randomNumber == int.Parse(Console.ReadLine()))
{
Console.WriteLine("Correct");
c++;
}
else
{
Console.WriteLine("Wrong it is: " + randomNumberTables * randomNumber);
w++;
}
}
Console.WriteLine("\nYou were correct {0} times, and wrong {1} times.", c, w);
Console.ReadLine();
Console.WriteLine("Would you like to play again? Type y for Yes with new settings, r for repeat using last settings, and any other key to exit.");
char again = Console.ReadKey().KeyChar;
if (again == 'y')
{
c = 0;
w = 0;
goto start;
}
else if (again == 'r')
{
c = 0;
w = 0;
goto repeat;
}
}
}
}
I'd keep track in a Dictionary....
Resisting the urge to re-write the GOTO stuff.....here's a relatively simple way to do it:
Dictionary<string, int> wrongs;
//Console questions...
//Begin looping logic
wrongs = new Dictionary<string, int>();
for (int i = 1; i <= numberOfGos; i++)
{
randomNumberTables = rng.Next(minRangeTables, maxRangeTables + 1);
randomNumber = rng.Next(minRange, maxRange + 1);
string eq = String.Format("{0} x {1} = ", randomNumberTables, randomNumber);
Console.Write("{0}: " + eq, i);
if (randomNumberTables * randomNumber == int.Parse(Console.ReadLine()))
{
Console.WriteLine("Correct");
c++;
}
else
{
Console.WriteLine("Wrong it is: " + randomNumberTables * randomNumber);
if (wrongs.Any(x => x.Key == eq))
{
wrongs[eq]++;
}
else
{
wrongs.Add(eq, 1);
}
w++;
}
}
Console.WriteLine("\nYou were correct {0} times, and wrong {1} times.", c, w);
Console.WriteLine("\n\nYou got:");
foreach (var item in wrongs)
{
Console.WriteLine("{0} wrong {1} times", item.Key, item.Value);
}
//Your logic to repeat/restart
Also....note that this does not count "2 x 4" and "4 x 2" as the same equation....
Also also....You can easily do this without using GOTO....please consider it.