C# 获取调查C中选择的用户数#

C# 获取调查C中选择的用户数#,c#,windows,class,C#,Windows,Class,我正在用C#为课堂活动编写代码。其中一个要求是必须获得用户选择该选项的次数,然后将其打印出来。我已经设法让他们输入所有问题的有效输入,只需输入1、2或3即可 我可以在代码末尾打印它被选中的次数吗?还有,如果他们想再次重复所有问题,我如何让他们做出选择 我被卡住了,不知道怎么写出来。感谢您的帮助。谢谢 using System.Collections.Generic; using System.Text; namespace Week_3__wed_ { class Activity4

我正在用C#为课堂活动编写代码。其中一个要求是必须获得用户选择该选项的次数,然后将其打印出来。我已经设法让他们输入所有问题的有效输入,只需输入1、2或3即可


我可以在代码末尾打印它被选中的次数吗?还有,如果他们想再次重复所有问题,我如何让他们做出选择

我被卡住了,不知道怎么写出来。感谢您的帮助。谢谢

using System.Collections.Generic;
using System.Text;

namespace Week_3__wed_
{
    class Activity4
    {
        static void Main(string[] args)
        {
            Console.WriteLine("4 question Survey!");

            //Questions below with survey question while-looping until user input is 1, 2 or 3

            Console.WriteLine("How are you feeling?");
            Console.WriteLine("(1) Okay (2) Good (3) Really Good");
            int survey1 = Convert.ToInt32(Console.ReadLine());

            while (survey1 < 1 || survey1 > 3)
            {
                Console.WriteLine("Please enter a valid choice");
                survey1 = Convert.ToInt32(Console.ReadLine());
            }

            Console.WriteLine("How would you describe your mood?");
            Console.WriteLine("(1) Fine (2) Happy (3) Excited");
            int survey2 = Convert.ToInt32(Console.ReadLine());

            while (survey2 < 1 || survey2 > 3)
            {
                Console.WriteLine("Please enter a valid choice");
                survey2 = Convert.ToInt32(Console.ReadLine());
            }


            Console.WriteLine("Do you have plans for today?");
            Console.WriteLine("(1) Yes (2) No (3) Possibly");
            int survey3 = Convert.ToInt32(Console.ReadLine());

            while (survey3 < 1 || survey3 > 3)
            {
                Console.WriteLine("Please enter a valid choice");
                survey3 = Convert.ToInt32(Console.ReadLine());
            }


            Console.WriteLine("Have you had a good day so far?");
            Console.WriteLine("(1) Yes (2) Not Really (3) No!");
            int survey4 = Convert.ToInt32(Console.ReadLine());

            while (survey4 < 1 || survey4 > 3)
            {
                Console.WriteLine("Please enter a valid choice");
                survey4 = Convert.ToInt32(Console.ReadLine());
            }
        }
    }
}
使用System.Collections.Generic;
使用系统文本;
命名空间星期三星期三_
{
班级活动4
{
静态void Main(字符串[]参数)
{
Console.WriteLine(“4个问题调查!”);
//以下问题与调查问题一起循环,直到用户输入为1、2或3
你感觉怎么样;
Console.WriteLine(“1)好(2)好(3)真的好”);
int survey1=Convert.ToInt32(Console.ReadLine());
而(测量1<1 | |测量1>3)
{
Console.WriteLine(“请输入有效选项”);
survey1=Convert.ToInt32(Console.ReadLine());
}
WriteLine(“你会如何描述你的情绪?”);
(1)罚款(2)快乐(3)兴奋);
int survey2=Convert.ToInt32(Console.ReadLine());
而(测量2<1 | |测量2>3)
{
Console.WriteLine(“请输入有效选项”);
survey2=Convert.ToInt32(Console.ReadLine());
}
你今天有计划吗;
控制台写线(“(1)是(2)否(3)可能”);
int survey3=Convert.ToInt32(Console.ReadLine());
而(调查3<1 | |调查3>3)
{
Console.WriteLine(“请输入有效选项”);
survey3=Convert.ToInt32(Console.ReadLine());
}
到目前为止你过得好吗;
Console.WriteLine(“(1)是(2)不是真的(3)不是!”);
int survey4=Convert.ToInt32(Console.ReadLine());
而(调查4<1 | |调查4>3)
{
Console.WriteLine(“请输入有效选项”);
survey4=Convert.ToInt32(Console.ReadLine());
}
}
}
}

