Jump to content

Help with C#??

Im trying to make a program where if you enter in a certain number, it outputs a result, then restarts itself so it can receive a different input to output a different result. This is my code so far. Can someone help me? Its for a project.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("NetScreen Diary Entry Log: Type in number of entry to access"); 
            do
            {
                int answer = Convert.ToInt16(Console.ReadLine());
                if (answer == 1)
                Console.Write("return blank space");
                Console.Write("NetScreen Diary Entry Log: Type in number of entry to access"); 
                answer = 0;
                if (answer == 2)
                Console.Write("return blank space 2");
                Console.Write("NetScreen Diary Entry Log: Type in number of entry to access"); 
                answer = 0;
            } while (true);
        }
    }
}

 

 

Link to comment
https://linustechtips.com/topic/795366-help-with-c/
Share on other sites

Link to post
Share on other sites

First off, please read and format with this in mind: https://linustechtips.com/main/announcement/12-please-use-code-tags/

Second: Use comments as often as possible, specially when sharing code with others

Third: check the use of curly braces after logic operators:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("NetScreen Diary Entry Log: Type in number of entry to access"); 
            do
            {
                int answer = Convert.ToInt16(Console.ReadLine());
				//if you dont use {} curly braces, then the if statement will only execute the immediatly next line, use this instead
                if (answer == 1)
				{
                	Console.Write("return blank space");
                	Console.Write("NetScreen Diary Entry Log: Type in number of entry to access"); 
                	answer = 0;
				}
                if (answer == 2)
				{
                Console.Write("return blank space 2");
                Console.Write("NetScreen Diary Entry Log: Type in number of entry to access"); 
                answer = 0;
				}
            } while (true);
        }
    }
}

  

Cheers!

Quote or tag if you want me to answer! PM me if you are in a real hurry!

Why do Java developers wear glasses? Because they can't C#!

 

My Machines:

The Gaming Rig:

Spoiler

-Processor: i5 6600k @4.6GHz

-Graphics: GTX1060 6GB G1 Gaming

-RAM: 2x8GB HyperX DDR4 2133MHz

-Motherboard: Asus Z170-A

-Cooler: Corsair H100i

-PSU: EVGA 650W 80+bronze

-AOC 1080p ultrawide

My good old laptop:

Spoiler

Lenovo T430

-Processor: i7 3520M

-4GB DDR3 1600MHz

-Graphics: intel iGPU :(

-Not even 1080p

 

Link to comment
https://linustechtips.com/topic/795366-help-with-c/#findComment-10012778
Share on other sites

Link to post
Share on other sites

1 minute ago, dany_boy said:

First off, please read and format with this in mind: https://linustechtips.com/main/announcement/12-please-use-code-tags/

Second: Use comments as often as possible, specially when sharing code with others

Third: check the use of curly braces after logic operators:


 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("NetScreen Diary Entry Log: Type in number of entry to access"); 
            do
            {
                int answer = Convert.ToInt16(Console.ReadLine());
				//if you dont use {} curly braces, then the if statement will only execute the immediatly next line, use this instead
                if (answer == 1)
				{
                	Console.Write("return blank space");
                	Console.Write("NetScreen Diary Entry Log: Type in number of entry to access"); 
                	answer = 0;
				}
                if (answer == 2)
				{
                Console.Write("return blank space 2");
                Console.Write("NetScreen Diary Entry Log: Type in number of entry to access"); 
                answer = 0;
				}
            } while (true);
        }
    }
}

  

Cheers!

Thank you so much! Ill use the code format from now on. 

Link to comment
https://linustechtips.com/topic/795366-help-with-c/#findComment-10012787
Share on other sites

Link to post
Share on other sites

1 minute ago, NovaMan01 said:

Thank you so much! Ill use the code format from now on. 

No worries man, if you need more help with C# or python, feel free to tag me in future posts.

Quote or tag if you want me to answer! PM me if you are in a real hurry!

Why do Java developers wear glasses? Because they can't C#!

 

My Machines:

The Gaming Rig:

Spoiler

-Processor: i5 6600k @4.6GHz

-Graphics: GTX1060 6GB G1 Gaming

-RAM: 2x8GB HyperX DDR4 2133MHz

-Motherboard: Asus Z170-A

-Cooler: Corsair H100i

-PSU: EVGA 650W 80+bronze

-AOC 1080p ultrawide

My good old laptop:

Spoiler

Lenovo T430

-Processor: i7 3520M

-4GB DDR3 1600MHz

-Graphics: intel iGPU :(

-Not even 1080p

 

Link to comment
https://linustechtips.com/topic/795366-help-with-c/#findComment-10012798
Share on other sites

Link to post
Share on other sites

in this case i would suggest using a switch case rather than ifs, will give an over all better readability 

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/795366-help-with-c/#findComment-10013872
Share on other sites

Link to post
Share on other sites

In addition to what's been said already: 

  • You can put the prompt for a new choice at the top of your do-while loop, instead of outside the loop & after each choice. 
  • You should use Int.TryParse() instead of Convert.ToInt16() and handle the situation if a user enters something that is not an integer. Right now, it'll crash if you enter something that isn't an integer.
  • You don't need to set the answer to zero after a choice has been made. You should only set it to a default value if (from above) the user tries to choose something that isn't a valid choice (or isn't an integer, if that's how you choose to handle that situation).

vorticalbox is also correct, a switch statement should be used here for readability. Switches are pretty much always used for menus like this.

Link to comment
https://linustechtips.com/topic/795366-help-with-c/#findComment-10013949
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×