【问题标题】:How can I check to see if the user won? [closed]如何检查用户是否获胜? [关闭]
【发布时间】:2020-11-22 12:55:08
【问题描述】:

如何检查用户是否通过条件获胜?

                int score1 = 0;
                int score2 = 0;
                int score3 = 0;
                for (int i = 1; i < 11; i++)
                { //open for
                    if (score1 > score2 && score1 > score3) //open if
                        Console.WriteLine(name1 + " takes the lead in lap " + i + "!");
                    else if (score2 > score1 && score2 > score3)
                    {
                        Console.WriteLine(name2 + " takes the lead in lap " + i + "!");
                    }
                    else if (score3 > score2 && score3 > score1)
                    {
                        Console.WriteLine(name3 + " takes the lead in lap " + i + "!");
                    } //close if
                } // close for
            } //close if 

这是我指的那段代码。

【问题讨论】:

标签: c# simulation


【解决方案1】:

所以,要解决这个问题,您需要 3 个变量,我们将每圈的各种分数添加到这些变量中,然后在 while 循环结束之前比较这些变量。发布的代码仅显示第一个选项的比较。我相信您可以弄清楚如何比较其余选项。

   class Program
{
    public static void Main(string[] args)
    { //open main method
        string name1 = "Buck";
        int endurance1 = 6;
        int speed1 = 4;
        string name2 = "Daisy";
        int endurance2 = 4;
        int speed2 = 6;
        string name3 = "Leo";
        int endurance3 = 7;
        int speed3 = 3;
        int balance = 100;
        Console.WriteLine("Welcome to the racetrack!\nToday's races will include 10 laps for reach race. There will be 3 races today. \nYou have $100 you can choose to bet on one of our 3 horses.\n1. Bet on a horse. \n2. Quit the game.");
        string input = Console.ReadLine();

        while (input == "1")
        { //open while1
            Console.WriteLine("Would you like to bet on: \n1." + name1 + ": \nEndurance:" + endurance1 + "\nSpeed:" + speed1 + "\n2." + name2 + ": \nEndurance:" + endurance2 + "\nSpeed:" + speed2 + "\n3." + name3 + ":\nEndurance:" + endurance3 + "\nSpeed:" + speed3 + "\nPlease enter the name of the horse as it appears on the screen.");
            string choice = Console.ReadLine();
            if (choice == "Buck")
            {
                Console.WriteLine("You have chosen " + name1 + ".");
            }
            else if (choice == "Daisy")
            {
                Console.WriteLine("You have chosen " + name2 + ".");
            }
            else
            {
                Console.WriteLine("You have chosen " + name3 + ".");
            }
            Console.WriteLine("How much would you like to bet? Your current balance is $" + balance + ".");
            string money = Console.ReadLine();
            int bet = int.Parse(money);
            Console.WriteLine("You are betting $" + bet + " on " + choice + ". Type \'start\' to start the race.");
            string race = Console.ReadLine();
            int totalScore1 = 0;
            int totalScore2 = 0;
            int totalScore3 = 0;

            if (race == "start")
            { // open if
                Console.WriteLine("Let the races begin!");
                for (int i = 1; i < 11; i++)
                { //open for
                    System.Random r = new System.Random();
                    int score1 = speed1 + endurance1 + r.Next(1, 8);
                    int score2 = speed2 + endurance2 + r.Next(1, 8);
                    int score3 = speed3 + endurance3 + r.Next(1, 8);
                    totalScore1 += score1;
                    totalScore2 += score2;
                    totalScore3 += score3;
                    if (score1 > score2 && score1 > score3) //open if
                        Console.WriteLine(name1 + " takes the lead in lap " + i + "!");
                    else if (score2 > score1 && score2 > score3)
                    {
                        Console.WriteLine(name2 + " takes the lead in lap " + i + "!");
                    }
                    else if (score3 > score2 && score3 > score1)
                    {
                        Console.WriteLine(name3 + " takes the lead in lap " + i + "!");
                    } //close if
                } // close for
            } //close if 
            else
                Console.WriteLine("Okay, take your time.");
            if(totalScore1> totalScore2 && totalScore1 > totalScore3 && choice =="Buck")
            {
                Console.WriteLine("Buckl won the race");
            }
        } //close while
        Console.WriteLine("Aw, too bad. Thanks for joining us!");
    } //close main method
}

}