我已编辑了您的代码,使其能够满足您的要求:

int cont = 1;
while (cont == 1) // jut add while loop :)
{
    Console.WriteLine("4 question Survey!");

    int[] questionCount = new int[3]; // 3 options as answers
    //Questions below with survey question while-looping until user input is 1, 2 or 3

    Console.WriteLine("How are you feeling?");
    Console.WriteLine("(1) Okay (2) Good (3) Really Good");
    int survey1 = Convert.ToInt32(Console.ReadLine());

    while (survey1 < 1 || survey1 > 3)
    {
        Console.WriteLine("Please enter a valid choice");
        survey1 = Convert.ToInt32(Console.ReadLine());
    }
    questionCount[survey1 - 1]++; // increase count based on the answer

    Console.WriteLine("How would you describe your mood?");
    Console.WriteLine("(1) Fine (2) Happy (3) Excited");
    int survey2 = Convert.ToInt32(Console.ReadLine());

    while (survey2 < 1 || survey2 > 3)
    {
        Console.WriteLine("Please enter a valid choice");
        survey2 = Convert.ToInt32(Console.ReadLine());
    }
    questionCount[survey2 - 1]++;

    Console.WriteLine("Do you have plans for today?");
    Console.WriteLine("(1) Yes (2) No (3) Possibly");
    int survey3 = Convert.ToInt32(Console.ReadLine());

    while (survey3 < 1 || survey3 > 3)
    {
        Console.WriteLine("Please enter a valid choice");
        survey3 = Convert.ToInt32(Console.ReadLine());
    }
    questionCount[survey3 - 1]++;

    Console.WriteLine("Have you had a good day so far?");
    Console.WriteLine("(1) Yes (2) Not Really (3) No!");
    int survey4 = Convert.ToInt32(Console.ReadLine());

    while (survey4 < 1 || survey4 > 3)
    {
        Console.WriteLine("Please enter a valid choice");
        survey4 = Convert.ToInt32(Console.ReadLine());
    }
    questionCount[survey4 - 1]++;

    Console.WriteLine($"aswered 1 count: {questionCount[0]}"); // printing out the results
    Console.WriteLine($"aswered 2 count: {questionCount[1]}");
    Console.WriteLine($"aswered 3 count: {questionCount[2]}");

    Console.WriteLine("Do you want to take the survey again?"); // asking whether they want to take the survey again
    Console.WriteLine("(1) Yes (2) No!");
    cont = Convert.ToInt32(Console.ReadLine());

    while (cont < 1 || cont > 2)
    {
        Console.WriteLine("Please enter a valid choice");
        cont = Convert.ToInt32(Console.ReadLine());
    }
}
int cont=1;
while(cont==1)//jut add while循环:)
{
Console.WriteLine(“4个问题调查!”);
int[]questionCount=new int[3];//3个选项作为答案
//以下问题与调查问题一起循环,直到用户输入为1、2或3
你感觉怎么样;
Console.WriteLine(“1)好(2)好(3)真的好”);
int survey1=Convert.ToInt32(Console.ReadLine());
而(测量1<1 | |测量1>3)
{
Console.WriteLine(“请输入有效选项”);
survey1=Convert.ToInt32(Console.ReadLine());
}
questionCount[调查1-1]++;//根据答案增加计数
WriteLine(“你会如何描述你的情绪?”);
(1)罚款(2)快乐(3)兴奋);
int survey2=Convert.ToInt32(Console.ReadLine());
而(测量2<1 | |测量2>3)
{
Console.WriteLine(“请输入有效选项”);
survey2=Convert.ToInt32(Console.ReadLine());
}
问题计数[调查2-1]+;
你今天有计划吗;
控制台写线(“(1)是(2)否(3)可能”);
int survey3=Convert.ToInt32(Console.ReadLine());
而(调查3<1 | |调查3>3)
{
Console.WriteLine(“请输入有效选项”);
survey3=Convert.ToInt32(Console.ReadLine());
}
问题计数[调查3-1]+;
到目前为止你过得好吗;
Console.WriteLine(“(1)是(2)不是真的(3)不是!”);
int survey4=Convert.ToInt32(Console.ReadLine());
而(调查4<1 | |调查4>3)
{
Console.WriteLine(“请输入有效选项”);
survey4=Convert.ToInt32(Console.ReadLine());
}
问题计数[调查4-1]+;
Console.WriteLine($“aswered 1计数:{questionCount[0]}”);//打印结果
WriteLine($“aswered 2计数:{questionCount[1]}”);
WriteLine($“aswered 3计数:{questionCount[2]}”);
Console.WriteLine(“您想再做一次调查吗?”);//询问他们是否想再做一次调查
Console.WriteLine(“(1)是(2)否!”);
cont=Convert.ToInt32(Console.ReadLine());
而(续<1 | |续>2)
{
Console.WriteLine(“请输入有效选项”);
cont=Convert.ToInt32(Console.ReadLine());
}
}
但是如果你有很多重复的代码。如果您还有更多问题,我强烈建议您为其创建一个类并使用循环:

