Im just starting to learn unity and saw this task in one of my c# studying books. I have to create a code using an if statement inside foreach, so that it checks if i can afford each item in the dictionary, but i have no idea how to check all of them or even once specific, so i could write if 3 times for example.
At the moment my Log shows all the items and thier values, but shows if i can afford only the first one. What should i put in the IF brackets to check every value after it appears it Log?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LearningCurve : MonoBehaviour
{
public int currentGold = 3;
void Start()
{
Dictionary<string, int> itemInventory = new Dictionary<string, int>()
{
{"Potions", 4 },
{"Daggers", 3 },
{"Lockpicks", 1 }
};
foreach (KeyValuePair<string, int> itemCost in itemInventory)
{
Debug.LogFormat("Item {0} - {1}g", itemCost.Key, itemCost.Value);
if (currentGold >= itemCost.Value)
{
Debug.Log("I can afford that!");
}
}
}
CodePudding user response:
I am not sure if I understood the question but I will try to give you a basic overview of what is happening in the code you posted. Let's start with the if, how an if block works is simple you put a boolean bool for short in C# that can have two different values true and a false, inside the if(BOOL VALUE) and if the value is true it will run the code between the { CODE TO RUN }. Let's refactor the code a bit to see what is going on here.
Dictionary<string, int> itemInventory = new Dictionary<string, int>()
{
{"Potions", 4 },
{"Daggers", 3 },
{"Lockpicks", 1 }
};
foreach (KeyValuePair<string, int> itemCost in itemInventory)
{
Debug.LogFormat("Item {0} - {1}g", itemCost.Key, itemCost.Value);
bool iCanBuyitem = currentGold >= itemCost.Value;
Debug.LogFormat("{0} >= {1} is {2}", currentGold, itemCost.Value,iCanBuyitem);
if (iCanBuyitem)
{
Debug.LogFormat("I can buy {0} ", itemCost.Key);
}else
{
Debug.LogFormat("I can't buy {0} ", itemCost.Key);
}
}
Unlike in mathematics in programing symbol >= is not an equality symbol but something called a binary operator that takes two variables of one of the many numeric types in c# in your dictionary they are integers Dictionary<string, int> and produce a bool value that tells you if one number is more or equal to a second number, it's a method that has something similar to the the following signature public bool FirstIsBiggerOrEqualToSecond(int first, int second)
Here is a dotnet fiddle demonstrating the output https://dotnetfiddle.net/oWlYlY
CodePudding user response:
Read the question header You mean, if you want to put two or more conditions inside the IF, you have to use &&
operator:
if (currentGold >= itemCost.Value && currentGold <= 15)
{
Debug.Log("I have enough gold to buy this item and it's cheap.");
}
CodePudding user response:
Testing the code snippet provided two logs within the Console: "I can afford that!". Through this, I have determined that the issue lies within your snippet implementation. I suggest you check if you have enabled Collapse within the Console.
I have attached an Imgur link for reference. Console Log