【讨论】:

    【解决方案2】:

    我想向您展示集合的力量(数组和Dictionary),而不是解决您的确切问题。它们允许您不必为一匹马手动输入每个条件(想象一下,如果您有 15 匹马)

    由于您是新手,您可能很难理解此代码,但请观看并学习!

    using System;
    using System.Collections.Generic;
    
    struct Horse 
    {
        public string name;
        public int endurance;
        public int speed;
    };
        
    class Program {
        public static void Main(string[] args)
        {
            var horseByName = new Dictionary<string, Horse> 
            {
                {"Buck", new Horse{ name = "Buck", endurance = 6, speed = 4 } },
                {"Daisy", new Horse{ name = "Daisy", endurance = 6, speed = 4 } },
                {"Leo", new Horse{ name = "Leo", endurance = 7, speed = 3 } },
                {"Maya", new Horse{ name = "Maya", endurance = 5, speed = 4 } },
                {"Richard", new Horse{ name = "Richard", endurance = 5, speed = 4 } },
            };
            int balance = 100;
            int numberOfHorses = horseByName.Count;
            
            Console.WriteLine("Welcome to the racetrack!");
            Console.WriteLine("Today's races will include 10 laps for reach race. There will be 3 races today.");
            Console.WriteLine("You have $100 you can choose to bet on one of our 3 horses.");
            Console.WriteLine("1. Bet on a horse.");
            Console.WriteLine("2. Quit the game.");
            
            string input = Console.ReadLine();
    
            while (input == "1")
            {
                Console.WriteLine("Would you like to bet on:");
                int horseNumber = 1;
                foreach (var horse in horseByName.Values) 
                {
                    Console.WriteLine($"{horseNumber}. {horse.name}\nEndurance: {horse.endurance}\nSpeed: {horse.speed}");
                    ++horseNumber;
                }
    
                string choice = Console.ReadLine();
                while (!horseByName.ContainsKey(choice)) 
                {
                     Console.WriteLine("We don't have such horse, try again:");
                     choice = Console.ReadLine();
                }
                Console.WriteLine($"You have chosen {choice}.");
                
                Console.WriteLine($"How much would you like to bet? Your current balance is ${balance}.");
                int bet = int.Parse(Console.ReadLine());
                Console.WriteLine($"You are betting ${bet} on {choice}. Type \'start\' to start the race.");
                string race = Console.ReadLine();
                if (race == "start")
                {
                    Console.WriteLine("Let the races begin!");
                    int[] scores = new int[numberOfHorses];
                    int maxScorePerLap = 0;
                    string maxScoredHorseName = "";
                    
                    for (int lap = 1; lap < 11; lap++)
                    {
                        System.Random r = new System.Random();
                        int horseId = 0;
    
                        foreach (var horse in horseByName.Values) 
                        {
                            scores[horseId] = horse.speed + horse.endurance + r.Next(1, 8);
                            if (scores[horseId] > maxScorePerLap)
                            {
                                maxScorePerLap = scores[horseId];
                                maxScoredHorseName = horse.name;
                            }
                            ++horseId;
                        }
                        Console.WriteLine($"{maxScoredHorseName} takes the lead in lap {lap}!");
                    }
                    if (maxScoredHorseName == choice) 
                    {
                        Console.WriteLine($"Hurray! {choice} won!");
                        balance += bet;
                    }
                    else 
                    {
                        Console.WriteLine($"Unfortunately {maxScoredHorseName} won, but you had bet on {choice}...");
                        balance -= bet;
                    }
                }
                else
                {
                    Console.WriteLine("Okay, take your time.");
                }
            }
            Console.WriteLine("Aw, too bad. Thanks for joining us!");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-24
      相关资源
      最近更新 更多