2017-09-08 5 views
-3

Ich versuche derzeit C# durch dotnetacademy zu lernen. In der letzten Prüfung bin ich auf den letzten Schritt, in dem die Anweisungen sind:dotnetacademy Abschlussprüfung Ausgabe C#

  1. Bitte ändern Sie den Eintrag Punkt-Methode eine Instanz der Umfrage Art zu erklären. Fügen Sie nach der Deklaration der Umfrage Ihrer Wahl zwei Textfragen hinzu.

  2. Erstellen Sie als Nächstes nach den beiden Fragedeklarationen eine neue lokale Deklaration, die das Ergebnis der GetScore-Methode zurückgibt.

  3. Zum Schluss die Nachricht "Your score: {Score}" mit WriteLine an die Konsole drucken, wobei {Score} der aus der Umfrage erhaltene Score ist.

Ich habe es geschafft, die letzten beiden Schritte, die ich denke, abzuschließen, weil ich die Partitur drucken kann. Ich brauche 1.

hier mit Schritt helfen, ist mein Code:

using System; using System.Collections.Generic; public class Program { public static void Main(string [] args) { Survey survey = new Survey("Survey"); survey.Questions.AsReadOnly(); TextQuestion tq = new TextQuestion(); survey.AddQuestion((Question)tq.Ask()); int score = survey.GetScore(); Console.Write("Your score: {0}", score); } } public abstract class Answer { public int Score { get; set; } } public abstract class Question { public string Label { get; set; } protected abstract Answer CreateAnswer(string input); protected virtual void PrintQuestion() { Console.WriteLine(Label); } public Answer Ask() { PrintQuestion(); string input = Console.ReadLine(); return CreateAnswer(input); } } public class TextAnswer : Answer { public string Text { get; set; } } public class TextQuestion : Question { protected override Answer CreateAnswer(string input) { return new TextAnswer { Text = input, Score = input.Length }; } } public class Survey { public Survey(string title) { Title = title; Questions = new List<Question>(); } public string Title { get; set; } public List<Question> Questions { get; private set; } public void AddQuestion(Question question) { Questions.Add(question); } public int GetScore() { int total = 0; foreach (Question question in Questions) { Answer answer = question.Ask(); total = total + answer.Score; } return total; }

Ich kann nicht eine Frage der Umfrage Instanz hinzugefügt werden, da es eine Frage Parameter der Fragetyp erfordert. Irgendwelche Vorschläge?

Und schließlich, das ist der Link zu der Übung: Exercise

+0

Versuchen 'survey.AddQuestion (TQ)' ? – Sybren

+0

vor der Umfrage.AddQuestion (tq) put tq.Ask(); – DanielVorph

+2

Die Tutorials in diesem Kurs erklären, was zu tun ist, um die Frage zu vervollständigen, und wenn Sie sich ansehen, wo Sie bisher sind, sieht es so aus, als müssten Sie etwas mehr lernen. Bitte verwenden Sie SO nicht für Hilfe bei Prüfungen, da dies Ihre Lernerfahrung beeinträchtigt. – Kodaloid

Antwort

0

Mit Hilfe von euch es jetzt passiert:

public static void Main() 
{ 
    Survey survey = new Survey("Survey"); 
    Question tq1 = new TextQuestion(); 
    Question tq2 = new TextQuestion(); 
    tq1.Ask(); 
    tq2.Ask(); 
    survey.AddQuestion(tq1); 
    survey.AddQuestion(tq2); 

    int score = survey.GetScore(); 
    Console.WriteLine("Your score: {0}", score); 

} 

}