Finding highest number of votes and matching to array of string - c#

I'm trying to figure out how to match a candidate name with candidate votes and display the highest vote along with the candidate name.
As in how to match the two arrays I have.
I know I'm missing something but what? I've only started learing C# at home.
namespace NumberOfVotes
{
class Program
{
static void Main(string[] args)
{
int size, minVotes;
int[] numOfCandidates;
int[] numOfVotes;
double avgMarks;
string[] candidateName;
Console.WriteLine("Enter number of candidates");
size = int.Parse(Console.ReadLine());
numOfCandidates = new int[size];
candidateName = new string[size];
numOfVotes = new int[size];
for (int i = 0; i < numOfCandidates.Length; i++)
{
Console.WriteLine("Enter a Candidate Name");
candidateName[i] = Console.ReadLine();
Console.WriteLine("Enter number of votes thus far");
numOfVotes[i] = int.Parse(Console.ReadLine());
}
int max = numOfVotes.Max();
avgMarks = numOfVotes.Average();
minVotes = numOfVotes.Min();
Console.WriteLine("Average votes: {0}", avgMarks);
Console.WriteLine("Min number of votes is: {0}", minVotes);
}
}
}

See these kind of things you can do with thinking about it with your head. StackOverflow isn't a website to post your problem if you're stuck, only if the problem you have needs a solution which can help other people.
This would work(most straightfoward approach to me):
int maxIndex = -1;
int max = -1;
for (int i = 0; i < numOfCandidates.Length; i++)
{
Console.WriteLine("Enter a Candidate Name");
candidateName[i] = Console.ReadLine();
Console.WriteLine("Enter number of votes thus far");
int num = int.Parse(Console.ReadLine()); // <-- unsafe
// just check it every time, if the number is greater than the previous maximum, update it.
if (num > max)
{
max = num;
maxIndex = i;
}
numOfVotes[i] = num;
}
Console.WriteLine("Candidate {0}, with {1} votes, has the most votes", candidateName[maxIndex], max);
However, if you want more things to calculate (like who has the least votes) without doing these kind of things, you should use a Dictionary<string, int>. That's a string associated with a number, a name associated with votes.
(More info about that here: http://www.dotnetperls.com/dictionary)

You should use a Dictionary for this:
static void Main(string[] args)
{
var candidates = new Dictionary<string, int>();
Console.WriteLine("Enter number of candidates");
var size = int.Parse(Console.ReadLine());
for (int i = 0; i < size; i++)
{
Console.WriteLine("Enter a Candidate Name");
var name = Console.ReadLine();
Console.WriteLine("Enter number of votes thus far");
var votes = int.Parse(Console.ReadLine());
candidates.Add(name, votes);
}
Console.WriteLine("Average votes: {0}", candidates.Average(entry => entry.Value));
Console.WriteLine("Min number of votes is: {0}", candidates.Min(entry => entry.Value));
}

You should probably use dictionary instead, but if you want to use array, here's how to do it :
avgMarks = numOfVotes.Average();
int avgIndex = numOfVotes.ToList().IndexOf(avgMarks);
Console.WriteLine("Average votes: {0} Candidate Names: {1}", avgMarks, candidateName[avgIndex]);

Your code just works fine. What you are looking for is the index of the array having highest number of votes. This index will be also useful to get the candidateName having highest number of vote. So, to get that index simply use the maximum value you got from this line :
int max = numOfVotes.Max();
and then use IndexOf static method to find the index of max in your array. For that try this line of code :
int index = Array.IndexOf<int>(numOfVotes, max);
Now simply print out the candidateName and highest number of votes as below :
Console.WriteLine(candidateName[index] + " has highest number of vote : " + numOfVotes[index] );
You can have a clean conception about Array.IndexOf() from DotNetPerls and MSDN

The solution to the problem, as it is, is to do something like this:
int indexOfWinner = Array.IndexOf(numOfVotes, numOfVotes.Max());
string winner = candidateName[indexOfWinner];
I don't know how far along in your C# and programming education you are (OOP and suchlike), so a couple of points you may find obvious or not:
you should use generic collections and not primitive arrays (List<> in this case).
you should encapsulate all this into a class.
This is how I would do it:
class Program
{
static void Main(string[] args) {
Candidates c = new Candidates("foo", "bar", "baz");
Random rand = new Random();
c.addVote(0, rand.Next(100));
c.addVote(1, rand.Next(100));
c.addVote(2, rand.Next(100));
Console.WriteLine(c.getWinner());
Console.WriteLine("number of votes:");
Console.WriteLine(c[0] + ": " + c[0].numberOfVotes);
Console.WriteLine(c[1] + ": " + c[1].numberOfVotes);
Console.WriteLine(c[2] + ": " + c[2].numberOfVotes);
}
}
class Candidates
{
private List<Candidate> candidates;
public Candidate this[string name] {
get {
return candidates.First(v => v.name == name);
}
}
public Candidate this[int index] {
get {
return candidates[index];
}
}
public Candidates(string firstCandidate, params string[] candidates) { //this is done to disable an empty constructor call
//and also allow multiple candidates
this.candidates = new List<Candidate>(candidates.Length);
this.candidates.Add(new Candidate(firstCandidate));
foreach(var c in candidates) {
this.candidates.Add(new Candidate(c));
}
}
public void addVote(int candidateNumber, int numberOfVotes = 1) {
candidates[candidateNumber].numberOfVotes += numberOfVotes;
}
public Candidate getWinner() {
candidates.Sort((candidate1, candidate2) => candidate2.numberOfVotes.CompareTo(candidate1.numberOfVotes));
return candidates[0];
}
}
class Candidate
{
public string name { get; private set; }
public int numberOfVotes { get; set; }
public Candidate(string name) {
this.name = name;
this.numberOfVotes = 0;
}
public override string ToString() {
return name;
}
}

Related

Trying to sort a List with dynamic List Size, having Unhandled Exceptions

I'm trying to creat an algorithm who receive a list of user-defined size (dynamic list) but i keep having unhandled exceptions when trying to implement the algorithm.
The cleanest i found was this
using System.Linq;
using System.Collections.Generic;
static void Sorter()
{
Console.WriteLine("Write the number of words the dictionary will have");
int Size = Convert.ToInt32(Console.ReadLine());
int ListEnd = Size;
List<string?> wordList = new() {};
while (Size>0)
{
Console.WriteLine("Write a term A");
string? item1 = "" ?? "1";
item1 = Console.ReadLine();
wordList.Add(item1);
Size--;
}
Console.WriteLine($"{wordList.Count()} terms");
Console.WriteLine(string.Join(" ", wordList));
string? last = wordList[ListEnd];
wordList.Sort();
Console.WriteLine(string.Join(" ", wordList));
}
I want the program to receive all the unsorted names written by the user and then reposition then in the List in a alphabetically sorted way.
First of all, if you want to declare an empty list, you can do it with:
List<string?> wordList = new List<string?>();
Then, A namespace cannot directly contain members such as fields or methods. Thats why you need to wrap the function by a class:
public class AnyClassName
{
static void Sorter()
{
Console.WriteLine("Write the number of words the dictionary will have");
int Size = Convert.ToInt32(Console.ReadLine());
int ListEnd = Size - 1;
List<string?> wordList = new List<string?>();
while (Size>0)
{
Console.WriteLine("Write a term A");
string? item1 = "" ?? "1";
item1 = Console.ReadLine();
wordList.Add(item1);
Size--;
}
Console.WriteLine($"{wordList.Count()} terms");
Console.WriteLine(string.Join(" ", wordList));
string? last = wordList[ListEnd];
wordList.Sort();
Console.WriteLine(string.Join(" ", wordList));
}
}
Then, you need to call the function in main function:
using System;
using System.Linq;
using System.Collections.Generic;
public class AnyClassName
{
static void Sorter()
{
Console.WriteLine("Write the number of words the dictionary will have");
int Size = Convert.ToInt32(Console.ReadLine());
int ListEnd = Size - 1;
List<string?> wordList = new List<string?>();
while (Size>0)
{
Console.WriteLine("Write a term A");
string? item1 = "" ?? "1";
item1 = Console.ReadLine();
wordList.Add(item1);
Size--;
}
Console.WriteLine($"{wordList.Count()} terms");
Console.WriteLine(string.Join(" ", wordList));
string? last = wordList[ListEnd];
wordList.Sort();
Console.WriteLine(string.Join(" ", wordList));
}
public static void Main(string[] args)
{
Sorter();
}
}
A couple pointers, as pointed out in the comments of your original post (pun not intended):
Try to include any errors when stating that you are seeing errors.
Try to specify what you are actually looking for. Are you looking for a cleaner implementation? Are you looking for a fix to an error (if so, refer to the previous pointer)?
As for the direct answer to your question, I am assuming that you are looking for a cleaner implementation that doesn't result in errors during compilation. Here we go:
using static System.Int32;
List<string> ReadStringsFromConsole()
{
int numWords = 0;
string word = string.Empty;
List<string> words = new();
// Keep on reading until we get a valid number
while (numWords < 1)
{
Console.Clear();
Console.Write("How many words would you like to enter? ");
bool unused = TryParse(Console.ReadLine(), out numWords); // TryParse to make sure we don't get an exception
}
// Keep on reading until we get a valid word, at which point it is added to the list
// numWords is only decremented if we were able to successfully add the word to the list
do
{
Console.Clear();
Console.Write("Enter a word: ");
word = Console.ReadLine() ?? string.Empty;
if (word == string.Empty) continue;
words.Add(word);
numWords--;
} while (numWords > 0 && word != string.Empty);
words.Sort();
return words;
}
List<string> words = ReadStringsFromConsole();
Console.WriteLine(string.Join(" ", words));
Console.ReadKey();

Function that writes out the sum only writes out 0

Problem: The sum writes out as 0. How do i make it so that the function Summa() writes out the actual sum out of the 10 numbers that user writes in?
This is probably extremely simple but im new to this :P
All of the things in the code weren't separate before but i wanted if i could move the sum part to it's own function
using System;
namespace Array
{
class Program
{
static void Main(string[] args)
{
int number;
int[] vektor = new int[10];
for (int i = 0; i < vektor.Length; i++)
{
Console.WriteLine("Enter a number");
number = Convert.ToInt32(Console.ReadLine());
vektor[i] = number;
}
Summa();
}
static void Summa()
{
int sum = 0;
int[] vektor = new int[10];
int i = 0;
sum = sum + vektor[i];
Console.WriteLine("The amount is " + sum);
}
}
}
You are creating a new array called vektor in the Summa() function, instead of using the one created in the main() function. Also you've to iterate through the array to find the sum
Change Summa to :
static void Summa(int[] vektor)
{
int sum = 0;
for(int i=0; i < vektor.Length; i++)
{
sum = sum + vektor[i];
}
Console.WriteLine("The amount is " + sum);
}
And change the function call in the main() to:
Summa(vektor);
You are summing a different array; you'd need to pass the array in:
static void Summa(int[] vektor)
{
int sum = 0;
foreach (var val in vektor)
sum += val;
// ^^^ or just use: var sum = vektor.Sum();
Console.WriteLine("The amount is " + sum);
}
/// ...
Summa(vektor);
So, you're not passing any information to your method "Summa", but you're instead creating a new Array.
So you need to pass the Array from your main method to you "summa" method.
namespace Array
{
class Program
{
static void Main(string[] args)
{
int number;
int[] vektor = new int[3];
for (int i = 0; i < vektor.Length; i++)
{
Console.WriteLine("Enter a number");
number = Convert.ToInt32(Console.ReadLine());
vektor[i] = number;
}
Summa(vektor);
}
static void Summa(int[] vektor)
{
int sum = 0;
foreach (var item in vektor)
{
sum += vektor[item];
}
Console.WriteLine("The amount is " + sum);
}
}
}
Also, using foreach loops will make your life much easier in the future.
https://www.w3schools.com/cs/cs_for_loop.asp
When you're unsure what your program is doing and don't know why it isn't working like it should. Try using the debug feature.
If you're using Visual Studio you can access it by pressing F11.
Lycka till med studierna!

Method being run through twice when it should only run once

class Myclass
{
public string Driver1()
{
string a = "";
Console.Write("Please enter drivers name: ");
a = Console.ReadLine();
return a;
}
public int numberOfTrips()
{
int a = 0;
{
Console.Write("Enter the number of trips: ");
a = Convert.ToInt32(Console.ReadLine());
}
return a;
}
public List<float> Payments()
{
List<float> a = new List<float>();
float input;
for (int i = 0; i<numberOfTrips(); i++)
{
Console.Write("Enter payment {0}: ", (1 + i));
input = float.Parse(Console.ReadLine());
Console.WriteLine("Payment added");
a.Add(input);
}
return a;
}
}
class Program
{
static void Main(string[] args)
{
Myclass a = new Myclass();
string name = a.Driver1();
int trip = a.numberOfTrips();
float total = a.Payments().Sum();
Console.WriteLine("\nDriver: {0}\n" + "Number of trips: {1}\n" + "Total payment: {2}\n", name, trip, total);
}
}
The issue i am having is that the "public int numberOfTrips()" method is running twice before it gets to the method containing the for loop. I think this is to do with the fact i am using it within the for loop to specify when the loop should stop. I am guessing i have done this wrong so how would i correct this? I need the user to be able to set the how many times it will ask for payment.
Any help is appreciated.
Just pass the number from numberOfTrips as a parameter to Payments:
public List<float> Payments(int tripCount)
{
List<float> a = new List<float>();
float input;
for (int i = 0; i < tripCount; i++)
{
Console.Write("Enter payment {0}: ", (1 + i));
input = float.Parse(Console.ReadLine());
Console.WriteLine("Payment added");
a.Add(input);
}
return a;
}
In your Main method:
Myclass a = new Myclass();
string name = a.Driver1();
int trip = a.numberOfTrips();
float total = a.Payments(trip).Sum();
Instead of calling numberOfTrips() in Main() as well as in Payments() you might try creating an instance variable or static variable in MyClass. Then you can fetch the number of trips from that variable after all payments are calculated.
That is correct. The first time it runs is in Main to set the 'trip' variable. The second time it runs is in Payments, inside the for loop declaration.

Two instances of a class: only sort 1

I have a basic class that has four attributes (Patient). In Main() I have reserved the memory for the array and before I actually create the instance, I ask the user for the account number to ensure it doesn't already exist within the array. So BinarySearch requires it to be sorted but as soon as I sort it the ability to do the for loop is lost.
//Variables
int intMaxNum = 5; //set max number of patients to 5
int intInputValue;
int intResult;
string strTempName;
int intTempAge;
double dblTempTotal;
Patient[] objectPatient = new Patient[intMaxNum]; //create an array of references
for (int x = 0; x < objectPatient.Length; ++x)
{
//attempt to create a 'shadow' class to search through and keep integrity of main class (objectPatient)
Patient[] tempobjectPatient = new Patient[intMaxNum];
tempobjectPatient = objectPatient;
if (x > 0)
{
Console.Write("\n***Next Patient***");
Array.Sort(tempobjectPatient); //this will sort both objects even though I send the temporary class only - interface impact I'm sure
}
//ask for the Patient Account number
Console.Write("\nEnter Patient Account Number: ");
ReadTheAccountNumber:
intInputValue = Convert.ToInt32(Console.ReadLine());
//create temporary class for comparison
Patient SeekPatient = new Patient();
SeekPatient.PatientNumber=intInputValue; // reset the default info with the input Pateint Account Number
//verify the Patient Account number doesn't already exist
intResult = Array.BinarySearch(tempobjectPatient, SeekPatient);
//intResult = Array.BinarySearch(objectPatient, SeekPatient);
//if (objectPatient.Equals(SeekPatient)) //Can not get the .Equals to work at all...
if (intResult >= 0)
{
Console.Write("\nSorry, Patient Account Number {0} is a duplicate.", intInputValue);
Console.Write("\nPlease re-enter the Patient Account Number: ");
goto ReadTheAccountNumber;
}
else //no match found, get the rest of the data and create the object
{
if (x > 0) { Console.Write("***Patient Account Number unique and accepted***\n"); } //looks silly to display this if entering the first record
Console.Write("Enter the Patient Name: ");
strTempName = Console.ReadLine();
Console.Write("Enter the Patient Age: ");
intTempAge = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the total annual Patient amount due: ");
dblTempTotal = Convert.ToDouble(Console.ReadLine());
objectPatient[x] = new Patient(intInputValue, strTempName, intTempAge, dblTempTotal);
}
}
Here is the class:
class Patient : IComparable
{
//Data fields
private int patientNumber;
private string patientName;
private int patientAge;
private double patientAmountDue;
//Constructors
public Patient(): this(9,"ZZZ",0,0.00)
{
}
public Patient(int _patientNumber, string _patientName, int _patientAge, double _patientAmountDue)
{
PatientNumber = _patientNumber;
PatientName = _patientName;
PatientAge = _patientAge;
PatientAmountDue = _patientAmountDue;
}
//Properties
public int PatientNumber
{
get { return patientNumber; }
set { patientNumber = value; }
}
public string PatientName
{
get { return patientName; }
set { patientName = value; }
}
public int PatientAge
{
get { return patientAge; }
set { patientAge = value; }
}
public double PatientAmountDue
{
get { return patientAmountDue; }
set { patientAmountDue = value; }
}
//Interfaces
int IComparable.CompareTo(Object o)
{
int returnVal; //temporary value container
Patient temp = (Patient)o; //create temp instance of the class
if (this.PatientNumber > temp.PatientNumber)
returnVal = 1;
else
if (this.PatientNumber < temp.PatientNumber)
returnVal = -1;
else
returnVal = 0; //exact match
return returnVal;
}
}
You can put all the patient numbers into HashSet<int> and test via Contains() if a number is allocated one:
class Patient : IComparable {
...
// Simplest, not thread safe
private static HashSet<int> s_AllocatedPatientNumbers = new HashSet<int>();
public static Boolean IsNumberAllocated(int patientNumber) {
return s_AllocatedPatientNumbers.Contains(patientNumber);
}
public int PatientNumber {
get {
return patientNumber;
}
set {
s_AllocatedPatientNumbers.Remove(patientNumber);
patientNumber = value;
s_AllocatedPatientNumbers.Add(patientNumber);
}
}
}
So whenever you need to test if the number has been allocated you have no need to create a temporal patient, sort the array etc. just one simple call:
if (Patient.IsNumberAllocated(intInputValue)) {
...
}
This
Patient[] tempobjectPatient = new Patient[intMaxNum];
creates a new array and assigns it to tempobjectPatient. But this new array is never used, because here
tempobjectPatient = objectPatient;
you immediately assign the old one to tempobjectPatient. So after this, you don't have two array instances. Both tempobjectPatient and objectPatient refer to the same instance.
You probably want:
Patient[] tempobjectPatient = (Patient[])objectPatient.Clone();
Replace the Array with a Dictionary, it is desigend to be used with unique keys:
Dictionary<int,Patient> patients = new Dictionary<int,Patient>();
while (true)
{
Console.Write("\nEnter Patient Account Number: ");
int number = Convert.ToInt32(Console.ReadLine());
if (patients.ContainsKey(number))
{
Console.Write("\nSorry, Patient Account Number {0} is a duplicate.", number);
Console.Write("\nPlease re-enter the Patient Account Number: ");
continue;
}
Console.Write("***Patient Account Number unique and accepted***\n");
Console.Write("Enter the Patient Name: ");
string name = Console.ReadLine();
Console.Write("Enter the Patient Age: ");
int age = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the total annual Patient amount due: ");
double amountDue = Convert.ToDouble(Console.ReadLine());
patients.Add(number, new Patient(number, name, age, amountDue));
}
The loop is now missing an exit condition.
EDIT:
While this does not answer the question of the title i think it is what the OP was looking for.

The array dont save my numbers

I am trying to learn C# and doing some questions i googeld. This is the task to do:
*"Beginner level:
The task is to make
a dice game where the user throws 3
Each 12-sided dice (numbers
shall be randomly selected and stored in an array / field or list).
Add up the total of the dice and show on the screen.
Create a function / method that accepts a figure
(the total sum of the dice). Function / method
should return the text "Good throw" if the figure
is higher or equal to 20.
In all other cases, the text
"Sorry" is returned.
Call the function / method in the main method
and prints the total and the text.
Advanced level:
This is an extension of the task where you must use a class to simulate a dice. The user shall have the option of writing a x y-sided dice himself.
If the total sum of a roll of the dice generates a score that is> 50% of the maximum score, the words "good throw" is displayed.
This logic can be in your main method.
Create the class described in the class diagram and use appropriate
way in your code."*
The thing is that i cant get it to work, the array in my class do not save my numbers im typing in... I only get the reslut 0. I think i have just done some big misstake i cant see...
This is the Main code:
static void Main(string[] args)
{
List<Dice> _Dice = new List<Dice>();
int a = 0;
int times = int.Parse(Interaction.InputBox("Write how many times you want to repeat the game:"));
while (a != times)
{
int antThrow = int.Parse(Interaction.InputBox("Write how many times you want each dice to get thrown:"));
int xChoice = int.Parse(Interaction.InputBox("Write how many dice you want to throw:"));
int yChoice = int.Parse(Interaction.InputBox("Write how many sides you want each dice should have:"));
_Dice.Add(new Dice(xChoice,yChoice, antThrow));
a++;
}
int e = 1;
foreach (var item in _Dice)
{
Interaction.MsgBox(string.Format("Result of game {0}: {1}", e++, item.Tostring()));
}
}
This is the Dice class:
class Dice
{
static int _xChoice, _yChoice, _throw;
static List<int> sum = new List<int>();
static int w = 0;
static int _sum;
static int[,] dice = new int[_xChoice, _yChoice];
public string Tostring()
{
int half = _sum / 2;
if (half <= _sum/2)
{
return "Good throw!" + _sum;
}
else
{
return "Bad throw!";
}
}
void random()
{
Random rnd = new Random();
while (w != _throw)
{
for (int i = 0; i < dice.GetLength(0); i++)
{
for (int j = 0; j < dice.GetLength(1); j++)
{
dice[i, j] = rnd.Next(1, _yChoice);
_sum += dice[j, i];
sum.Add(_sum);
}
}
w++;
}
}
public Tarning(int Xchoice, int Ychoice, int throw)
{
_throw = thorw;
_xChoice = Xchoice;
_yChoice = Ychoice;
}
}
Your main problem is in the static keyword. Static field means that there's only
one field for all the instances, which is not your case: you need each instance of Dice has its own fields' values.
class Dice {
// no static here
private int _xChoice, _yChoice, _throw;
// no static here
private List<int> sum = new List<int>();
// no static here
private int w = 0;
// no static here
private int _sum;
// no static here
private int[,] dice = new int[_xChoice, _yChoice];
// BUT, you want a random generator for all the instances, that's why "static"
private static Random rnd = new Random();
// When overriding method mark it with "override"
// And Be Careful with CAPitalization:
// the method's name "ToString" not Tostring
public override string ToString() {
...
}
void random() {
// Do not create Random generator each time you call it:
// It makes the random sequences skewed badly!
// Istead use one generator for all the calls, see the code above
// private static Random rnd = new Random();
// Random rnd = new Random();
...
}
...
class Program
{
static void Main(string[] args)
{
var dice = new List<DiceLogic>();
int a = 0;
int times = GetTimes();
while (a != times)
{
int antThrow = GetAntThrow();
int xChoice = GetXChoice();
int yChoice = GetYChoice();
dice.Add(new DiceLogic(xChoice, yChoice, antThrow));
a++;
}
int e = 1;
foreach (var item in dice)
{
Console.WriteLine("Result of game {0}: {1}", e++, item.Tostring());
}
Console.ReadLine();
}
private static int GetTimes()
{
while (true)
{
Console.WriteLine("Write how many times you want to repeat the game:");
int times;
var result = int.TryParse(Console.ReadLine(), out times);
if (result) return times;
Console.WriteLine("Value must be a number.");
}
}
private static int GetAntThrow()
{
while (true)
{
Console.WriteLine("Write how many times you want each dice to get thrown:");
int antThrow;
var result = int.TryParse(Console.ReadLine(), out antThrow);
if (result) return antThrow;
Console.WriteLine("Value must be a number.");
}
}
private static int GetXChoice()
{
while (true)
{
Console.WriteLine("Write how many dice you want to throw:");
int getXChoice;
var result = int.TryParse(Console.ReadLine(), out getXChoice);
if (result) return getXChoice;
Console.WriteLine("Value must be a number.");
}
}
private static int GetYChoice()
{
while (true)
{
Console.WriteLine("Write how many sides you want each dice should have:");
int getXChoice;
var result = int.TryParse(Console.ReadLine(), out getXChoice);
if (result) return getXChoice;
Console.WriteLine("Value must be a number.");
}
}
}
public class DiceLogic
{
public string Tostring()
{
int maxScore = _diceSides*_dices;
if (_result >= maxScore / 2)
{
return "Good throw! " + _result;
}
return "Bad throw! " + _result;
}
private readonly int _dices;
private readonly int _diceSides;
private readonly int _throwDice;
private int _result;
private void CalculateResult()
{
var rnd = new Random();
for (int i = 0; i < _dices; i++)
{
int currentResult = 0;
for (int j = 0; j < _throwDice; j++)
{
currentResult = rnd.Next(0, _diceSides);
}
_result += currentResult;
}
}
public DiceLogic(int dices, int diceSides, int throwEachDice)
{
_dices = dices;
_diceSides = diceSides;
_throwDice = throwEachDice;
CalculateResult();
}
}
This is an example of how you could implement what they are asking, go through te code line by line with the debugger so you understand what is going on.
You never call the method random(). Therefore, the value of your member variable _sum is never changed and remains 0. You need to call the method random() somewhere. You should probably make it public and call it from your main method after you have set up all your dice.
Furthermore, your member variables in the Dice class are all static! That means that the different Dice instances will all share the same values. I think this is not intended. You should make the variables instance variables by removing the static modifier.
Your method Tarning is not called and it takes a reserved word “throw” [I believe it was supposed to be thorw]. The method is not void so it must return a type.
Random() is not invoked and does display anything.
Dice has not constructor that has 3 arguments within its braces but it’s declared as _Dice.Add(new Dice(xChoice,yChoice, antThrow));

Categories