改进代码,添加Goto或循环功能

时间:2019-01-06 21:00:22

标签: c# goto

我正在尝试在代码中添加Goto或类似函数。该游戏是基于hammurabi游戏的。我正在尝试删除代码中显示“请重设”的部分,然后再回到问题所在。

游戏完成后,我希望它可以循环播放并为年份添加一个值,但也保留原始游戏中的值。

using System;

namespace Game2
{
    class Program
    {
        static void Main(string[] args)
        {
            Random rnd = new Random();
            int land = 1000, people = 100, landprice = rnd.Next(17, 26);
            int foodin = 0, landin = 0, landout = 0, seeds = 0;
            int foodcost = 20, food = 2800, totalfoodcost = 0;
            int year = 0;



            Console.WriteLine("TRY YOUR HAND AT GOVERNING ANCIENT SUMERIA");
            Console.WriteLine("SUCCESSFULLY FOR A 1 YEAR TERM OF OFFICE.");
            Console.WriteLine();

            Console.WriteLine("HAMURABI:  I BEG TO REPORT TO YOU,");
            Console.WriteLine();
            Console.WriteLine("Year " + year + 1 +" In Charge");
            Console.WriteLine("The city population is now " + people);
            Console.WriteLine("The city owns " + land + " acres");
           Console.WriteLine("You now have " + food + " bushels in store.");
            Console.WriteLine("Land is trading at " + landprice + " bushels per acre.");
            Console.WriteLine();

            Console.Write("How Many Acres Would You Like To Buy? ");
            landin = int.Parse(Console.ReadLine());
            Console.WriteLine();
            food = food - landprice * landin;
            land = landin + land;

            if (landin * landprice <= food)

            {
                Console.WriteLine("You Bought " + landin + " Acres");
                Console.WriteLine("You Own " + land + " Acres");
                Console.WriteLine("You Have " + food + " Bushell Left");
                Console.WriteLine();

            }

            else if (landin * landprice > food)

            {
                Console.WriteLine("You Dont Have Enought Bushell ");
                return;

            }


            Console.Write("How Many Acres Would You Like To Sell? ");
            landout = int.Parse(Console.ReadLine());
            Console.WriteLine();
            land = land - landout;
            food = landprice * landout + food;

            if (landout <= land)

            {
                Console.WriteLine("You Sold " + landout + " Acres");
                Console.WriteLine("You Own " + land + " Acres");
                Console.WriteLine("You Now Have " + food + " Bushells");
                Console.WriteLine();
            }

            else if (landout > land)
            {
                Console.WriteLine("You Dont Own Enough Acres - Please Reset");

                return;
            }

            Console.Write("How Many Bushell Would You Like To Feed Your People? ");
            foodin = int.Parse(Console.ReadLine());
            Console.WriteLine();


            totalfoodcost = people * foodcost;



            if (foodin == totalfoodcost && foodin < food)

            {
                Console.WriteLine("Your People Have Eaten");
                food = food - totalfoodcost;
                Console.WriteLine("You Have " + food + " Bushell Left");
                Console.WriteLine();

            }

            else if (foodin > food)

            {
                Console.WriteLine("You Dont Have Enought Bushell - Please Restart! ");
                return;
            }

            else if (foodin < totalfoodcost)
            {
                Console.WriteLine("You Have Been Over Thrown As People Are Hungry - Please Restart! ");
                return;

            }

            Console.Write("How Many Acres Do You Wish To Plant With Seed? ");
            seeds = int.Parse(Console.ReadLine());
            Console.WriteLine();


            if (seeds <= food && seeds <= people)

            {
                Console.WriteLine("You Have Planted " + seeds + " Seeds");
                food = food - seeds;
                Console.WriteLine("You Now Have " + food + " Bushell");
                Console.WriteLine();
            }

            else if (seeds > food)
            {
                Console.WriteLine("You Do Not Have Enought Bushell");
            }

            else if (seeds > people)
            {
                Console.WriteLine("You Do Not Have Enought People");
            }



            Console.WriteLine("End Of Year " + year);


        }
    }
}

2 个答案:

答案 0 :(得分:0)

首先,您需要根据程序的职责将其划分为功能(关注分离)

class Program
{
    static void Main(string[] args)
    {
        Random rnd = new Random();
        int land = 1000, people = 100, landprice = rnd.Next(17, 26);
        int foodin = 0, landin = 0, landout = 0, seeds = 0;
        int foodcost = 20, food = 2800, totalfoodcost = 0;
        int year = 0;

        for (; year < <break_condition> ; year++)
            StartMyGame(land, foodin, foodcost, year);
     } 
}

此外,您还可以使用goto statement

class Program
{
    static void Main(string[] args)
    {
        Random rnd = new Random();
        int land = 1000, people = 100, landprice = rnd.Next(17, 26);
        int foodin = 0, landin = 0, landout = 0, seeds = 0;
        int foodcost = 20, food = 2800, totalfoodcost = 0;
        int year = 0;

        game_start_pos:

        StartMyGame(land, foodin, foodcost, year++);

        if (!<your_break_condition> )
            goto game_start_pos:
     } 
}

