15,942,710 members
See more: , +
Bill, Tommy and Daisy are playing basket ball. Bill's father is capturing the score. Each player gets 2 chances. prompt the user to input the player's name who scored the basket. If a player is not scoring in his/her chance, enter a space.

Quote:
Scoring Condition:
Bill's father records the basket scorer's name. Each basket gives the player 1 point.

If any player is scoring continuous basket , then they will get one bonus point.

For example, if Bill baskets 1st and 2nd consecutively, he gets a bonus point (which means, he gets 3 points)

I am a beginner in c# . My code fails in major cases in the calculation of scores? which is the right approach and for certain inputs I get value cannot be null.

```Sample Input 1:

Player Name : Daisy
Player Name: Daisy
Player Name : Bill
Player Name : Bill
Player Name : Tommy
<space>

Sample output  :

Bill Score : 3
Tommy Score : 1
Daisy Score : 3

Sample Input 2:

Player Name : Tommy
Player Name : Bill
Player Name : Bill
Player Name : Daisy
<space>
Player Name : Tommy

Sample output  :
Bill Score : 3
Tommy Score : 2
Daisy Score : 1```

What I have tried:

```using System.IO;
using System;
using System.Collections;
using System.Linq;

class Program
{
static void Main()
{
Dictionary<string,int> players = new Dictionary<string,int>();
int n=6;
string[] arr = new string[6];
for(int i=0;i<n;i++)
{
Console.WriteLine("Player Name : ");
}
//to check consecutive chances
for(int i=0;i<=n-2;i++)
{
if(arr[i]==arr[i+1])
{
players[arr[i]]=3;
}
}
//to check missed chances
for(int i=1;i<n;i++)
{
if(arr[i]==" ")
{
players[arr[i-1]]=1;
}
}

//not consecutive chances taken and one chance missed
string remainingPlayer = players.FirstOfDefault(x=> x.Value == 0).Key;
players[remainingPlayer]=2;

//printing results
for(int count=0;count<players.Count;count++)
{
var element = players.ElementAt(count);
var key = element.Key;
var value = element.Value;
Console.WriteLine("{0} Score : {1} ",key,value);
}
}
}```
Posted
Updated 7-May-21 3:53am

## Solution 1

Your code can be simplified to the following:
C#
```using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
static void Main()
{
Dictionary<string,int> players = new Dictionary<string,int>();
string previous = "";
for (int n = 0; n < 6; n++)
{
Console.Write("Player Name : ");
if (!string.IsNullOrEmpty(player) && player != " ")
{
players[player]++;
if (player.Equals(previous))
players[player]++;
previous = player;
}
}
foreach (KeyValuePair<string, int> kvp in players)
{
}
}
}```

You have not explained which test cases fail, or where the reported error occurs, so this may still be incomplete.

Aniket May2021 26-May-21 3:32am
Its Working 100%.Thank You.
Richard MacCutchan 26-May-21 3:40am
Are you the same person as priyaa_2021?
Aniket May2021 26-May-21 5:06am
No Sir.