none
How to fix that you can input a number and a word. RRS feed

  • Question

  • Hello I wanna make a roullet game but add this moment you can only add the coller black and red but i want that you can also add numbers how to fix this.

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

    namespace roullet
    {
        class Program
        {
            
            static void Main(string[] args)
            {
                int Cash = 100;
                Game(Cash);


                Console.ReadLine();
            }
            static void Game(int Cash)
            {
                Random numbergenerator = new Random();

                int RandomColler = numbergenerator.Next(0, 2);
                int a = 1;
                while (a == 1)
                {
                    Console.WriteLine(" ");
                    Console.WriteLine("Cash:" + Cash + "$");
                    Console.WriteLine("Bit?");
                    int Bit = Convert.ToInt32(Console.ReadLine());
                    Console.Write("Cash:"); Console.Write(Cash - Bit); Console.WriteLine("$");
                    Console.WriteLine("Bit:" + Bit + "$");
                    Console.WriteLine("Black Or Red");
                    string Coller = (Console.ReadLine());
                    if (RandomColler == 0)
                    {
                        Console.WriteLine("Black");

                        if (Coller == "Black")
                        {
                            Console.WriteLine("You Won" + Bit * 2);
                            Cash += Bit * 2;
                        }
                        else if (Coller == "black")
                        {
                            Console.WriteLine("You Won" + Bit * 2);
                            Cash += Bit * 2;
                        }
                        else
                        {
                            Console.WriteLine("You lose LOSER");
                            Cash -= Bit;
                        }
                    }


                    if (RandomColler == 1)
                    {
                        Console.WriteLine("Red");

                        if (Coller == "Red")
                        {
                            Console.WriteLine("You Won" + Bit * 2);
                            Cash += Bit * 2;
                        }
                        else if (Coller == "red")
                        {
                            Console.WriteLine("You Won" + Bit * 2);
                            Cash += Bit * 2;
                        }
                        else
                        {
                            Console.WriteLine("You lose LOSER");
                            Cash -= Bit;
                        }
                    }
                }
            }  
        }
    }

    Sunday, May 26, 2019 7:38 PM

All replies

  • Hi KipkesZjnGeil,

    Thank you for posting here.

    For your question, what does the "but i want that you can also add numbers how to fix this." mean? Do you mean you want to output different things when the RandomColler return 0 or 1? The number has the same level with Black and Red?

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, May 27, 2019 6:46 AM
    Moderator
  • Thnx for you reply I want that not only con you select a coller but also a number like in the real roullet game but with a string you can only add a word and with a int you can only add a number so how can i fix that i can add a number or a word add the same questing that the program is asking me.
    Monday, May 27, 2019 1:46 PM
  • Thnx for you reply I want that not only con you select a coller but also a number like in the real roullet game but with a string you can only add a word and with a int you can only add a number so how can i fix that i can add a number or a word add the same questing that the program is asking me.

    As with many programming tasks there are many ways to code this objective.
    Here is an example of one such way. It accepts input of a color string and
    a number string such as:

    Red 25
    Black 15
    Black   15
    Red          36

    static void Main(string[] args)
    {
        Console.WriteLine("Enter Color (Red or Black) and a number from 0-36:");
        string str = Console.ReadLine();
        string color = "";
        int num = 0;
        var subs = str.Split(' ');
        var subslst = new List<string>();
        foreach (string s in subs)
        {
            if (s == "") continue;
            subslst.Add(s);
        }
        if(subslst.Count != 2)
        {
            Console.WriteLine("You didn't enter two strings!");
            Console.ReadLine();
            return;
        }
        if (subslst[0] != "Red" && subslst[0] != "Black")
        {
            Console.WriteLine("You didn't enter the right color!");
            Console.ReadLine();
            return;
        }
        color = subslst[0];
        bool res = int.TryParse(subslst[1], out num);
        if(!res)
        {
            Console.WriteLine("You didn't enter a valid number!");
            Console.ReadLine();
            return;
        }
        if(num < 0 || num > 36)
        {
            Console.WriteLine("That number is outside the valid range 0-36!");
            Console.ReadLine();
            return;
        }
        Console.WriteLine("You entered color {0} and number {1}", color, num);
        Console.ReadLine();
    }
    

    - Wayne

    Monday, May 27, 2019 6:01 PM
  • As I understand it you want your gamer to insert either a number or a valid color (?). With TryParse you could check for both. Just could use a Enum for the valid colors and check for them, just as you check for an integer. Here is a small method that'll show what I mean, using the Enum RouletteColor:
    public enum RouletteColor { Red, Green, Black }
    
    static void Roulette()
    {
        string question = "Enter a color or a number: ";
        string input;
        Console.WriteLine(question);
        while ((input = Console.ReadLine()) != "x")
        {
            if (Int32.TryParse(input, out int number))
            {
                Console.WriteLine($"Inserted number is: {number}");
            }
            else if (Enum.TryParse<RouletteColor>(input, out RouletteColor farbe))
            {
                Console.WriteLine($"Inserted string is color: {input}");
            }
            else
            {
                Console.WriteLine("Wrong input!");
            }
            Console.WriteLine(question);
        }
    }
    wizend
    • Edited by Wizend Wednesday, May 29, 2019 6:43 PM Changed the method's name
    Wednesday, May 29, 2019 6:37 PM