【问题标题】:How do I compare the old data type value with the new ones如何将旧数据类型值与新数据类型值进行比较
【发布时间】:2017-01-23 07:40:46
【问题描述】:

所以我正在使用这个API,它显示了以太币的当前价格和其他信息。我正在尝试创建一个小型控制台应用程序,用于检查该值是否与我们上次扫描时相比发生了变化。

到目前为止,我所拥有的是......而且我知道我正在用当前值扫描当前值,所以很明显它永远不会改变。 我尝试设置一个变量来保存旧值,但没有做任何事情。

如何比较第一次扫描和第二次扫描以查看浮点值是上升还是下降?

private static void Ticker()
        {
            while (true)
            {
                const string uri = @"https://api.coinmarketcap.com/v1/ticker/ethereum/";
                var client = new WebClient();
                var content = client.DownloadString(uri);

                var results = JsonConvert.DeserializeObject<List<CoinApi>>(content);

                float currentAmount = results[0].price_usd;
                if (currentAmount < currentAmount)
                {
                    Console.WriteLine("Ammount is lower than the last time.");
                }
                else if (currentAmount > currentAmount)
                {
                    Console.WriteLine("The amount is higher than the last time.");
                }
                else if (currentAmount == currentAmount)
                {
                    Console.WriteLine("The amount hasnt changed since the last time we scanned.");
                }
            }
        }

这是类文件。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CryptoTicker.Core
{
    class Main
    {
    }

    public class CoinApi
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Symbol { get; set; }
        public float price_usd { get; set; }
    }
}

【问题讨论】:

    标签: c# json linq api console-application


    【解决方案1】:

    尝试将旧值保存到静态变量应该可以解决您的问题。 TODOS:你将如何处理你的第一个请求?处理异常等...

    其他问题:是否应该在 while(true) 中运行?也许您应该使用 HttpClient 并重用它。

        private static float _oldValue;
    
        private static void Ticker()
        {
            while (true)
            {
                const string uri = @"https://api.coinmarketcap.com/v1/ticker/ethereum/";
                var client = new WebClient();
                var content = client.DownloadString(uri);
    
                var results = JsonConvert.DeserializeObject<List<CoinApi>>(content);
    
                float currentAmount = results[0].price_usd;
                if (currentAmount < _oldValue)
                {
                    Console.WriteLine("Ammount is lower than the last time.");
                }
                else if (currentAmount > _oldValue)
                {
                    Console.WriteLine("The amount is higher than the last time.");
                }
                else if (currentAmount == _oldValue)
                {
                    Console.WriteLine("The amount hasnt changed since the last time we scanned.");
                }
    
                _oldValue = currentAmount;
            }
        }
    

    【讨论】:

      【解决方案2】:

      正如你所写,currentAmount 将永远等于它自己。

      这应该可行:

      private static void Ticker()
      {
          float previousAmount = 0.0;
          while (true)
          {
              const string uri = @"https://api.coinmarketcap.com/v1/ticker/ethereum/";
              var client = new WebClient();
              var content = client.DownloadString(uri);
      
              var results = JsonConvert.DeserializeObject<List<CoinApi>>(content);
      
              float currentAmount = results[0].price_usd;
      
              if (currentAmount < previousAmount )
              {
                  Console.WriteLine("Ammount is lower than the last time.");
              }
              else if (currentAmount > previousAmount )
              {
                  Console.WriteLine("The amount is higher than the last time.");
              }
              else if (currentAmount == previousAmount)
              {
                  Console.WriteLine("The amount hasnt changed since the last time we scanned.");
              }
              previousAmount = currentAmount;
          }
      }
      

      【讨论】:

        【解决方案3】:

        您需要记住之前的值。一种方法是维护 id 和 CoinApi 类的字典

        public Dictionary<int, CoinApi> CoinDict = new Dictionary<int, CoinAPI>();
        

        然后您可以将当前值存储在此字典中,然后将新值与现有值进行比较。然后更新为新值

        public void UpdateCoinDict(CoinApi coin)
        {
            CoinDict[coin.Id] = coin;
        }
        
        public float GetCoinPrice(CoinApi coin)
        {
            if (CoinDict.Contains(coin.Id))
            {
                return CoinDict[coin.Id].price_usd;
            }
            else 
            {
                UpdateCoinDict(coin);
                return coin.price_usd;
            }              
        }
        
        
        
        private static void Ticker()
        {
            while (true)
            {
                const string uri = @"https://api.coinmarketcap.com/v1/ticker/ethereum/";
                var client = new WebClient();
                var content = client.DownloadString(uri);
        
                var results = JsonConvert.DeserializeObject<List<CoinApi>>(content);
        
                for (coin in results)
                {
                    float currentAmount = coin.price_usd;
                    float oldPrice = GetCoinPrice(coin); 
                    if (currentAmount < oldPrice)
                    {
                        Console.WriteLine("Ammount is lower than the last time.");
                    }
                    else if (currentAmount > oldPrice)
                    {
                        Console.WriteLine("The amount is higher than the last time.");
                    }
                    else
                    {
                    Console.WriteLine("The amount hasnt changed since the last time we scanned.");
                    }
                } 
            }
        }
        

        【讨论】:

        • 我的方法允许处理多个硬币实例,如果您只需要管理一个价格,那么其他用户发布了更简单的方法,可能更可取
        【解决方案4】:

        您必须将先前的执行值传递给当前执行进行比较。 将该变量声明为该类中的全局分数。或者将其声明到您想要使用它的级别。

        【讨论】:

          猜你喜欢
          • 2023-03-16
          • 2013-09-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-10
          • 1970-01-01
          • 2010-12-30
          • 1970-01-01
          相关资源
          最近更新 更多