0

只是想制作一个掷硬币的游戏,但也试图使等于随机数 0 或 1 的硬币转换为字符串类型“正面”或“反面”。编程新手,所以请不要判断它是否看起来很业余。谢谢你。

  namespace TestCoin2
 {
        class Program
   {  

    static void Main(string[] args)
    {
        int coin;// this will hold my random int numbers.

        string userInput;// This will hold all my user input/ answers.




        Console.WriteLine("Hello, Pick Heads or Tails:");
        userInput =Console.ReadLine();

        Random rng = new Random();

        coin = rng.Next(0, 2);


        string myString2;
        string myString;
        if (coin == 0)
        {
            myString = coin.ToString("heads");
        }

        else if (coin == 1)
        {
             myString2 = coin.ToString("tails"); <error under myString2
        }

       if (myString && userInput == "heads")
        {

            Console.WriteLine("You picked Right! Heads! YOU WIN!");
        }

        else if (coin == 1 && userInput == "Tails")
        {

            Console.WriteLine("You picked Right! Tails! YOU WIN!");
        }

        else
        {
        Console.WriteLine("You picked Wrong! it was..." + myString); <error

        }

        Console.ReadLine();
    }



}

}

4

3 回答 3

1

Since you are converting a fixed number of ints to something else. Your best bet would be to define an enum.

public enum CoinSide
{
    Heads = 0,
    Tails = 1
}

Then you can just cast the int to the enum and it will output the side.

var rng = new Random();
var coin = rng.Next(0,2);
Console.WriteLine((CoinSide)coin);

If you need to compare them you can use this.

if (((CoinSide)coin).ToString() == "Heads")
    Console.WriteLine("Winner");

Full Solution

//Accept user input
Console.WriteLine("Hello, Pick Heads or Tails:");
var userInput = Console.ReadLine();

//Create and flip the coin
var rng = new Random();
var coin = (CoinSide)rng.Next(0, 2);

//Compare input to coin
if (coin.ToString() == userInput)
    Console.WriteLine("You picked Right! {0}! YOU WIN!", coin);
else
    Console.WriteLine("You picked Wrong! it was... {0}", coin);
于 2016-01-17T23:10:31.397 回答
0

您可以像这样简单地使用?:运算符

myString = coin == 0 ? "heads" : "tails";
于 2016-01-17T23:05:17.600 回答
0

为了使其更具可读性,您可以使用如下枚举。您可以使用任何您喜欢的数字来代替 0 和 1。在类中定义您的枚举并在方法中使用它。

public enum CoinSides{ Heads = 0, Tails = 1}

你可以重写你的完整代码如下,

 namespace TestCoin2
{

class Program
{
    public enum CoinSides { Heads = 0, Tails = 1 }

    static void Main(string[] args)
    {
        string userInput;// This will hold all my user input/ answers.
        Console.WriteLine("Hello, Pick Heads or Tails:");
        userInput = Console.ReadLine();

        int coin;// this will hold my random int numbers.
        Random rng = new Random();
        coin = rng.Next(0, 2);

        if (coin == Convert.ToInt32(CoinSides.Heads) && userInput == CoinSides.Heads.ToString("D"))
        {
            Console.WriteLine("You picked Right! Heads! YOU WIN!");
        }
        else if (coin == Convert.ToInt32(CoinSides.Tails) && userInput == CoinSides.Tails.ToString("D"))
        {
            Console.WriteLine("You picked Right! Tails! YOU WIN!");
        }
        else
        {
            Console.WriteLine("You picked Wrong! it was..." + myString);
        }

        Console.ReadLine();
    }
}
}
于 2016-01-17T23:21:39.853 回答