Question Restart or exit console using an IF query

andrezi

New member
Joined
Dec 30, 2020
Messages
2
Programming Experience
Beginner
Hello, I have a question. I have started programming with a simple console application, a calculator.
This all works very well, but I have a problem with the if query if I want to restart the console (recalculate) or exit.
I'm looking for days on the Internet for a solution, unfortunately I can not find anything.

Therefore I would like to try it here whether vllt someone could help me to solve this problem.


Thanks in advance!

This is my code, but the lower part is missing, which I don't know...

C#:
Console.WriteLine("Do you want to start a recalculation or exit the program?");
Console.ReadLine();

char input;

if (input == 'J' || input == 'j')
{
    //code
}

if (input == 'N' || input == 'n')
{
    //code
}

Thank you in advance, I am a beginner so please understand that even such a simple thing is hard for me.

LG Andre
 
Last edited by a moderator:
If you want to do something repeatedly then you use a loop. You should put your calculation code inside a loop and then a new calculation can start when the current one ends. You can either have your loop depend on a condition that you set inside the loop or you can use an infinite loop and break out of it explicitly when you don't want to continue. That should be enough guidance for you to do some appropriate research and make an attempt to implement something. If what you do doesn't work as expected then you should post back and provide more specific details.
 
Read up on loops.

Also, you are not storing the results of the ReadLine() call. You probably should so you can check what the user entered in response.

As an aside, what book or tutorial are you using to learn C#. Most books and tutorials would cover loops and variables.
 
By the way, your if statements are poorly written. The input can't be more than one thing so there's no point checking for "N" if you've already determined that it was "J". At the very least, there should be an else there:
C#:
if (input == 'J' || input == 'j')
{
    //code
}
else if (input == 'N' || input == 'n')
{
    //code
}
Better still, use a switch statement:
C#:
switch (input)
{
    case 'J':
    case 'j':
        // ...
        break;
    case 'N':
    case 'n':
        // ...
        break;
}
 
By the way, your if statements are poorly written. The input can't be more than one thing so there's no point checking for "N" if you've already determined that it was "J". At the very least, there should be an else there:
C#:
if (input == 'J' || input == 'j')
{
    //code
}
else if (input == 'N' || input == 'n')
{
    //code
}
Better still, use a switch statement:
C#:
switch (input)
{
    case 'J':
    case 'j':
        // ...
        break;
    case 'N':
    case 'n':
        // ...
        break;
}
Thanks has helped me a lot.
By the way I am new to the subject I think writing my codes will automatically time to
better. When I have all the basics down pat.
I wish everyone a happy new year and stay
healthy.
 
Back
Top Bottom