Adding switch case - c#

I want to add switch case to not allow the user to write string when entering temperature or when there is nothing to delete it says "there is nothing to delete, go back to menu".
List<string> Temp = new List<string>();
while (true)
{
string val;
Console.WriteLine("[L] ägg till temp-mätning: ");
Console.WriteLine("[S] kriv ut alla temperaturer och medeltemperatur");
Console.WriteLine("[T] ag bort temp-mätning");
Console.WriteLine("[A] vsluta");
Console.Write("Selection: ");
val = Console.ReadLine();
if (val == "l" || val == "L")
{
Console.WriteLine("add temperature : ");
Temp.Add(Console.ReadLine());
Console.Clear();
}
else if(val == "s" || val == "S")
{
int index = 1;
Console.Clear();
Console.WriteLine($"Your temperatures are: ");
Temp.ForEach(x => Console.WriteLine($"{index++} - {x}"));
}
else if (val == "t" || val == "T")
{
Console.Write($"Which temp do you want to delete [index from 1 to {Temp.Count}]: ");
int deleteIndex = int.Parse(Console.ReadLine()) - 1;
Temp.RemoveAt(deleteIndex);
}
else
{
Console.WriteLine("incorrect input: ");
Console.Clear();
break;
}

To control use input you can extract methods, e.g.
private static int ReadInteger(string title) {
while (true) {
if (!string.IsNullOrWhiteSpace(title))
Console.WriteLine(title);
if (int.TryParse(Console.ReadLine(), out int result))
return result;
Console.WriteLine("Incorrect syntax, please, try again.");
}
}
then you can put
val = Console
.ReadLine()
.Trim() // Let's be nice and tolerate leading / trailing spaces, e.g. " L "
.ToUpper();
val = val.Substring(0, Math.Max(1, val.Length));
switch (val) {
case "L":
// We read valid integer, turn it to string and out to Temp
Temp.Add(ReadInteger("add temperature : ").ToString());
Console.Clear();
break;
case "T":
int deleteIndex = ReadInteger(
"$"Which temp do you want to delete [index from 1 to {Temp.Count}]: ");
if (deleteIndex >= 0 && deleteIndex < Temp.Count)
Temp.RemoveAt(deleteIndex);
else
Console.WriteLine("Index out of range");
break;
...
}

Please check C# reference websites or books before asking questions.
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/
Here is the code you wanted, hope this helps you:
List<string> Temp = new List<string>();
while (true)
{
menu:
string val = string.Empty;
Console.WriteLine("[L] ägg till temp-mätning: ");
Console.WriteLine("[S] kriv ut alla temperaturer och medeltemperatur");
Console.WriteLine("[T] ag bort temp-mätning");
Console.WriteLine("[A] vsluta");
Console.Write("Selection: ");
val = Console.ReadLine();
switch (val.ToLower())
{
case "l":
addTemperature:
Console.WriteLine("add temperature : ");
string temperatureInput = Console.ReadLine();
int temperatureToAddToList;
try
{
temperatureToAddToList = Convert.ToInt32(temperatureInput); //This line trys to convert string variables to integer variables. If string variable includes any character, it will throw an exception.
}
catch (Exception error) //If an exception was thrown, this code block gets activated, which will give out the message you asked for.
{
Console.Clear();
Console.WriteLine("Please enter a number instead of a string!");
goto addTemperature;
}
Temp.Add(temperatureInput.Trim());
Console.Clear();
break;
case "s":
int index = 1;
Console.Clear();
Console.WriteLine($"Your temperatures are: ");
Temp.ForEach(x => Console.WriteLine($"{index++} - {x}"));
break;
case "t":
if (Temp.Count == 0)
{
Console.Clear();
Console.WriteLine("There is nothing to delete, go back to menu.");
goto menu;
}
else
{
Console.Write($"Which temp do you want to delete [index from 1 to {Temp.Count}]: ");
int deleteIndex = int.Parse(Console.ReadLine()) - 1;
Temp.RemoveAt(deleteIndex);
break;
}
default:
Console.WriteLine("incorrect input: ");
Console.Clear();
break;
}
I have revised and updated my code example to better solve your problem.

Related

Calculator Transforming Coulomb units freezes unexpletedly

internal class Program
{
static void Main(string[] args)
{
double result = 0;
Console.WriteLine("Welcome to the Physics calculator!");
Console.WriteLine("Please choose one of the following options");
Console.WriteLine("1.Unit Conversions");
Console.WriteLine();
Console.WriteLine("What is the current form of the number you are trying to convert?");
Console.WriteLine("1.Coulomb");
Console.WriteLine("2.mCoulomb");
Console.WriteLine("3.μCoulomb");
Console.WriteLine("4.nCoulomb");
Console.WriteLine("5.pCoulomb");
string answer1 = Console.ReadLine();
double answerbase = 0;
double answerpow = 0;
switch (answer1)
{
case "1":
Console.WriteLine("Please continue to the next part");
answerbase = Convert.ToDouble(Console.ReadLine());
break;
case "2":
Console.WriteLine("Please enter the number: ");
answerbase = Convert.ToDouble(Console.ReadLine());
break;
case "3":
Console.WriteLine("Please enter the number: ");
answerbase = Convert.ToDouble(Console.ReadLine());
break;
case "4":
Console.WriteLine("Please enter the number: ");
answerbase = Convert.ToDouble(Console.ReadLine());
break;
case "5":
Console.WriteLine("Please enter the number: ");
answerbase = Convert.ToDouble(Console.ReadLine());
break;
default:
Console.WriteLine("That is not a valid option!");
break;
}
Console.WriteLine("In which for would you like to convert your number?");
Console.WriteLine("Choose one of the following options");
Console.WriteLine("1.Coulomb");
Console.WriteLine("2.mCoulomb");
Console.WriteLine("3.μCoulomb");
Console.WriteLine("4.nCoulomb");
Console.WriteLine("5.pCoulomb");
string answer2 = Console.ReadLine();
//Console.WriteLine("Please input your number");
//double answer3 = Convert.ToDouble(Console.ReadLine());
double resultconversion = 0;
while (answer1 == "1")
{
if (answer1 == answer2)
{
Console.WriteLine("No conversion neeeded!");
}
else if (answer1 != answer2)
{
switch (answer2)
{
case "2":
answerpow = 10 ^ -3;
resultconversion = Math.Pow(answerbase, answerpow);
break;
case "3":
answerpow = 10 ^ -6;
resultconversion = Math.Pow(answerbase, answerpow);
break;
case "4":
answerpow = 10 ^ -9;
resultconversion = Math.Pow(answerbase, answerpow);
break;
case "5":
answerpow = 10 ^ -12;
resultconversion = Math.Pow(answerbase, answerpow);
break;
}
}
}
while(answer1 == "2")
{
if (answer1 == answer2)
{
Console.WriteLine("No conversion needed");
}
else if (answer1 != answer2)
switch (answer2) //line 107
{
case "1":
answerpow = 10 ^ 3;
resultconversion = answerbase/(10 ^ 3);
break;
case "3":
answerpow = 10 ^ 6;
resultconversion = Math.Pow(answerbase, answerpow);
break;
case "4":
answerpow = 10 ^ 9;
resultconversion = Math.Pow(answerbase, answerpow);
break;
case "5":
answerpow = 10 ^ 12;
resultconversion = Math.Pow(answerbase, answerpow);
break;
}
}
Console.WriteLine($"The result in an int form: {answerbase}^{answerpow}");
Console.WriteLine($"The number {answerbase} is raised to the power of {answerpow}");
Console.WriteLine("The result in decimal for: " + resultconversion);
}
}
The problem that I face is that when I reach line 107 my code freezes and nothing happens. I am not sure what's happening. Please note that the code is incomplete and the working functions as of right now are only the first and half of the second. I am a beginner and I would appreciate if you would not critisise me for not discovering the problem.
I added a while loop so for each answer specific things would happen. But inside the second while loop something seems to be broken inside the first switch case at line 107. When I input the form of the current number as mCoulomb and the form I am trying to convert it to Coulomb the program just freezes.
The problem is simple, you have a while loop on answer1 but you are not modifying it anywhere in your code. and that is the reason it is becoming an infinite loop. you can either add a break at the end of each while loop (you need to check this first). or modify the answer1 so that it may come out of the loop.
Vivek's answer is correct as to what's happening in your program. You should mark his answer as correct.
I'm posting this as a way for you to see how this code can get written.
string unit = "Coulomb";
var factors = new[]
{
new { name = "", magnitude = 0 },
new { name = "m", magnitude = -3 },
new { name = "μ", magnitude = -6 },
new { name = "n", magnitude = -9 },
new { name = "p", magnitude = -12 },
};
string[] header1 = new string[] { "Welcome to the Physics calculator!", "Please choose one of the following options", "1.Unit Conversions", "", "What is the current form of the number you are trying to convert?", };
string[] header2 = new string[] { "In which for would you like to convert your number?", "Choose one of the following options", };
int GetFactorIndex(string[] header)
{
Console.WriteLine(String.Join(Environment.NewLine, header.Concat(factors.Select((x, n) => $"{n + 1}. {x.name}{unit}"))));
return int.Parse(Console.ReadLine()) - 1;
}
int index1 = GetFactorIndex(header1);
Console.WriteLine("Please enter the number: ");
double value = double.Parse(Console.ReadLine());
int index2 = GetFactorIndex(header1);
int magnitude = factors[index1].magnitude - factors[index2].magnitude;
double factor = Math.Pow(10.0, magnitude);
double answer = value * factor;
Console.WriteLine($"The number {value} {factors[index1].name}{unit} is multiplied by {factor}");
Console.WriteLine($"The result in value of: {answer} {factors[index2].name}{unit}");
That's it. I think it is working correctly. Let me know if there are any issues you discover.

Return to a specific block of code in my application

I am fairly new to C# and currently building a simple ATM app. I am attempting to write code to return the user to the main menu according to his/her entry of the letter M. The break, continue, goto or return keywords do not seem to work in my scenario; perhaps I used them incorrectly. The statement directly below is where I would like to jump to.
Console.WriteLine("Select an option? \n VIEW BALANCE (B1) checking, (B2) saving \n DEPOSIT (C1) checking, (C2) saving \n WITHDRAW (W1) checking, (W2) saving");
I would like to jump from the line JUMP (below) within the else if statement nested within the switch statement into the section of code above. How can I achieve this? any help is appreciated...thanks!
switch (response)
{
case "C1":
Console.WriteLine("How much would you like to deposit to your checking account?");
string depositEntry = Console.ReadLine();
double checkingBalance = Convert.ToInt32(depositEntry) + currentCheckingBalance;
currentCheckingBalance += checkingBalance;
Console.WriteLine("Your current checking balance is " + checkingBalance + "\n (X) Exit, (M) Main Menu" );
string selection = Console.ReadLine().ToUpper();
if (selection == "X")
{
return;
}
else if (selection == "M")
{
***JUMP***
}
else
{
Console.WriteLine("Your entry was invalid");
}
break;
case "C2":
break;
case "W1":
Using a jump statement usually indicates the flow of logic is jumbled. I try to avoid any kind of jumps if necessary. The code below prints out a main menu and if the user types “x” the program will quit. If the user selects one of the other options, a message is simply printed out indicating what the user selected. After the user presses any key, the console clears and the main menu is re-displayed.
In the main menu, if the user does not type one of the selections, then the selection is ignored, the console is cleared, and the menu is reprinted. No error is displayed indicating invalid selections.
This does not require the user to type “m” to go back to the main menu. After a selection is made for Deposit/withdraw/… after the method is finished the code will automatically return to the main menu.
I am guessing this may be what you are looking for. Hope this helps.
static void Main(string[] args) {
string userInput = "";
while ((userInput = GetMainSelection()) != "x") {
switch (userInput) {
case "c1":
Console.WriteLine("C1 Deposit Checking method");
break;
case "c2":
Console.WriteLine("C2 Deposit Savings method");
break;
case "b1":
Console.WriteLine("B1 View Balance Checking method");
break;
case "b2":
Console.WriteLine("B2 View Balance Savings method");
break;
case "w1":
Console.WriteLine("W1 Withdraw Checking method");
break;
case "w2":
Console.WriteLine("W2 withdraw Savings method");
break;
}
Console.WriteLine("Press Any Key to continue"); // <-- show what method was just used
Console.ReadKey();
Console.Clear();
}
Console.Write("Press any key to exit the program");
Console.ReadKey();
}
private static string GetMainSelection() {
string userInput = "";
while (true) {
Console.WriteLine("Select an option? \n VIEW BALANCE (B1) checking, (B2) saving \n DEPOSIT (C1) checking, (C2) saving \n WITHDRAW (W1) checking, (W2) saving. (X) to EXit");
userInput = Console.ReadLine().ToLower();
if (userInput == "b1" || userInput == "b2" || userInput == "c1" || userInput == "c2" || userInput == "w1" || userInput == "w2" || userInput == "x") {
return userInput;
}
else {
Console.Clear();
}
}
}
Put the JUMP code in a function and return.
public void MainMenu() {
// Show the main menu
}
public void Response(string response) {
switch (response)
{
case "C1":
Console.WriteLine("How much would you like to deposit to your checking account?");
string depositEntry = Console.ReadLine();
double checkingBalance = Convert.ToInt32(depositEntry) + currentCheckingBalance;
currentCheckingBalance += checkingBalance;
Console.WriteLine("Your current checking balance is " + checkingBalance + "\n (X) Exit, (M) Main Menu" );
string selection = Console.ReadLine().ToUpper();
if (selection == "X")
{
return;
}
else if (selection == "M")
{
***JUMP***
MainMenu();
return;
}
else
{
Console.WriteLine("Your entry was invalid");
}
break;
case "C2":
break;
case "W1":
}
}
Similar to the already given answer, I suggest breaking this out. Here's an example:
The Main method:
static void Main(string[] args) {
string input = null;
do {
input = Console.ReadLine();
ParseInput(input);
} while (input != "X");
}
ParseInput:
static void ParseInput(string input) {
switch (input) {
case "X": //from Main(), this will close the app
return;
case "M":
MainMenu();
break;
case "C1":
ShowAccount("C1"); //move your deposit/withdraw logic into a method and call with the selected account
return;
//other accounts
default:
break; //error message?
}
}
and MainMenu:
static void MainMenu() {
Console.WriteLine("Select an option? \n VIEW BALANCE (B1) checking, (B2) saving \n DEPOSIT (C1) checking, (C2) saving \n WITHDRAW (W1) checking, (W2) saving");
}
This should let you read the input in a loop and the ParseInput function can handle your individual cases. You may also want to call MainMenu() at the start, so it shows from the beginning.
It works like this:
Get input from the user
Pass the input to ParseInput() which decides where to go next.
Any functions hit in ParseInput() will execute, writing to the console or asking for further input
Once that function returns, while (input != "X") evaluates. If input != "X", goto 1, else exit.
I suggest you use goto C# reference.
static void Main()
{
int x = 200, y = 4;
int count = 0;
string[,] array = new string[x, y];
// Initialize the array:
for (int i = 0; i < x; i++)
for (int j = 0; j < y; j++)
array[i, j] = (++count).ToString();
// Read input:
Console.Write("Enter the number to search for: ");
// Input a string:
string myNumber = Console.ReadLine();
// Search:
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
if (array[i, j].Equals(myNumber))
{
goto Found;
}
}
}
Console.WriteLine("The number {0} was not found.", myNumber);
goto Finish;
Found:
Console.WriteLine("The number {0} is found.", myNumber);
Finish:
Console.WriteLine("End of search.");
// Keep the console open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
Output for the Input 44 would be:
Enter the number to search for: 44
The number 44 is found.
End of search.
See here for the MSDN reference.

Error when trying to do math equation

I am new to Console Application, I usually use C# for Unity. The code doesn't really work how I want it
Yes I know using Goto isn't good. But I don't know alternatives
I had [ a = 2 ] [ b = 3 ] and [ ans = a+b ] so the obvious answer is 5. So when you put 5 it runs the Else statement which is getting it incorrect.
goto start;
error:
Console.Clear();
Console.WriteLine("Input not Recognized");
Console.WriteLine("Try Again");
Console.WriteLine("\nType (Reset) to Reset Program");
Console.WriteLine("\nType (End) to End Program");
Console.WriteLine("");
string error1 = Console.ReadLine();
if (error1.Equals("reset", StringComparison.InvariantCultureIgnoreCase))
{
goto start;
}
if (error1.Equals("end", StringComparison.InvariantCultureIgnoreCase))
{
Environment.Exit(0);
}
else
{
goto error;
}
start:
Console.WriteLine("Solve the Math Equation");
int a = 2;
int b = 3;
int ans = a + b;
Console.WriteLine("\n2 + 3");
Console.WriteLine("");
string user = "";
ConsoleKeyInfo key;
do
{
key = Console.ReadKey(true);
if (key.Key != ConsoleKey.Backspace)
{
double val = 0;
bool _x = double.TryParse(key.KeyChar.ToString(), out val);
if (_x)
{
user += key.KeyChar;
Console.Write(key.KeyChar);
}
}
else
{
if (key.Key == ConsoleKey.Backspace && user.Length > 0)
{
user = user.Substring(0, (user.Length - 1));
Console.Write("\b \b");
}
}
}
while (key.Key != ConsoleKey.Enter);
if (user.Equals(ans))
{
Console.Clear();
Console.WriteLine("Correct!");
Console.WriteLine("\nYour answer " + ans);
Console.WriteLine("\nType (End) to End Program");
Console.WriteLine("");
string end1 = Console.ReadLine();
if (end1.Equals("end", StringComparison.InvariantCultureIgnoreCase))
{
Environment.Exit(0);
}
else
{
goto error;
}
}
else
{
Console.Clear();
Console.WriteLine("Incorrect!");
Console.WriteLine("\nThe answer was " + ans);
Console.WriteLine("\nType (Reset) to Reset Program");
Console.WriteLine("Type (End) to End Program");
Console.WriteLine("");
string rne1 = Console.ReadLine();
if (rne1.Equals("reset", StringComparison.InvariantCultureIgnoreCase))
{
Console.Clear();
goto start;
}
if (rne1.Equals("end", StringComparison.InvariantCultureIgnoreCase))
{
Environment.Exit(0);
}
else
goto error;
Your user and ans are not equal in your code which is why your code jumps to the error. The reason why they are not equal are their Types.
user is a string
ans is an integer
So you are comparing "5" to 5 and that can not be equal.
Convert one of the variables so you have the same type.
Either use user.Equals(ans.ToString()) in the if statement or convert the string to a number (which is the better solution IMO - because it also handles the situation when the input is not a number). Like this:
int userAns;
if (!Int32.TryParse(user, userAnsj))
Console.WriteLine("Input is not a valid integer.");
and then comapare userAns to ans.

How to implement a safety net so that my program does not crash when incorrect format is input

This is my C# program for taking a number and then an operator and another number that the user enters and then doing a simple calculation, how would I go about implementing a safety net so that no matter what the users enters, whether it be nothing or a string of unwanted characters, the program will say invalid input, try again. right now it crashes if the exactly correct input is not entered. thanks in advance. Please explain the location in my code where it would be best to implement this and the flow of logic.
using System;
class Calculation
{
public static void Main(string[] data)
{
double result = Convert.ToDouble(data[0]);
char op;
double number;
GetData(out op, out number);
while (op != 'q' && op != 'Q')
{
switch (op)
{
case '+':
result += number;
break;
case '-':
result -= number;
break;
case '*':
result *= number;
break;
case '/':
result /= number;
break;
}
Console.WriteLine("Result = " + result.ToString());
GetData(out op, out number);
}
}
static void GetData(out char anOperator, out double aNumber)
{
aNumber = 0;
string line;
Console.Write("Enter an opertor and a number or 'q' to quit: ");
line = Console.ReadLine();
anOperator = Convert.ToChar(line.Substring(0, 1));
if (anOperator != 'q' && anOperator != 'Q')
aNumber = Convert.ToDouble(line.Substring(2));
}
}
You may want to look into the Double.TryParse() method. Personally I wouldn't recommend using exceptions for user input - invalid user input is not an exceptional situation in my opinion, but something you should expect all the time. There are probably some edge cases that I missed that should be solved with exception handling - can't think of any right now.
Basically the idea here is to check all the conditions you can think of and don't let exceptions bubble up.
I'm employing the TryXXX() pattern in here. I introduced a string to get the error message from the method that gets the user input in case the input is invalid. By the way, I also replaced your loop with a do-while.
public static void Main(string[] data)
{
if(data.Length == 0)
{
Console.WriteLine("No input :(");
return;
}
double result;
if(!Double.TryParse(data[0], out result))
{
Console.WriteLine("Invalid input: " + data[0]);
return;
}
Console.WriteLine("Starting with number: " + result);
char op;
double number;
string errorMessage;
do
{
if(!TryGetData(out op, out number, out errorMessage))
{
Console.WriteLine("Invalid input: " + errorMessage);
continue;
}
switch (op)
{
case '+':
result += number;
break;
case '-':
result -= number;
break;
case '*':
result *= number;
break;
case '/':
result /= number;
break;
default:
Console.WriteLine("Invalid operator: " + op);
continue;
}
Console.WriteLine("Result = " + result.ToString());
} while (Char.ToLower(op) != 'q');
}
static bool TryGetData(out char anOperator, out double aNumber, out string message)
{
aNumber = 0;
message = null;
Console.Write("Enter an operator and a number or 'q' to quit: ");
var line = Console.ReadLine();
anOperator = line[0];
if (anOperator != 'q' && anOperator != 'Q')
{
if(line.Length <= 2)
{
// string too short
message = "Input string too short";
return false;
}
var isValidNumber = Double.TryParse(line.Substring(2), out aNumber);
if(!isValidNumber)
{
message = "Invalid number: " + line.Substring(2);
return false;
}
if(isValidNumber && (anOperator == '/' && aNumber == 0))
{
message = "Cannot divide by 0.";
return false;
}
}
return true;
}
this is called Exception handling ... You can use try/catch like this
try
{
string mystring = "this is a string and can't be converted to int ";
int myint = int.Parse(mystring);
}
catch
{
Console.WriteLine("Please Enter a vaild Data Type");
}
this way the app won't crush , instead it will display any error message you want

c#....my while loop keep repeating [duplicate]

This question already exists:
c# programming constructs - my while loop wont stop working when i want it to? [closed]
Closed 9 years ago.
i am trying to get my while loop to ONLY keep repeating till all subjects have been answered, then it should stop and display the bonus and final score. but don't know why its not doing that? help please.
namespace Assignment
{
class Class1
{
public static int attempt, sum, AptScore, GenScore, MathScore, EngScore, bonus, TotalScore, FinalScore, choice = 0;
public static string ans;
static void Main(string[] args)
{
bool stop = false;
Console.WriteLine("Welcome to this Salisbury University IQ Test game");
Console.WriteLine();
Console.WriteLine("How many times have you attempted this test?");
attempt = Convert.ToInt32(Console.ReadLine());
while (true)
if (attempt > 1)
{
Console.WriteLine("You cannot take this test");
}
else
{
Console.WriteLine(" \n1. Aptitude \n2. English. \n3. Math \n4. Gk \n5. Exit");
choice = Convert.ToInt32(Console.ReadLine());
switch (choice)
{
case 1:
Console.WriteLine(" What was the name of the lebanon tyrant who ruled for years unending before he was toppled due to civil war? \nA. Osama Bin laden \nB. Gaddafi \nC. Jonathan ");
ans = Console.ReadLine();
if (ans == "B" || ans == "b")
{
AptScore += 10;
}
break;
case 2:
Console.WriteLine(" What is the antonym of Pleasure? \nA. Pain \nB. Ecstacy \nC. Wonder");
ans = Console.ReadLine();
if (ans == "A" || ans == "a")
{
EngScore += 10;
}
break;
case 3:
Console.WriteLine(" What is the sum of 435 and 345? \nA. 799 \nB. 780 \nC. 600 ");
ans = Console.ReadLine();
if (ans == "B" || ans == "b")
{
MathScore += 10;
}
break;
case 4:
Console.WriteLine(" What year did Nigeria become a republic? \nA. 1960 \nB. 1963 \nC. 1990 ");
ans = Console.ReadLine();
if (ans == "B" || ans == "b")
{
GenScore += 10;
}
break;
case 5:
Environment.Exit(0);
break;
}
if (stop)
break;
TotalScore = MathScore + GenScore + EngScore + AptScore;
Console.WriteLine("Your total score is : " + TotalScore);
if (TotalScore == 10)
{
Console.WriteLine(" You have no Bonus point ");
}
else if (TotalScore == 20)
{
bonus += 2;
Console.WriteLine("Your Bonus is {0}", bonus);
}
else if (TotalScore == 30)
{
bonus += 5;
Console.WriteLine("Your Bonus is {0}", bonus);
}
else if (TotalScore == 40)
{
bonus += 10;
Console.WriteLine("Your Bonus is {0}", bonus);
}
else
{
FinalScore = TotalScore + bonus;
Console.WriteLine("Your finalscore is : " + FinalScore);
}
switch (FinalScore)
{
case 10:
if (FinalScore >= 10)
{
Console.WriteLine("Your IQ level is below average");
}
break;
case 22:
if (FinalScore >= 22)
{
Console.WriteLine("Your IQ level is average");
}
break;
case 35:
if (FinalScore >= 35)
{
Console.WriteLine("You are intelligent");
}
break;
case 40:
if (FinalScore == 40)
{
Console.WriteLine("You are a genius");
}
break;
default:
break;
}
}
}
}
}
if (stop)
break;
This never happens.
I see:
bool stop = false;
and also:
if (stop)
break;
But I never see a stop = true;
Where do you intend to set stop to true?

Categories