C
C#13mo ago
krixsick

❔ How to display All the integers in a list concatenated with a string

Hello! I was wondering if there was a possible way of cancatenating elements inside a list (that are integers) and combining it inside a console.WriteLine without having to do list.[index]. I was thinking of a for loop, but couldn't get it to print it all out in one line.
using System.Linq;
using System.Collections.Generic;
namespace BlackJack_Game
{
internal class Program
{
private static List<int> cards = new List<int>
{
11,2,3,4,5,6,7,8,9,10,10,10,10
};

private static List<int> userCards = new List<int>
{

};

private static List<int> computerCards = new List<int>
{

};

static Random rnd = new Random(); //Create an instance of Random class somewhere. Note that it's pretty important not to create a new instance each time you need a random number.
//You should reuse the old instance to achieve uniformity in the generated numbers. You can have a static field somewhere (be careful about thread safety issues):
//This static field allows all static methods to use this random object as a field

static void Main(string[] args)
{

userStartingCards();


static void userStartingCards()
{
for(int i = 0; i < 2; i++)
{
int randomStartingNumberUser = rnd.Next(cards.Count);
userCards.Add(cards[randomStartingNumberUser]);
}
Console.WriteLine("Your cards are [{0}, {1}], current score {2}", userCards[0], userCards[1], userCards.Sum());
}
using System.Linq;
using System.Collections.Generic;
namespace BlackJack_Game
{
internal class Program
{
private static List<int> cards = new List<int>
{
11,2,3,4,5,6,7,8,9,10,10,10,10
};

private static List<int> userCards = new List<int>
{

};

private static List<int> computerCards = new List<int>
{

};

static Random rnd = new Random(); //Create an instance of Random class somewhere. Note that it's pretty important not to create a new instance each time you need a random number.
//You should reuse the old instance to achieve uniformity in the generated numbers. You can have a static field somewhere (be careful about thread safety issues):
//This static field allows all static methods to use this random object as a field

static void Main(string[] args)
{

userStartingCards();


static void userStartingCards()
{
for(int i = 0; i < 2; i++)
{
int randomStartingNumberUser = rnd.Next(cards.Count);
userCards.Add(cards[randomStartingNumberUser]);
}
Console.WriteLine("Your cards are [{0}, {1}], current score {2}", userCards[0], userCards[1], userCards.Sum());
}
9 Replies
ZacharyPatten
ZacharyPatten13mo ago
yes it is possible. It isn't clear what you are asking specifically, but the string.Join(", ", cards) might be what you are asking for there are other methods that could help too so like
string userCardsOutput = "Your cards are " + string.Join(" + ", userCards) + " = " + userCards.Sum();
Console.WriteLine(userCardsOutput);
string userCardsOutput = "Your cards are " + string.Join(" + ", userCards) + " = " + userCards.Sum();
Console.WriteLine(userCardsOutput);
krixsick
krixsick13mo ago
OH Got it thank you so much!
ZacharyPatten
ZacharyPatten13mo ago
also... are you by chance going through the games in the dotnet-console-games repo?
krixsick
krixsick13mo ago
Nope, I have no clue what that is lol I was just searching random simple C# projects to do to improve my skills lol
ZacharyPatten
ZacharyPatten13mo ago
You appear to have been working on - blackjack - tic tac toe - rock paper scissors and this repo has examples of all of those apps: https://github.com/dotnet/dotnet-console-games
krixsick
krixsick13mo ago
Oh Damn never knew that lol Thanks for that!
ZacharyPatten
ZacharyPatten13mo ago
If you have questions about that repo feel free to "@" me 🙂
krixsick
krixsick13mo ago
Sounds good! Thank you so much for your help, I really appreciate it!
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts