Jump to content

Beginner C# Programmer

Enthusiast25
Go to solution Solved by Supericy,

In your case, you would actually want to use "else if" statements for your code, after the initial "if" statement.

if (userValue == "1")    message = "You chose 1!";else if (userValue == "2")    message = "You chose 2!";else if (userValue == "3")    message = "You chose 3!";else    message = "You didn't chose 1, 2, or 3!";

This way, your code will only execute 1 of the if-blocks, and if none of them are true, it will execute the else-block.

Hey, I started learning C# the other day, and I'm kind of a noob. My only previous "programming" was using small basic. Anyways, I'm making a simple CMD program just to kinda exercise my programming muscles a but and there's something wrong. http://pastebin.com/U6nF6vx3 That's the (super) simple code I made, but when I run the program and type 1 or 2, It says that I didn't type 1, 2, or 3. However, when I type 3, it says that I typed 3. Can you spot the error I made and tell me how to fix it?

 

Thanks in advance.

Link to comment
Share on other sites

Link to post
Share on other sites

well i really cant seem to find anything wrong with it. maybe try a switch case like:

switch (userValue){

case "1": message = "You chose 1!";

    break;

case "2": message = "You chose 2!";

    break;

case "3": message = "You chose 3!";

    break;

default: message = "You didn't chose 1, 2, or 3!";

    break;

 

but really there doesnt seem to be anything wrong with your ifs sometimes its just faster to try something else than to find out what exactly fucked up

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

Hey, I started learning C# the other day, and I'm kind of a noob. My only previous "programming" was using small basic. Anyways, I'm making a simple CMD program just to kinda exercise my programming muscles a but and there's something wrong. http://pastebin.com/U6nF6vx3 That's the (super) simple code I made, but when I run the program and type 1 or 2, It says that I didn't type 1, 2, or 3. However, when I type 3, it says that I typed 3. Can you spot the error I made and tell me how to fix it?

 

Thanks in advance.

also c# likes { ... } maybe include those and see if visual studio stops being weird

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

You should use if else if I think or a switch like above, I believe because of the ; at the end of each if it only actually runs the bottom area of your code which would explian why 3 works

 

(The code only goes for the 3 loop)

Link to comment
Share on other sites

Link to post
Share on other sites

In your case, you would actually want to use "else if" statements for your code, after the initial "if" statement.

if (userValue == "1")    message = "You chose 1!";else if (userValue == "2")    message = "You chose 2!";else if (userValue == "3")    message = "You chose 3!";else    message = "You didn't chose 1, 2, or 3!";

This way, your code will only execute 1 of the if-blocks, and if none of them are true, it will execute the else-block.

Link to comment
Share on other sites

Link to post
Share on other sites

well i really cant seem to find anything wrong with it. maybe try a switch case like:

switch (userValue){

case "1": message = "You chose 1!";

    break;

case "2": message = "You chose 2!";

    break;

case "3": message = "You chose 3!";

    break;

default: message = "You didn't chose 1, 2, or 3!";

    break;

 

but really there doesnt seem to be anything wrong with your ifs sometimes its just faster to try something else than to find out what exactly fucked up

I don''t get what you mean by that :/

Link to comment
Share on other sites

Link to post
Share on other sites

In your case, you would actually want to use "else if" statements for your code, after the initial "if" statement.

if (userValue == "1")    message = "You chose 1!";else if (userValue == "2")    message = "You chose 2!";else if (userValue == "3")    message = "You chose 3!";else    message = "You didn't chose 1, 2, or 3!";

This way, your code will only execute 1 of the if-blocks, and if none of them are true, it will execute the else-block.

Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

In your case, you would actually want to use "else if" statements for your code, after the initial "if" statement.

if (userValue == "1")    message = "You chose 1!";else if (userValue == "2")    message = "You chose 2!";else if (userValue == "3")    message = "You chose 3!";else    message = "You didn't chose 1, 2, or 3!";

This way, your code will only execute 1 of the if-blocks, and if none of them are true, it will execute the else-block.

oh yeah i overlooked that one yes that would be needed. that explains it right there because even if you hit 2 then the if for 2 trigger but the else for 3 also triggers and overwrites it

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks!

you could have seen that very easily by setting a stop marker at the first line and then hit F10 or F11 to jumps steps manually so you can see line by line what happens

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

I don''t get what you mean by that :/

maybe google switch case c#

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
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

×