如何使该程序正常运行?

时间:2019-11-11 18:51:08

标签: c#

让我首先快速总结一下该程序的功能。这是答题器游戏的一种非常基本的形式(此处:空格/键盘上的任何按钮/)。我有2座建筑物(庄园和助推器)。庄园根据其级别制造黄金,而助推器可以提高每次点击(按)的黄金量。

但是,当我尝试运行程序本身时,就会遇到问题。基本上(在我将在此处复制粘贴的代码上将可见),当我运行程序的第一个功能(制作黄金)时,它将正确生成并保存黄金(您的黄金余额现在为x),但是当我尝试升级建筑物/查看余额时,它说:黄金/ 0不足。

我尝试单独打印出步骤(没有while函数),并且可以工作,我什至可以升级我的建筑物,但是之后,它开始产生奇怪的黄金(因此,而不是在2级生产该黄金/ press,即使结果为负数,它也会产生其他结果。

现在在下面的代码中,我没有完整的while函数(我已尝试对其进行修复,因为出于测试目的,我只需要Production和一个Upgrade函数,所以我没有使用4个选项来完成该函数( A)制作B)升级c)升级D)显示余额),但只有前两个。)

感谢您的帮助,并祝您愉快!

using System;
using System.Collections.Generic;
using System.Linq;

namespace Clicker_Game
{
  class Program
  {
    class Buildings
    {
      // two buildings: manor, helper(multiplier) + gold balance // 
      int manorlevel = 0;
      int boosterlevel = 0;
      int goldbalance = 0;

      // level cost of the buildings and their properties
      // on each level index - 1 = level's bonus // 
      int[] mproduce = new int[10] { 1, 5, 7, 10, 15, 20, 25, 30, 40, 50 };
      int[] mlevelcost = new int[9] { 5, 30, 45, 60, 90, 115, 130, 200, 400 };
      int[] bvalue = new int[10] { 1, 1, 1, 2, 2, 2, 2, 3, 4, 6 };
      int[] blevelcost = new int[9] { 5, 30, 45, 60, 90, 115, 130, 200, 400 };

      public int ManorUpgrader()
      {
        Console.WriteLine("Do you really wanna upgrade your manor from " + manorlevel + 
                          " to " + ( manorlevel + 1 ) + "?");
        string answer = Console.ReadLine();

        if ( answer == "yes" )
        {
          if ( goldbalance >= mlevelcost[manorlevel - 1] )
          {
            Console.WriteLine("Congrats. You have successfully upgraded your manor to level" + 
                              ( manorlevel + 1 ) + "!");
            manorlevel += 1;
            goldbalance -= mlevelcost[manorlevel - 1];
          }
          else
          {
            Console.WriteLine("Insufficient funds!");
          }
        }
        return manorlevel;
      }
      public int BoosterUpgrader()
      {
        Console.WriteLine("Do you really wanna upgrade your booster from " + boosterlevel + 
                          " to " + ( boosterlevel + 1 ) + "?");
        string answer = Console.ReadLine();

        if ( answer == "yes" )
        {
          if ( goldbalance >= blevelcost[manorlevel - 1] )
          {
            Console.WriteLine("Congrats. You have successfully upgraded your booster to level" + 
                              ( boosterlevel + 1 ) + "!");
            boosterlevel += 1;
            goldbalance -= blevelcost[manorlevel - 1];
          }
          else
          {
            Console.WriteLine("Insufficient funds!");
          }
        }
        return boosterlevel;
      }

      public int Clicker()
      {
        Console.WriteLine("Here you can click to produce gold! Can we start?");
        string answer = Console.ReadLine();
        Console.WriteLine("If you want to stop just say no!");

        if ( answer == "yes" )
        {
          while ( true == true )
          {
            string a = Console.ReadLine();
            if ( a == "no" )
            {
              Console.WriteLine(goldbalance);
              break;
            }
            goldbalance += mproduce[manorlevel - 1] * bvalue[boosterlevel - 1];
          }
        }

        return goldbalance;
      }
      public int Balance()
      {
        Console.WriteLine("You wanna see your balance?");
        string a = Console.ReadLine();
        if ( a == "yes" )
        {
          Console.WriteLine(goldbalance);
        }
        return goldbalance;
      }
    }

    static void Main(string[] args)
    {
      {
        string answer = "yes";
        while ( answer != "exit" )
        {
          Console.WriteLine("baba");
          answer = Console.ReadLine();
          Buildings app = new Buildings();

          if ( answer == "a" )
          {
            app.Clicker();
          }
          else if ( answer == "b" )
          {
            app.ManorUpgrader();
          }

          Console.ReadKey();
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

基本上,您只是错误地输入了while循环。您需要做的是移动生产线:

Buildings app = new Buildings();

while循环之外。因此,您的Main方法应类似于:

static void Main(string[] args)
{
    // You need to create instance this class once
    // earlier you were creating it every time the loop started
    // so your values were reset to default
    var app = new Buildings();

    var answer = "yes";
    while (answer != "exit")
    {
        Console.WriteLine("Start loop");
        answer = Console.ReadLine();

        if (answer == "a")
        {
            app.Clicker();
        }
        else if (answer == "b")
        {
            app.ManorUpgrader();
        }

        Console.ReadKey();
    }
}