0

It's a pretty rookie question. Here is my code

double jury;
jury = double.Parse(Console.ReadLine());
for (int i = 0; i < jury ; i++)
{
    Console.ReadLine();
}

How can I define the input inside the loop so I can use the input data from the additional inputs for math calculations. It's a vote system and the first variable is the jury count - each jury member votes for a number 1-10. The idea is that the jury is dynamic from 1 to 100,000. Any ideas are welcome.

More: Here is the idea.

-> When you have for example 3 jury members

-> You input 3 on the first row

-> You get 3 new inputs where your jury vote for candidates ( numbers 1 to 10 )

-> In this case the votes are 1, 3, 3

The idea is to parse all this information and output the winner which in our case is "3".

2
  • 1
    You might want to go google some examples of Console.ReadLine Commented Mar 10, 2014 at 17:36
  • if you are using jury to store jury members count why it is double? can you have 3.5 juries ? Commented Mar 10, 2014 at 17:42

3 Answers 3

2

You can use List<T> to store votes; IMHO int looks better than Double in your task

Console.WriteLine("Enter number of jurors, please"); 

int jury;

if (!int.TryParse(Console.ReadLine(), out jury)) { 
  Console.WriteLine("Incorrect jury number format");

  return; // <- I'd exit on the first error occured; you may adopt different policy
}
else if ((jury < 1) || (jury > 100000)) {
  Console.WriteLine("Jury should be in range [1..100000]");

  return;
}

var List<int> = new List<int>();

for (int i = 0; i < jury; ++i) {
  int vote;

  Console.WriteLine("Enter next vote, please"); 

  if (!int.TryParse(Console.ReadLine(), out vote)) {
    Console.WriteLine("Incorrect vote format");

    return;
  }
  else if ((vote < 1) || (vote > 10)) {
    Console.WriteLine("Each vote should be in range [1..10]");

    return;
  }

  votes.Add(vote);
} 
Sign up to request clarification or add additional context in comments.

Comments

1

This code prompts for a number of jurors and validates the input. Then, it prompts for each juror's vote, validating the vote input.

const int minimumVote = 1;
const int maximumVote = 10;
int jurorCount;

do
{
    Console.Write("Enter the number of jurors: ");
} while (!Int32.TryParse(Console.ReadLine(), out jurorCount) || jurorCount < 0);

var votes = new List<int>();

for (int i = 0; i < jurorCount; i++)
{
    int vote;

    do
    {
        Console.Write("Enter juror #{0}'s vote ({1}-{2}): ", i + 1, minimumVote, maximumVote);
    } while (!Int32.TryParse(Console.ReadLine(), out vote) || vote < minimumVote || vote > maximumVote);

    votes.Add(vote);
}

1 Comment

I rewrote the code using a do/while instead of a while. That eliminated some extra lines. I also introduced two constants to keep from violating DRY.
0

Use a List or Array

var values = new List<double>();
for (int i = 0; i < jury; i++)
{
    double current;
    if (double.TryParse(Console.ReadLine(), out current))
    {
       values.Add(current);
    }
    else
      i--;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.