C#控制台通过+/-符号调整问卷问题位置的实现求助
C#控制台程序实现问卷问题位置调整功能
问题说明
现有C#控制台代码可按questionid排序展示问卷问题列表,需求新增功能:通过控制台输入+(上移指定问题)或-(下移指定问题)调整问题展示位置,例如输入+ 2可将第2个问题移至第1位,输入- 5可将第5个问题移至第4位。此前尝试用switch case实现未成功,以下是可行解决方案。
完整实现代码
class Program { static void Main(string[] args) { List<Questionnaire> Qs = new List<Questionnaire>() { new Questionnaire() { questionid = 1, Topic = "Information Technology", Question = "1)what is the important aspects of IT?" }, new Questionnaire() { questionid = 2, Topic = "Security and compliance", Question = "2)What are security controls?" }, new Questionnaire() { questionid = 3, Topic = "Risk Assessments", Question = "3)What is Risk Assessment?", }, new Questionnaire() { questionid = 4, Topic = "Third Party Control", Question = "4)What are third party tools in software?", }, new Questionnaire() { questionid = 5, Topic = "cloud based solution", Question = "5)What are cloud based solutions?", } }; // 按questionid排序并转为可修改的List var orderedQuestions = Qs.OrderBy(q => q.questionid).ToList(); // 初始展示问题列表 DisplayQuestions(orderedQuestions); // 获取用户操作输入 Console.Write("请输入操作(格式:+ 序号 或 - 序号,例如+2表示第2个问题上移):"); string? input = Console.ReadLine(); if (string.IsNullOrWhiteSpace(input)) { Console.WriteLine("输入无效"); Console.ReadKey(); return; } // 解析输入 var parts = input.Trim().Split(' ', StringSplitOptions.RemoveEmptyEntries); if (parts.Length != 2 || !int.TryParse(parts[1], out int targetIndex)) { Console.WriteLine("输入格式错误,请按要求输入"); Console.ReadKey(); return; } // 转换为0-based索引 int actualIndex = targetIndex - 1; if (actualIndex < 0 || actualIndex >= orderedQuestions.Count) { Console.WriteLine("指定的问题序号不存在"); Console.ReadKey(); return; } // 执行移动操作 switch (parts[0]) { case "+": // 上移:非第一个元素时,与前一个交换位置 if (actualIndex > 0) { (orderedQuestions[actualIndex], orderedQuestions[actualIndex - 1]) = (orderedQuestions[actualIndex - 1], orderedQuestions[actualIndex]); } else { Console.WriteLine("第一个问题无法上移"); } break; case "-": // 下移:非最后一个元素时,与后一个交换位置 if (actualIndex < orderedQuestions.Count - 1) { (orderedQuestions[actualIndex], orderedQuestions[actualIndex + 1]) = (orderedQuestions[actualIndex + 1], orderedQuestions[actualIndex]); } else { Console.WriteLine("最后一个问题无法下移"); } break; default: Console.WriteLine("无效的操作符号,只能输入+或-"); break; } // 展示调整后的列表 Console.WriteLine("\n调整后的问题列表:"); DisplayQuestions(orderedQuestions); Console.ReadKey(); } // 提取展示逻辑为单独方法,避免重复代码 static void DisplayQuestions(List<Questionnaire> questions) { Console.WriteLine("\n当前问题列表:"); for (int i = 0; i < questions.Count; i++) { Console.WriteLine($"{i+1}. {questions[i].Question}"); } Console.WriteLine(); } public class Questionnaire { public int? questionid { get; set; } public string? Topic { get; set; } public string? Question { get; set; } } }
关键实现要点
- 转为可修改的List:原代码中
OrderBy返回的是IEnumerable<Questionnaire>,无法直接修改元素顺序,通过ToList()转为可操作的List。 - 输入验证与解析:要求用户按
+ 序号或- 序号格式输入,拆分输入内容后验证格式有效性,将用户习惯的1-based序号转换为代码使用的0-based索引,避免索引越界。 - 移动逻辑与边界处理:
- 上移:判断目标元素非第一个时,与前一个元素交换位置;
- 下移:判断目标元素非最后一个时,与后一个元素交换位置;
- 对边界情况给出明确提示,避免异常。
- Switch Case正确使用:通过解析后的操作符号匹配分支,执行对应移动逻辑,解决此前switch case失效问题。
内容的提问来源于stack exchange,提问作者ani H
相关产品推荐
相关产品推荐

