首页 > 解决方案 > 需要帮助创建控制台应用程序,该应用程序输出调查 1-5(整数)评级的百分比响应

问题描述

从事一项我需要完成以下任务的任务: 在一项调查中,一个问题要求被调查者从 1-5(整数)对某事进行评分。您的程序的最终用户在未知数量的调查中输入该问题的答案。编写一个允许此操作并输出每个值(1、2、3、4 和 5)的响应百分比的程序。

我做了一个以前的控制台应用程序,带有一个循环来收集平均值,我不确定如何收集 5 个不同可能输入的百分比响应。

下面是我之前的代码。

namespace WhileLoopsMean

public class MeanProgram

    static void Main(string[] args)
    {
        long test, sum, loop, count;
        double avg;
        Console.Write("How many tests? ");
        count = long.Parse(Console.ReadLine());
        sum = 0;
        loop = 1;
        while (loop <= count)
        {
            Console.Write("enter score " + loop + " : ");
            test = long.Parse(Console.ReadLine());
            sum = sum + test;
            loop = loop + 1;
        }
        avg = sum;
        avg = avg / count;
        Console.WriteLine("\naverage : " + avg);

        Console.WriteLine("\n\nenter a score of -100 to end\n");
        count = 1;
        sum = 0;
        Console.Write("enter score " + count + " : ");
        test = long.Parse(Console.ReadLine());
        sum = sum + test;
        while (test != -100)
        {
            count = count + 1;
            Console.Write("enter score " + count + " : ");
            test = long.Parse(Console.ReadLine());
            if (test != -100)
            {
                sum = sum + test;
            }
            else { }
        }
        count = count - 1;
        avg = sum;
        avg = avg / count;
        Console.WriteLine("\naverage : " + avg);
        Console.ReadKey();

标签: c#console

解决方案


class Program {
    static void Main(string[] args) {
        string input = "";
        List<List<int>> answers = new List<List<int>>();
        int questionsCount = ReadInt32("The number of questions: ");
        for (int i = 0; i < questionsCount; i++) {
            answers.Add(new List<int>());
        }
        while (input == "" || input == "y") {
            for (int i = 0; i < answers.Count; i++) {
                List<int> a = answers[i];
                a.Add(ReadInt32($"Question [{i}]: "));
            }
            input = Read("Continue (y/n)? ").ToLower();
        }
        WriteLine("End of input!");
        for (int i = 0; i < answers.Count; i++) {
            List<int> a = answers[i];
            Write($"Average for question[{i}]: {a.Average()}\n");
        }
        ReadKey();
    }

    static string Read (string a) {
        Write(a);
        return ReadLine();
    }

    static int ReadInt32 (string a = "") {
        Write(a);
        return ToInt32(ReadLine());
    }
}

试试这个。您可以自定义问题。请注意,要使用Write()and WriteLine(),您应该添加

using static System.Console;

在顶部,在项目的引用中。


推荐阅读