class Program
{
    static void Main(string[] args)
    {
        Question[] questions = new Question[]
        {
            new Question("How are you feeling?", "(1) Okay (2) Good (3) Really Good"),
            new Question("How would you describe your mood?", "(1) Fine (2) Happy (3) Excited"),
            new Question("Do you have plans for today?", "(1) Yes (2) No (3) Possibly"),
            new Question("Have you had a good day so far?", "(1) Yes (2) Not Really (3) No!")
        };
        bool cont = true;
        while (cont)
        {
            int[] ansCount = new int[3];
            foreach (Question q in questions)
                ansCount[q.Ask() - 1]++;
            for (int i = 0; i < ansCount.Length; i++)
                Console.WriteLine($"Answered {i + 1} count: {ansCount[i]}");
            Console.WriteLine("Enter 1 to repeat survey, or anything else to continue");
            cont = Console.ReadLine() == "1";
        }
    }
}

class Question
{
    public string Quest { get; private set; }
    public string Answers { get; private set; }
    public int Answer { get; private set; }
        
    public Question(string question, string answers)
    {
        Quest = question;
        Answers = answers;
    }

    public int Ask()
    {
        Console.WriteLine(Quest);
        Console.WriteLine(Answers);
        Answer = Convert.ToInt32(Console.ReadLine());
        if ((Answer > 3) || (Answer < 1))
        {
            Console.WriteLine("Please enter valid choise");
            Answer = Convert.ToInt32(Console.ReadLine());
        }
        return Answer;
    }
}
类程序
{
静态void Main(字符串[]参数)
{
问题[]问题=新问题[]
{
新问题(“你感觉怎么样?”,“(1)还好(2)很好(3)真的很好”),
新问题(“你如何描述你的心情?”,“(1)好(2)快乐(3)兴奋”),
新问题(“你今天有计划吗?”,“(1)是(2)否(3)可能”),
新问题(“到目前为止你过得好吗?”,“(1)是(2)不是真的(3)不是!”)
};
bool cont=真;
while(续)
{
int[]ansCount=新的int[3];
foreach(问题中的问题q)
ansCount[q.Ask()-1]+;
for(int i=0;i