修复C#错误CS9010:主构造函数体不允许及分数求和代码修正
解决C#中error CS9010: Primary constructor body is not allowed错误
问题描述
编写了一段计算15个给定分数之和的C#代码,运行时抛出错误:error CS9010: Primary constructor body is not allowed,错误出现在分数求和方法中。
原错误代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class Program { public static void Main(string[] args) { MyStruct Q = new MyStruct(0, 0); Q.c = int.Parse(Console.ReadLine()); Console.WriteLine("Числитель дроби = " + Q.c); Q.z = int.Parse(Console.ReadLine()); Console.WriteLine("Знаменатель дроби = " + Q.z); MyStruct W = new MyStruct(0, 0); W.c = int.Parse(Console.ReadLine()); Console.WriteLine("Числитель дроби = " + W.c); W.z = int.Parse(Console.ReadLine()); Console.WriteLine("Знаменатель дроби = " + W.z); MyStruct E = new MyStruct(0, 0); E.c = int.Parse(Console.ReadLine()); Console.WriteLine("Числитель дроби = " + E.c); E.z = int.Parse(Console.ReadLine()); Console.WriteLine("Знаменатель дроби = " + E.z); MyStruct R = new MyStruct(0, 0); R.c = int.Parse(Console.ReadLine()); Console.WriteLine("Числитель дроби = " + R.c); R.z = int.Parse(Console.ReadLine()); Console.WriteLine("Знаменатель дроби = " + R.z); MyStruct T = new MyStruct(0, 0); T.c = int.Parse(Console.ReadLine()); Console.WriteLine("Числитель дроби = " + T.c); T.z = int.Parse(Console.ReadLine()); Console.WriteLine("Знаменатель дроби = " + T.z); MyStruct Y = new MyStruct(0, 0); Y.c = int.Parse(Console.ReadLine()); Console.WriteLine("Числитель дроби = " + Y.c); Y.z = int.Parse(Console.ReadLine()); Console.WriteLine("Знаменатель дроби = " + Y.z); MyStruct U = new MyStruct(0, 0); U.c = int.Parse(Console.ReadLine()); Console.WriteLine("Числитель дроби = " + U.c); U.z = int.Parse(Console.ReadLine()); Console.WriteLine("Знаменатель дроби = " + U.z); MyStruct I = new MyStruct(0, 0); I.c = int.Parse(Console.ReadLine()); Console.WriteLine("Числитель дроби = " + I.c); I.z = int.Parse(Console.ReadLine()); Console.WriteLine("Знаменатель дроби = " + I.z); MyStruct O = new MyStruct(0, 0); O.c = int.Parse(Console.ReadLine()); Console.WriteLine("Числитель дроби = " + O.c); O.z = int.Parse(Console.ReadLine()); Console.WriteLine("Знаменатель дроби = " + O.z); MyStruct P = new MyStruct(0, 0); P.c = int.Parse(Console.ReadLine()); Console.WriteLine("Числитель дроби = " + P.c); P.z = int.Parse(Console.ReadLine()); Console.WriteLine("Знаменатель дроби = " + P.z); MyStruct A = new MyStruct(0, 0); A.c = int.Parse(Console.ReadLine()); Console.WriteLine("Числитель дроби = " + A.c); A.z = int.Parse(Console.ReadLine()); Console.WriteLine("Знаменатель дроби = " + A.z); MyStruct S = new MyStruct(0, 0); S.c = int.Parse(Console.ReadLine()); Console.WriteLine("Числитель дроби = " + S.c); S.z = int.Parse(Console.ReadLine()); Console.WriteLine("Знаменатель дроби = " + S.z); MyStruct D = new MyStruct(0, 0); D.c = int.Parse(Console.ReadLine()); Console.WriteLine("Числитель дроби = " + D.c); D.z = int.Parse(Console.ReadLine()); Console.WriteLine("Знаменатель дроби = " + D.z); MyStruct F = new MyStruct(0, 0); F.c = int.Parse(Console.ReadLine()); Console.WriteLine("Числитель дроби = " + F.c); F.z = int.Parse(Console.ReadLine()); Console.WriteLine("Знаменатель дроби = " + F.z); MyStruct G = new MyStruct(0, 0); G.c = int.Parse(Console.ReadLine()); Console.WriteLine("Числитель дроби = " + G.c); G.z = int.Parse(Console.ReadLine()); Console.WriteLine("Знаменатель дроби = " + G.z); MyStruct c; c = Program.SummStruct(Q,W,E,R,T,Y,U,I,O,P,A,S,D,F,G); Console.WriteLine("Cложение: " + Q.ToString() + "+" + W.ToString() + "+" + E.ToString() + "+" + R.ToString() + "+" + T.ToString() + "+" + Y.ToString() + "+" + U.ToString() + "+" + I.ToString() + "+" + O.ToString() + "+" + P.ToString() + "+" + A.ToString() + "+" + S.ToString() + "+" + D.ToString() + "+" + F.ToString() + "+" + G.ToString() + "=" + c.ToString()); Console.ReadKey(); } public static MyStruct SummStruct(params MyStruct[] p) => p.Aggregate((f,s) => SummStruct(f,s)); //error here { MyStruct t = new MyStruct(0, 0); t.c = (a.c * b.z + a.z * b.c); t.z = a.z * b.z; return t; } public struct MyStruct { public double c; public double z; public MyStruct(int c, int z) { this.c = c; this.z = z; } public override string ToString() { return "(" + c.ToString() + "/" + z.ToString() + ")"; } } }
错误原因分析
- 同时使用表达式体语法(
=>)和常规大括号方法体,这违反C#语法规则,二者只能选其一。 - 下方的方法体未定义参数
a和b,编译器无法识别这两个变量。 - 本质需要两个重载的
SummStruct方法:一个处理多个MyStruct的可变参数,另一个处理两个MyStruct的相加逻辑。
修复后的代码
using System; using System.Linq; class Program { public static void Main(string[] args) { // 用循环简化15次重复输入逻辑 var fractions = new MyStruct[15]; for (int i = 0; i < 15; i++) { fractions[i] = new MyStruct(0, 0); Console.WriteLine($"Введите числитель дроби {i+1}:"); fractions[i].c = int.Parse(Console.ReadLine()); Console.WriteLine($"Введите знаменатель дроби {i+1}:"); fractions[i].z = int.Parse(Console.ReadLine()); } var result = SummStruct(fractions); // 拼接输出字符串,提升可读性 var sumStr = string.Join("+", fractions.Select(f => f.ToString())); Console.WriteLine($"Сложение: {sumStr} = {result.ToString()}"); Console.ReadKey(); } // 处理多个分数求和的方法 public static MyStruct SummStruct(params MyStruct[] p) { return p.Aggregate((f, s) => SummStruct(f, s)); } // 处理两个分数相加的方法 public static MyStruct SummStruct(MyStruct a, MyStruct b) { MyStruct t = new MyStruct(0, 0); t.c = (a.c * b.z + a.z * b.c); t.z = a.z * b.z; return t; } public struct MyStruct { public double c; public double z; public MyStruct(int c, int z) { this.c = c; this.z = z; } public override string ToString() { return $"({c}/{z})"; } } }
额外优化点
- 用循环替代重复15次的输入代码,减少冗余。
- 使用
string.Join简化求和表达式的拼接,提升代码可读性。 - 格式化输出提示文本,交互更直观。
内容的提问来源于stack exchange,提问作者Naydar
相关产品推荐
相关产品推荐

