C# 如何使此程序正常工作?

C# 如何使此程序正常工作?,c#,C#,首先让我快速总结一下这个程序的功能。这是一款非常基本的点击游戏(这里是:空格/键盘上的任意按钮/)。我有两栋建筑(庄园和增压器)。Manor根据其等级制造黄金,而booster提高每次点击(按)的黄金制造量 然而,当我试图运行程序本身时,我遇到了一个问题。基本上(正如我将在这里复制粘贴的代码上看到的),当我运行程序的第一个功能(制作黄金)时,它会正确地生成和保存黄金(您的黄金余额现在是x),但当我尝试升级我的建筑/查看我的余额时,它会显示:黄金不足/0 我试着分别打印出这些步骤(没有while功

首先让我快速总结一下这个程序的功能。这是一款非常基本的点击游戏(这里是:空格/键盘上的任意按钮/)。我有两栋建筑(庄园和增压器)。Manor根据其等级制造黄金,而booster提高每次点击(按)的黄金制造量

然而,当我试图运行程序本身时,我遇到了一个问题。基本上(正如我将在这里复制粘贴的代码上看到的),当我运行程序的第一个功能(制作黄金)时,它会正确地生成和保存黄金(您的黄金余额现在是x),但当我尝试升级我的建筑/查看我的余额时,它会显示:黄金不足/0

我试着分别打印出这些步骤(没有while功能),效果很好,我甚至可以升级我的建筑,但在那之后,它开始奇怪地生成黄金(因此,它没有在2级/压力机上生成黄金,而是生成了其他东西,结果甚至出现了负数)

现在,在下面的代码中,我没有完整的while函数(我试图修复它,因为出于测试目的,我只需要生成和一个升级函数,所以我没有使用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();
        }
      }
    }
  }
}

基本上你只是打错了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();
    }
}

从建筑标高
0
开始?还是我遗漏了什么?WindowsForms和WPF等桌面显示技术不是游戏编程的合适工具。安慰更不用说了。对于游戏开发,你需要一些带有GameLoop的东西使“构建”成为嵌套类,也会使阅读变得异常困难。它应该是兄弟/对等程序,而不是子部分。这样很容易搞乱值访问。@MarkiianBenovskyi不,你是对的。我把它改为1(作为基本等级),但我仍然面临原来的问题。@Christopher。谢谢你的澄清。我知道游戏制作需要你提到的其他东西(我不知道是什么,但现在我知道了)。基本上,我想用这个程序做的是制作这个点击游戏背后的基本逻辑/算法。它不可能以这种方式工作。游戏和Web应用程序都有一个非常独特的结构,这决定了您的工作方式。控制台应用程序也是如此。WinForms/WPF和其他基于消息泵/事件队列的东西也是如此也就是说,在控制台上所有不合适的工具中,它可能是最容易模拟/制作游戏循环的。最大的问题是获得一种非阻塞的方式来读取用户输入,但我想我找到了一些其他的解决方案:这也是游戏编程中处理输入的方式。