5. How to program in C# – SWITCH STATEMENTS – Beginner Tutorial

49
2



In this video we learn about switch statements, classes and how to generate random numbers.

Code used:

Want to make powerful games, Windows and Mac software or mobile applications? Then this course is a great place to start.

Difficulty of lesson: Novice.

Follow me on Twitter @BrackeysTweet

Donate:

Website:
Get a T-Shirt:
Facebook:
Twitter:

Music:
Baby Plays Electro Games

Nguồn:https://wijstaanvooronzegrondrechten.org/

Xem Thêm Bài Viết Khác:https://wijstaanvooronzegrondrechten.org/cong-nghe

49 COMMENTS

  1. I am writing a quiz program asking questions that don't involve numbers. How would I write the question with 4 or 2 multiple choice answers. Then have the user input an answer. Then mark it correct or incorrect so I can let the user try a second time on the questions that are wrong.

  2. you are the best
    i tryed to make a lop and it is working fin
    }

    Console.ReadKey ();

    Console.WriteLine ();

    goto start;

    }
    thank you man

  3. Hi Brackeys! First of all you are amazing !!! Thank you so much for inspiring me!

    Currently I am going through the C# Beginner Tutorials and I am trying to do each challenge at the end. However I did the last video challenge and programmed everything correctly but I only get the "Your answer is incorrect" statement as a feedback from the program. No matter if I input correct or incorrect value as an answer it always executes the "else" function.
    I am using Visual Studio and It is yielding the same result even if I copy your code in. Any suggestions what might be wrong ?

    Here's my code:

    using System;

    namespace FirstConsoleProject

    {

    class MainClass

    {

    public static void Main(string[] args)

    {

    int num01 = 16;

    int num02 = 4;

    Console.Write(" What is " + num01 + " multyplied by " + num02 + " equal to ?: ");

    int answer = Convert.ToInt32(Console.Read());

    if (answer == num01 * num02)

    {

    Console.WriteLine("That is correctc!");

    }

    else

    {

    Console.WriteLine("Hmm… You better try again");

    }

    Console.ReadKey();

    }

    }

    }

  4. using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    namespace FirstConsoleProject

    {

    class MainClass

    {

    public static void Main(string[] args) // method called when program starts

    {

    start:

    Console.WriteLine("nType a random number: ");

    int num01 = Convert.ToInt32(Console.ReadLine());

    Console.WriteLine("Type another number: ");

    int num02 = Convert.ToInt32(Console.ReadLine());

    Console.WriteLine("nnow answer what " + num01 + " Divided by " + num02 + " is!(decimals wont work 🙁 i tried my best): ");

    int answer = Convert.ToInt32(Console.ReadLine());

    if (answer == +num01 / num02)

    {

    Console.WriteLine("great job!!! you did the math");

    Console.ReadKey();

    goto start;

    }

    Console.WriteLine("Sorry but, you need to be more persice (decimals aren't counted)");

    Console.ReadKey();

    goto start;

    }

    }

    }
    my code

  5. I had a different code, but still worked!

    static void Main(string[] args)

    {

    Console.WriteLine("If You Are Able To Answer This Math Equation, You Are Welcome To Come In!");

    Console.WriteLine("What Is 10 * 10?");

    int answer = Convert.ToInt32(Console.ReadLine());

    if (answer == 100)

    {

    Console.WriteLine("You Answered Right, Come Right In Sir!");

    }else if (answer < 100)

    {

    Console.WriteLine("Wrong To Short, Sorry You Can't Come In");

    }else if (answer > 100)

    {

    Console.WriteLine("Wrong To Long, Sorry You Can't Come In");

    }

    Console.ReadLine();

    }

    }

    }

  6. I did pretty much the same thing but I used the exact number instead of num01 / num02, it looks more professional your way. Right now my method works but only because I know the number if it were random, it would only work with the proper formula you used.
    EDIT: By the way, now that I notice I also did something different, int num01; and then num01 = 10; because that's what you said in the first or second video and you told us not to do what you did here xD.

  7. I absolutely hate myself. I got half way through the video then chose to pause and write this script:
    https://pastebin.com/eNqfhxSv (notice the amount of "else if's" that I have)

    Little did I know that literally a minute later he would explain sWiTcH sTaTeMeNtS

    i AM ANGERy!1

  8. using System;
    namespace FirstConsoleProject

    {

    class MainClass

    {

    public static void Main(string[] args) // this is a method called "Main". It is called when the program starts.

    {
    Random num = new Random();

    start :
    int num01 = num.Next(1,11) ;

    int num02 = num.Next(1,11) ;

    Console.WriteLine("What is " + num01 + " times " + num02 + " ?");
    int answer = Convert.ToInt32(Console.ReadLine());
    if (answer == num01 * num02)
    {
    int right_answer = num.Next (1, 4);
    switch (right_answer){
    case 1 :
    Console.WriteLine("Well done! Your answer is correct.");
    break;
    case 2 :
    Console.WriteLine("good . you are genius");
    break;
    case 3 :
    Console.WriteLine("your intelligence is awsome!");
    break;
    }

    }
    else
    {
    int wrong_answer = num.Next (1, 4);
    switch (wrong_answer) {
    case 1 :
    Console.WriteLine("Are you even trying?");
    break;
    case 2 :
    Console.WriteLine("Your answer is wrong");
    break;
    case 3 :
    Console.WriteLine("you are stupid!");
    break;
    }

    }
    Console.WriteLine();
    goto start;
    //Console.ReadKey();

    }

    }

    }

  9. when i login the forum , i get this error

    https://user-images.githubusercontent.com/43931249/51563745-e99f4380-1e95-11e9-8974-44c4258eee00.png

  10. This is getting fun:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    namespace Testing1

    {

    class Program

    {

    static void Main(string[] args)

    {

    Start:

    Random numberGen = new Random();

    int num1 = numberGen.Next(1, 13);

    int num2 = numberGen.Next(1, 13);

    int usersAnswer;

    Problem1:

    Console.WriteLine("What is " + num1 + " multiplied by " + num2 + "?");

    usersAnswer = Convert.ToInt32(Console.ReadLine());

    if (usersAnswer == num1*num2)

    {

    Console.WriteLine("Correct!");

    Console.ReadLine();

    Console.WriteLine();

    goto Start;

    }

    else

    {

    Console.WriteLine("Incorrect!");

    Console.ReadLine();

    Console.WriteLine();

    goto Problem1;

    }

    }

    }

    }

  11. My soulution was different:

    namespace BaldisBasicsProblem
    {
    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine("Can you answer my problem");
    Console.WriteLine("5 X 6");
    int answer = Convert.ToInt32(Console.ReadLine());
    if (answer == 30)
    {
    Console.WriteLine("congrats your correct");
    }
    else if (answer > 30)
    {
    Console.WriteLine("Sorry thats incorrect ):");
    }
    else if (answer < 30)
    {
    Console.WriteLine("did you even try??? ):");
    }
    Console.ReadKey();
    }
    }
    }

  12. I had a problem with the code and it would close after I put a correct answer so I just put a Console.ReadKey() inside of the if statement values.

  13. haha.. I am what u will call a proper noob. I never had any computer classes since my birth. Whatever I know, I learnt it on my own. now I am total beginner at programming. but this course is awesome and I will stick to it. it is really awesome how I can solve his challenges and the end product after one coding session is worth all the pain. !!!

  14. im using visual studio guess that might be the issue but whenever i try to use switch it says "Control cannot fall out of switch from final case label ('case:1')

  15. it tells me the answer is wrong and changes the numbers after I type the answer.
    Is there a mistake?

    using System;

    public class Program
    {
    public static void Main()
    {

    Random numberGenerator = new Random();

    int num01 = numberGenerator.Next(1,11);
    int num02 = numberGenerator.Next(1,11);

    Console.WriteLine("What is "+num01+" times "+num02+"?");

    int answer = Convert.ToInt32(Console.ReadLine());

    if (answer == num01*num02)
    {
    Console.WriteLine("Well Done!");
    } else
    {
    Console.WriteLine("Go home, you're drunk!");
    }
    }
    }

  16. Perfect teaching skills … thanks for making these. I love the way you explain every single things and the chalenges are really good practice. Thanks a lot. 😊🌹

  17. when i ran this program the only answer i got when answering wrong was the one i set as default..why aren't the other cases showing
    up? 🙁

LEAVE A REPLY

Please enter your comment!
Please enter your name here