答案 1 :(得分:0)

这是我解决的代码,使用了while循环。

using System;

namespace Game2
{
    class Program
    {
        static void Main(string[] args)
        {

            {
                // This is the to allow random numbers to be created
                Random rand = new Random(DateTime.Now.Millisecond);
                Random ran1 = new Random(DateTime.Now.Millisecond);
                Random ran2 = new Random(DateTime.Now.Millisecond);
                Random ran3 = new Random(DateTime.Now.Millisecond);
                Random ran4 = new Random(DateTime.Now.Millisecond);
                //this is my integer which will hold my data
                int people = 100, landPrice = 0;
                int foodIn = 000, landIn = 0000, landOut = 0000, seeds = 0;
                int foodCost = 20, food = 2800, totalFoodCost = 0000;
                int year = 1;
                int land = 1000;
                int newPeople = 0;
                int peopleDied = 0;
                int seedCost = 0;
                int rats = 0;
                int peopleSell = 0;
                double landFinal = 0;
                peopleSell = people * 10;
                //game rules and start of game
                Console.WriteLine("Game Rules! ");
                Console.WriteLine("The game lasts 10 years, with a year being one turn. ");
                Console.WriteLine("Each year, enter how many bushels of grain to allocate to buying (or selling) acres of land, feeding your population, and planting crops for the next year ");
                Console.WriteLine("Each person needs 20 bushels of grain each year to live and can till at most 10 acres of land. ");
                Console.WriteLine("Each acre of land requires one bushel of grain to plant seeds. ");
                Console.WriteLine("The price of each acre of land fluctuates from 17 bushels per acre to 26 bushels. ");
                Console.WriteLine("If the conditions in your country ever become bad enough, the people will overthrow you and you won't finish your 10 year term. ");
                Console.WriteLine("If you make it to the 11th year, your rule will be evaluated ");
                Console.WriteLine();
                Console.WriteLine("TRY YOUR HAND AT GOVERNING ANCIENT SUMERIA");
                Console.WriteLine("SUCCESSFULLY FOR A 10 YEAR TERM OF OFFICE.");
                Console.WriteLine();
                Console.WriteLine("HAMURABI:  I BEG TO REPORT TO YOU,");
                Console.WriteLine();
                // loop to add + 1 to year if year is <= 10 and people > 0
                while (year <= 10 && people > 0)
                {
                    // this will random gen a number between 17 and 26
                    landPrice = rand.Next(17, 26);
                    Console.WriteLine("Year " + year + " In Charge.");
                    Console.WriteLine();
                    Console.WriteLine("You Harvested " + seeds + " Bushels.");
                    Console.WriteLine(newPeople + " People Came To The City.");
                    Console.WriteLine(peopleDied + " Died In The City.");
                    Console.WriteLine("Rats Have Eaten " + rats + " Bushels.");
                    people = people + newPeople;
                    people = people - peopleDied;
                    Console.WriteLine("The City's Population Is Now " + people + ".");
                    Console.WriteLine("The City Owns " + land + " Acres.");
                    food = seeds + food;
                    food = food - rats;
                    Console.WriteLine("You Now Have " + food + " Bushels In Storage.");
                    Console.WriteLine("Land is trading at " + landPrice + " Bushels per Acre.");
                    Console.WriteLine();
                    landFinal = ((double)land / people);
                    //start of game
                    // Section for user to enter how many acres would like to buy
                    Console.Write("How Many Acres Would You Like To Buy? ");
                    landIn = int.Parse(Console.ReadLine());
                    Console.WriteLine();
                    // this will random gen a number between 1 and 10 and 0 and 5 and so on.
                    newPeople = ran1.Next(1, 10);
                    peopleDied = ran2.Next(0, 5);
                    rats = ran3.Next(50, 500);
                    // loop if landIn * landPrice > food or landIn < 0 will show error and ask user to input again
                    while (landIn * landPrice > food || landIn < 0)
                    {
                        Console.WriteLine("You Dont Have Enough Bushels!! ");
                        Console.Write("PLEASE RE ENTER: How Many Acres Would You Like To Buy? ");
                        landIn = int.Parse(Console.ReadLine());
                        Console.WriteLine();
                    }
                    // loop if landIn * landPrice < food  then allows user to go on and shows summary
                    if (landIn * landPrice < food)
                    {
                        food = food - landPrice * landIn;
                        land = landIn + land;
                        Console.WriteLine("You Bought " + landIn + " Acres.");
                        Console.WriteLine("You Own " + land + " Acres.");
                        Console.WriteLine("You Have " + food + " Bushels Left.");
                        Console.WriteLine();
                    }
                    // Section for user to enter how many acres would like to sell
                    Console.Write("How Many Acres Would You Like To Sell? ");
                    landOut = int.Parse(Console.ReadLine());
                    // loop if land < landOut or landIn < 0 will show error and ask user to input again

                    Console.WriteLine();
                    while (land < landOut || landOut < 0)
                    {
                        Console.WriteLine("You Dont Own Enough Acres!!");
                        Console.Write("RE ENTER How Many Acres Would You Like To Sell? ");
                        landOut = int.Parse(Console.ReadLine());
                        Console.WriteLine();
                    }
                    //if the loop condition is met will allow user to carry on.
                    if (landOut < land || landOut == land)
                        land = land - landOut;
                    food = food + landPrice * landOut;
                    {
                        Console.WriteLine("You Sold " + landOut + " Acres.");
                        Console.WriteLine("You Own " + land + " Acres.");
                        Console.WriteLine("You Now Have " + food + " Bushels.");
                        Console.WriteLine();
                    }
                    // Section for user to enter how many bushel to feed their people.
                    Console.Write("How Many Bushels Would You Like To Feed Your People? ");
                    foodIn = int.Parse(Console.ReadLine());
                    Console.WriteLine();
                    //  Section if condition not met user will have to re enter value
                    while (foodIn > food || foodIn < 0)
                    {
                        Console.WriteLine("You Dont Have Enough Bushels!!");
                        Console.Write("RE ENTER How Many Bushels Would You Like To Feed Your People?  ");
                        foodIn = int.Parse(Console.ReadLine());
                        Console.WriteLine();
                    }
                    totalFoodCost = people * foodCost;
                    // if condition met user can carry on
                    if (foodIn >= totalFoodCost && foodIn <= food)
                    {
                        Console.WriteLine("Your People Have Eaten.");
                        Console.WriteLine();
                        food = food - foodIn;
                        Console.WriteLine("You Have " + food + " Bushels Left.");
                        Console.WriteLine();
                    }
                    //  Section if condition not met user will have to re enter values
                    else if (foodIn < totalFoodCost)
                    {
                        Console.WriteLine("You Have Been Over Thrown As People Are Hungry - Please Restart! ");
                        return;
                    }
                    Console.Write("How Many Acres Do You Wish To Plant With Seed? ");
                    seeds = int.Parse(Console.ReadLine());
                    Console.WriteLine();
                    seedCost = (people * 10);
                    while (seeds > seedCost || seeds < 0)
                    {
                        Console.WriteLine("You Dont Have Enough People! ");
                        Console.Write("How Many Acres Do You Wish To Plant With Seed?  ");
                        seeds = int.Parse(Console.ReadLine());
                        Console.WriteLine();
                    }
                    if (seeds < seedCost && seeds < food)
                    {
                        Console.WriteLine("You Have Planted " + seeds + " Seeds.");
                        food = food - seeds;
                        Console.WriteLine("You Now Have " + food + " Bushels.");
                        Console.WriteLine();
                    }
                    while (land == 1 || seeds > land || seeds < 0)
                    {
                        Console.WriteLine("You Dont Have Enough Land!!");
                        Console.Write("How Many Acres Do You Wish To Plant With Seed?  ");
                        seeds = int.Parse(Console.ReadLine());
                        Console.WriteLine();
                    }
                    while (seeds > food || seeds < 0)
                    {
                        Console.WriteLine("You Dont Have Enough Bushels!!");
                        Console.Write("How Many Acres Do You Wish To Plant With Seed?  ");
                        seeds = int.Parse(Console.ReadLine());
                        Console.WriteLine();
                    }
                    food = food - seeds;
                    seeds = seeds * rand.Next(2, 8);
                    //end of year summary will loop untill year 11.
                    Console.WriteLine("End Of Year " + year + ".");
                    year = year + 1;
                    Console.WriteLine();
                    Console.WriteLine("You Ended With " + food + " Bushels.");
                    Console.WriteLine("You Ended With " + land + " Acres.");
                    Console.WriteLine();

                    landFinal = ((double)land / people);
                    // end of year final summary
                    if (year == 11)
                    {
                        Console.WriteLine("Congratulations On Reaching The End Of Your Term!");
                        Console.WriteLine();
                        Console.WriteLine("You Ended With " + food + " Bushels.");
                        Console.WriteLine("You Ended With " + land + " Acres.");
                        Console.WriteLine("You Ended With " + people + " People.");
                        Console.WriteLine("Each Person Ended With " + landFinal + " Per Acre.");
                        Console.WriteLine();
                        landFinal = ((double)land / people);
                    }
                    //failed year summary 
                    if (year == 11 && landFinal < 10)
                    {
                        Console.WriteLine("You Have Been Marked As A Failure For Your Service!!");
                        return;
                    }
                    //sucessful game summary 
                    if (year == 11 && landFinal >= 10)
                    {
                        Console.WriteLine("You Are A Great King!!");
                        return;
                    }

                }
            }
        }
    }
}