C#代码问题:如何根据剩余鸡蛋数动态调整单复数表述
问题解决:C#鸡蛋数量输出的单复数处理
我是编程新手,现有一段C#代码大体可用,但需要实现:当剩余鸡蛋数量为1时,输出文本为“You have {0} eggs which equals {1} dozen and {2} egg left over.”;当剩余鸡蛋数量多于1时,输出文本中的“egg”变为“eggs”。目前代码始终显示“eggs”,不知道该如何修改。
原代码
using System; using System.Diagnostics.Eventing.Reader; namespace Q2 { public class Q2 { static void Main() { // Keep the following line intact Console.WriteLine("==========================="); // Insert your solution here. int totaleggs = 0; Console.Write("Enter the number of chickens: "); int Chickens = Convert.ToInt32(Console.ReadLine()); for (int i = 0; i < Chickens; i++) { Console.Write("Eggs: "); int egg = Convert.ToInt32(Console.ReadLine()); if (egg > 0) { totaleggs += egg; Console.WriteLine("You have {0} eggs which equals {1} dozen and {2} eggs left over.", totaleggs, (int)(totaleggs / 12), totaleggs % 12); Console.ReadLine(); } else if (totaleggs % 12 ==1) { Console.WriteLine("You have {0} eggs which equals {1} dozen and {2} egg left over.", totaleggs, (int)(totaleggs / 12), totaleggs % 12); Console.ReadLine(); } else { Console.WriteLine("You have {0} eggs which equals {1} dozen and {2} eggs left over.", totaleggs, (int)(totaleggs / 12), totaleggs % 12); Console.ReadLine(); } } // Keep the following lines intact Console.WriteLine("==========================="); } } }
问题分析
原代码的判断逻辑存在错误:
- 第一个
if (egg > 0)分支直接输出固定带eggs的文本,完全忽略剩余鸡蛋数量是否为1,这是导致始终显示eggs的核心原因。 else if的判断条件放在egg > 0之后,只有当输入的egg不大于0时才会触发,逻辑顺序完全颠倒。
修改方案
先计算剩余鸡蛋数量,再根据剩余数量决定输出的单复数形式,以下是两种简洁的实现方式:
方式一:分支判断输出
using System; using System.Diagnostics.Eventing.Reader; namespace Q2 { public class Q2 { static void Main() { // Keep the following line intact Console.WriteLine("==========================="); // Insert your solution here. int totaleggs = 0; Console.Write("Enter the number of chickens: "); int Chickens = Convert.ToInt32(Console.ReadLine()); for (int i = 0; i < Chickens; i++) { Console.Write("Eggs: "); int egg = Convert.ToInt32(Console.ReadLine()); // 先更新总鸡蛋数 if (egg > 0) { totaleggs += egg; } // 计算剩余鸡蛋数 int leftover = totaleggs % 12; // 根据剩余数量选择输出文本 if (leftover == 1) { Console.WriteLine("You have {0} eggs which equals {1} dozen and {2} egg left over.", totaleggs, (int)(totaleggs / 12), leftover); } else { Console.WriteLine("You have {0} eggs which equals {1} dozen and {2} eggs left over.", totaleggs, (int)(totaleggs / 12), leftover); } Console.ReadLine(); } // Keep the following lines intact Console.WriteLine("==========================="); } } }
方式二:三元运算符简化代码
直接在输出语句中用三元运算符动态选择单复数,让代码更精简:
using System; using System.Diagnostics.Eventing.Reader; namespace Q2 { public class Q2 { static void Main() { // Keep the following line intact Console.WriteLine("==========================="); // Insert your solution here. int totaleggs = 0; Console.Write("Enter the number of chickens: "); int Chickens = Convert.ToInt32(Console.ReadLine()); for (int i = 0; i < Chickens; i++) { Console.Write("Eggs: "); int egg = Convert.ToInt32(Console.ReadLine()); if (egg > 0) { totaleggs += egg; } int leftover = totaleggs % 12; // 三元运算符判断单复数 string eggWord = leftover == 1 ? "egg" : "eggs"; Console.WriteLine($"You have {totaleggs} eggs which equals {(int)(totaleggs / 12)} dozen and {leftover} {eggWord} left over."); Console.ReadLine(); } // Keep the following lines intact Console.WriteLine("==========================="); } } }
核心说明
- 先更新总鸡蛋数,再通过
totaleggs % 12计算剩余鸡蛋数量。 - 根据剩余数量是否等于1,动态选择输出
egg或eggs,确保逻辑正确。
内容的提问来源于stack exchange,提问作者josna jose
相关产品推荐
相关产品推荐

