0

I have this code all working good - it only accepts 20 input from user - compare them with the correct answers and finally show the result that which questions are not correct- code is working - shows up the result BUT not separated. its like 156791012 are incorrect. see the attachment.

enter image description here

    static void Main(string[] args)
    {
        char[] studentAnswers = new char[20];            
        char[] answers = new char[] { 'E', 'D', 'D', 'B', 'A', 'C', 'E', 'B', 'D', 'C', 'D', 'A', 'A', 'D', 'E', 'E', 'A', 'E', 'A', 'D' };
        int[] wrongAnswers = new int[20];            
        int correctlyAnswered = 0;
        int falselyAnswered = 0;
        string list = "";


        for (int i = 0; i < studentAnswers.Length; i++)
        {
            studentAnswers[i] = InputOutput_v1.GetValidChar("Question ",i);

            if (studentAnswers[i] == answers[i])
            {
                correctlyAnswered = correctlyAnswered + 1;
            }
            else
            {
                falselyAnswered = falselyAnswered + 1;                    
                wrongAnswers[i] = i + 1;
                list += i + 1;

            }

        }
        if (correctlyAnswered >= 15)
        {
            Console.WriteLine("Passed with {0} correct answers",correctlyAnswered);
        }
        else
        {
            Console.WriteLine("Failed with {0} incorrect answers. Incorrect answers are: {1} ", falselyAnswered, list);
        }
        Console.ReadLine();
    }
2
  • if (list == "") then list = i + 1 else list += ',' + i + 1; . Commented Mar 28, 2017 at 18:56
  • String.Join is probably what you want. Commented Mar 28, 2017 at 18:58

2 Answers 2

1

Your are looking for string.Join (and Linq):

using System.Linq;

... 

string list = string.Join(", ", studentAnswers
  .Where((answer, i) => answer != InputOutput_v1.GetValidChar("Question ", i))
  .Select((answer, i) => i + 1));
Sign up to request clarification or add additional context in comments.

Comments

0

Replace

list+= i+1;
Console.WriteLine("Failed with {0} incorrect answers. Incorrect answers are: {1} ", falselyAnswered, list);

to this

list+= i+1 + ",";
Console.WriteLine("Failed with {0} incorrect answers. Incorrect answers are: {1} ", falselyAnswered, list.Remove(list.Length-2));

2 Comments

How about the last comma at the end? how can i get rid of that?
@IvyKnm list.Remove(list.Length-2) removes last come at the end

Your Answer

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