2017-01-23 12 views
0

したがって、このAPIを使用しています。これは、現在の価格とethereumコインに関するその他のものを示しています。私は最後にスキャンしたときから値が変わったかどうかを調べる小さなコンソールアプリケーションを作成しようとしています。古いデータ型の値を新しいものと比較するには

私は今までこれを持っています...そして、私は現在の値で現在の値をスキャンしているので、決して変化しないことは明らかです。 私は古い値を保持する変数を設定しようとしましたが、何もしませんでした。

最初のスキャンと2番目のスキャンを比較して、浮動小数点値が上下しているかどうかを確認するにはどうすればよいですか?

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

以前の値を覚えておく必要があります。これを行う1つの方法は、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

古い値を静的変数に保存して、問題を解決してください。あなたの最初のリクエストで何をするつもりですか?例外処理など...

その他の問題:これは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

あなたが比較するために、現在の実行に前回の実行値を渡す必要があります。 そのクラス内のグローバルスコアと同じ変数を宣言します。または、それを使用したいレベルまで宣言します。

関連する問題