2017-01-23 152 views
0

因此,我使用此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; } 
    } 
} 

回答

0

您需要记住以前的值。这样做的一种方式可以是保持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."); 
      } 
     } 
    } 
} 
+0

我的方法允许处理多个硬币实例,如果您只需要管理一个价格,那么其他用户发布的更简单的方法可能更合乎需要 – Ben

0

尝试将旧值保存到静态变量应解决您的问题。 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; 
     } 
    } 
0

正如你所写,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; 
    } 
} 
0

你必须上一次执行值传递给当前执行比较。 将该变量声明为该类中的全局分数。或者将其声明到您要使用它的位置。