Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#和整数相加_C# - Fatal编程技术网

C#和整数相加

C#和整数相加,c#,C#,我很难理解C#与Python在语法和规则上的差异 因此,我必须创建一个程序,用户可以输入一只鸡一天下多少个蛋,计算总数,并给出几十个蛋 这是我现在的代码 int c1 = Convert.ToInt32("How many eggs did chicken 1 lay?"); System.Console.ReadLine(); int c2 = Convert.ToInt32("How many eggs did chicken 2 lay?"); System.Console.ReadLin

我很难理解C#与Python在语法和规则上的差异

因此,我必须创建一个程序,用户可以输入一只鸡一天下多少个蛋,计算总数,并给出几十个蛋

这是我现在的代码

int c1 = Convert.ToInt32("How many eggs did chicken 1 lay?");
System.Console.ReadLine();
int c2 = Convert.ToInt32("How many eggs did chicken 2 lay?");
System.Console.ReadLine();
int c3 = Convert.ToInt32("How many eggs did chicken 3 lay?");
System.Console.ReadLine();
int c4 = Convert.ToInt32("How many eggs did chicken 3 lay?");

int Sum = c1 + c2 + c3 + c4;
int Total = (Sum / 12);
System.Console.WriteLine(Sum);
System.Console.WriteLine(Total);
System.Console.ReadLine();

该消息仅向用户显示,以便用户知道需要输入什么。它不是你可以转换成整数的东西。所以你应该写下来:

Console.Write("How many eggs did chicken 1 lay?");
您必须将从控制台
ReadLine
获得的
字符串转换为
int

string sc1 = System.Console.ReadLine();
int c1 = int.Parse(sc1);

事实上,我能够完成整个项目

最终的代码如下所示

        Console.Write("How many eggs did chicken 1 lay?");
        string sc1 = System.Console.ReadLine();
        int c1 = int.Parse(sc1);
        Console.Write("How many eggs did chicken 2 lay?");
        string sc2 = System.Console.ReadLine();
        int c2 = int.Parse(sc2);
        Console.Write("How many eggs did chicken 3 lay?");
        string sc3 = System.Console.ReadLine();
        int c3 = int.Parse(sc3);
        Console.Write("How many eggs did chicken 3 lay?");
        string sc4 = System.Console.ReadLine();
        int c4 = int.Parse(sc4);


        int Sum = c1 + c2 + c3 + c4;
        int Total = (Sum / 12);

        System.Console.WriteLine("The sum of the eggs laid is  "  + Sum);

        System.Console.WriteLine("Dozen =  " +Total );

        System.Console.ReadLine();

欢迎来到SO。请看。特别是,提供示例输出和预期输出。
鸡1下了多少蛋?
不是数字!你不能直接转换。你的问题是什么?你有错误吗?如果是这样的话,请把它和你的问题包括在一起。注意
inttotal=(Sum/12)
容易出现整数除法截止问题。@TaW公平地说,我认为舍入错误是OPs最不担心的问题。如果没有对chicken 2、3和4的问题,该程序会让用户非常困惑。@AntonínLejsek为什么会这样?我想我已经为chicken 1做了充分的描述,他应该能够为其中的4个工作。这就是我没有使用循环的原因,因为我认为它可能会使答案混淆。我同意它可以填写,但在这种情况下,它并不像您所说的那样是完整的代码。@AntonínLejsek该问题清楚地表明它是用于学习目的的,我认为它足以专注于用户的问题。另一方面,答案也会被认为太过笼统,就像问题一样。@AntonínLejsek-我很乐意问鸡2、3和4,但我怀疑它们会有很多话要说。