Break loops using while statement - c#

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);
}

Related

Why does my 'averageScore' come out as 0?

why does my code not calculate an average score when entering "-1" into the console? It comes up at 0. It's a part of a loop exercise, so I'm sure there are faster ways to code this. I want to fix it within my current C# comprehension.
Here's the task
using System;
namespace Challenge_Loops1
{
internal class Program
{
static void Main(string[] args)
{
string individualScore = "0";
int scoreCount = 0;
int totalScore = 0;
int individualScoreIntoInt = 0;
while (individualScore != "-1")
{
Console.WriteLine($"Last number was {individualScoreIntoInt}");
Console.WriteLine("Please enter the next score");
Console.WriteLine($"Current amount of entries: {scoreCount}");
Console.WriteLine("Enter '-1' when you're ready to calculaate the average");
individualScore = Console.ReadLine();
if (individualScore.Equals("-1"))
{
Console.WriteLine("--------------------------------------------");
double averageScore = (double)totalScore / (double)scoreCount;
Console.WriteLine($"The average total score is {averageScore}");
if(int.TryParse(individualScore, out individualScoreIntoInt) && individualScoreIntoInt > 0 && individualScoreIntoInt < 21)
{
totalScore += individualScoreIntoInt;
//longer way: totalScore = individualScoreIntoInt + totalScore;
}
else if(individualScoreIntoInt < 0 || individualScoreIntoInt < 20)
{
Console.WriteLine("Enter a score > 0 and < 21");
continue;
}
else
{
Console.WriteLine("Please only enter numbers");
}
}
scoreCount++; // adding the individualscore entered to the count. writing it here so that it's only
//added to the count if it meets the requirements
}
}
}
}
Order of operations was incorrect:
1st validate if it's -1 or not,
2nd parse value and if it's possible perform below operations, if not drop error.
This was logic issue, rather than code itself.
You had added iteration despite exceptions, you didn't include possibility of 21 etc.
namespace Challenge_Loops1
{
internal class Program
{
static void Main(string[] args)
{
string individualScore = "0";
int scoreCount = 0;
int totalScore = 0;
int individualScoreIntoInt = 0;
while (individualScore != "-1")
{
Console.WriteLine($"Last number was {individualScoreIntoInt}");
Console.WriteLine("Please enter the next score");
Console.WriteLine($"Current amount of entries: {scoreCount}");
Console.WriteLine("Enter '-1' when you're ready to calculaate the average");
individualScore = Console.ReadLine();
if (individualScore.Equals("-1"))
{
Console.WriteLine("--------------------------------------------");
double averageScore = (double)totalScore / (double)scoreCount;
Console.WriteLine($"The average total score is {averageScore}");
}
else if (int.TryParse(individualScore, out individualScoreIntoInt))
{
if(individualScoreIntoInt > 0 && individualScoreIntoInt <= 21)
{
totalScore += individualScoreIntoInt;
scoreCount++;
}
//as mentioned in comment else here would also work, it's unnecessary to add any other validation.
else if (individualScoreIntoInt < 0 || individualScoreIntoInt > 21)
{
Console.WriteLine("Enter a score > 0 and < 21");
}
}
else
{
Console.WriteLine("Please only enter numbers");
}
}
}
}
}

C# While loop only returns one iteration regardless of input

Trying to write a simple set of code that converts from fahrenheit into celsius, and there are three set conditions that determines what happens. Either it's too cold, just right or too hot. For some reason no matter the input, it will only reply and say it is too cold. I can't really figure out why.
Here's the code so far;
{
class Program
{
static int FahrenheitToCelsius(int fahrenheit)
{
int celsius = ((fahrenheit - 32) * 5 / 9);
return celsius;
}
static void Main(string[] args)
{
Console.WriteLine("Please enter the desired temperature: ");
int fahrenheit = Convert.ToInt32(Console.ReadLine());
int celsius = FahrenheitToCelsius(fahrenheit);
while (celsius != 75)
if (celsius < 73)
{
Console.WriteLine("Too cold! Please enter a warmer temperature.");
Console.ReadLine();
}
else if (celsius > 77)
{
Console.WriteLine("Too warm! Please enter a colder temperature.");
Console.ReadLine();
}
else if (celsius == 75)
{
Console.WriteLine("Optimal input! Begin heating up.");
break;
}
else
{
Console.WriteLine("Invalid input! Please input a temperature.");
}
}
}
}
maybe You only getting same message because you haven't change value of celsius
static int FahrenheitToCelsius(int fahrenheit)
{
int celsius = ((fahrenheit - 32) * 5 / 9);
return celsius;
}
static void Main(string[] args)
{
Console.WriteLine("Please enter the desired temperature: ");
int fahrenheit = Convert.ToInt32(Console.ReadLine());
int celsius = FahrenheitToCelsius(fahrenheit);
Console.WriteLine("C = " + celsius);
int i = 0;
while (celsius != 75) {
if (i>0)
{
int x = Convert.ToInt32(Console.ReadLine());
celsius = FahrenheitToCelsius(x);
}
if (celsius < 73)
{
Console.WriteLine("Too cold! Please enter a warmer temperature.");
}
else if (celsius > 77)
{
Console.WriteLine("Too warm! Please enter a colder temperature.");
}
else if (celsius == 75)
{
Console.WriteLine("Optimal input! Begin heating up.");
break;
}
else
{
Console.WriteLine("Invalid input! Please input a temperature.");
}
i++;
}
}
}
First, Fix your conversion. When converting from one unit to an other one should not remove the decimal value.
static double FahrenheitToCelsius(double f) => (f - 32) * 5.0 / 9.0;
Now lets talk about your if/else. In your code
T <= 163 is too cold;
T = 164,165,166,169,170 are all invalid Temperature;
T >= 171 is too hot;
There is not reason to have those invalid right in the middle of the range.
And there is no explanation on Invalid temp so just drop it.
Is there a number that can satisfy multiple of those condition?
x < 73, x > 77, x ==75...
We can safely drop all the else.
if (tempC < 73)
{
Console.WriteLine("Too cold! Please enter a warmer temperature.\n");
}
if (tempC > 77)
{
Console.WriteLine("Too warm! Please enter a colder temperature.\n");
}
if (tempC == 75)
{
Console.WriteLine("Optimal input! Begin heating up.\n");
}
Using a Do/While loop we have :
static void Main(string[] args)
{
double tempC , tempF;
do
{
Console.WriteLine("Please enter the desired temperature: ");
tempF = Convert.ToDouble(Console.ReadLine());
tempC = FahrenheitToCelsius(tempF);
Console.WriteLine($"{tempF,4:F}°F, {tempC,4:F}°C");
if (tempC < 73)
{
Console.WriteLine("Too cold! Please enter a warmer temperature.\n");
}
if (tempC > 77)
{
Console.WriteLine("Too warm! Please enter a colder temperature.\n");
}
if (tempC == 75)
{
Console.WriteLine("Optimal input! Begin heating up.\n");
}
}
while (tempC != 75);
}
Nb renamed the variable from Fahrenheit and Celsius to tempF and tempC.
Temperatur unit find is the while are : C, F, K, R, De, N, Re, Ro.
I'm not sure one can write the name of those without google.

C# Sharp Grade program using for and while

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);
}

To simplify the code (for a beginner)

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();
}

C# - I need to add code! Guess the number

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
}

Categories

Resources