c# - 改进代码,添加 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);
}
}
}
解决方案
首先,您需要根据职责将程序划分为功能(关注点分离)
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:
}
}
推荐阅读
- directx - 为什么我的固定全息图不随头部移动而缩放?
- android - 如何通过匕首 2 注入使用意图值的依赖项?
- dart - 忽略子触摸,并在父小部件上接收它们
- python - 使用张量的普通最小二乘计算加速
- python - Python - 获取每个第 n 个元素(就地)
- java - 为什么 C++ 中的整数数组搜索循环比 Java 慢?
- javascript - 无法使用 React js 将数据推送和附加到状态
- hyperledger-fabric - 如何从组织外部查询链码
- python - 如何过滤以任何元音开头的字符串?[Python]
- google-cloud-platform - 通过 API 密钥访问存储桶中的 JSON