C# 如何将旧数据类型值与新数据类型值进行比较
所以我用这个来显示以太坊硬币的当前价格和其他东西。我正在尝试创建一个小型控制台应用程序,用于检查该值是否与上次扫描时的值发生了变化 到目前为止我所知道的是。。我知道我用当前值扫描当前值,所以很明显它永远不会改变。 我试着设置一个变量来保存旧的值,但是没有任何作用 如何将第一次扫描与第二次扫描进行比较,以查看浮点值是否上升或下降C# 如何将旧数据类型值与新数据类型值进行比较,c#,json,linq,api,console-application,C#,Json,Linq,Api,Console Application,所以我用这个来显示以太坊硬币的当前价格和其他东西。我正在尝试创建一个小型控制台应用程序,用于检查该值是否与上次扫描时的值发生了变化 到目前为止我所知道的是。。我知道我用当前值扫描当前值,所以很明显它永远不会改变。 我试着设置一个变量来保存旧的值,但是没有任何作用 如何将第一次扫描与第二次扫描进行比较,以查看浮点值是否上升或下降 private static void Ticker() { while (true) {
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.");
}
}
}
您需要记住以前的值。一种方法是维护id和CoinApi类的字典
public Dictionary<int, CoinApi> CoinDict = new Dictionary<int, CoinAPI>();
public Dictionary CoinDict=new Dictionary();
然后可以将当前值存储在此字典中,然后将新值与现有值进行比较。然后更新到新值
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.");
}
}
}
}
public void updatecondit(硬币)
{
CoinDict[coin.Id]=硬币;
}
公共浮动GetCoinPrice(CoinApi币)
{
if(硬币目录包含(硬币Id))
{
return CoinDict[coin.Id]。价格为美元;
}
其他的
{
更新指示(硬币);
返回硬币。价格为美元;
}
}
私有静态无效代码()
{
while(true)
{
常量字符串uri=@”https://api.coinmarketcap.com/v1/ticker/ethereum/";
var client=new WebClient();
var content=client.DownloadString(uri);
var results=JsonConvert.DeserializeObject(内容);
用于(投币结果)
{
浮动当前金额=硬币价格\美元;
浮动oldPrice=GetCoinPrice(硬币);
如果(当前金额<旧价格)
{
Console.WriteLine(“amount低于上次。”);
}
否则如果(当前金额>旧价格)
{
Console.WriteLine(“金额高于上次。”);
}
其他的
{
WriteLine(“自上次扫描以来,金额没有变化。”);
}
}
}
}
尝试将旧值保存到静态变量,应该可以解决您的问题。托多斯:你的第一个请求怎么办?处理异常等
其他问题:是否应在中运行,而(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;
}
}
私有静态浮点值;
私有静态无效代码()
{
while(true)
{
常量字符串uri=@”https://api.coinmarketcap.com/v1/ticker/ethereum/";
var client=new WebClient();
var content=client.DownloadString(uri);
var results=JsonConvert.DeserializeObject(内容);
浮动当前金额=结果[0]。价格(美元);
如果(当前金额<_旧值)
{
Console.WriteLine(“amount低于上次。”);
}
否则如果(当前金额>\u旧值)
{
Console.WriteLine(“金额高于上次。”);
}
else if(currentAmount==\u oldValue)
{
WriteLine(“自上次扫描以来,金额没有变化。”);
}
_oldValue=当前金额;
}
}
正如您所写,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;
}
}
private static void Ticker()
{
上浮金额=0.0;
while(true)
{
常量字符串uri=@”https://api.coinmarketcap.com/v1/ticker/ethereum/";
var client=new WebClient();
var content=client.DownloadString(uri);
var results=JsonConvert.DeserializeObject(内容);
浮动当前金额=结果[0]。价格(美元);
如果(当前金额<以前金额)
{
Console.WriteLine(“amount低于上次。”);
}
否则如果(当前金额>以前的金额)
{
Console.WriteLine(“金额高于上次。”);
}
else if(currentAmount==以前的金额)
{
WriteLine(“自上次扫描以来,金额没有变化。”);
}
以前的金额=当前金额;
}
}
您必须将以前的执行值传递给当前执行以进行比较。
将该变量声明为该类中的全局分数。或者将其声明到您想要使用的级别。我的方法允许处理多个硬币实例,如果您只需要管理一个价格,那么其他用户会发布更简单的方法,这可能更可取
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;
}
}