首页 > 解决方案 > 改进代码,添加 Goto 或循环功能

问题描述

我正在尝试在我的代码中添加 Goto 或类似功能。这个游戏是基于汉谟拉比游戏。我正在尝试删除代码中显示“请重置”的部分并返回问题。

此外,一旦游戏完成,我希望它循环回来并为年份增加价值,但同时也保留原始游戏的价值。

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);


        }
    }
}

标签: c#goto

解决方案


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

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:
     } 
}

推荐阅读