c# - 需要帮助创建控制台应用程序,该应用程序输出调查 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();
解决方案
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;
在顶部,在项目的引用中。
推荐阅读
- android - 无法从 RxJava2 准确迁移到 RxJava3
- android - Android:(Camera2)相机从活动返回后不再存在异常
- python - 如何将行数设置在 QTableView 的右侧?
- terminal - 现在强制 ncurses 更改光标颜色
- javascript - 文本文件到arraylist javascript
- javascript - Asp.Net core MVC Javascript Ajax 参数 null HttpPost
- sql - 在 SQL Server 中将日期时间格式从 YYY-MM-DD HH:MM:SS 更改为 YYYY.MM.DD HH:MM:SS
- javascript - 如何在 Angular 8 中对字符进行切片并在 HTML 文件中显示
- flutter - 如何减少 raisebutton.icon 小部件中图标和文本之间的空间?
- azure - 如何让 Azure Devops 识别订阅连接具有新的访问令牌?