C#并添加整数

时间:2017-09-09 19:40:00

标签: c#

与Python相比,我在使用C#的语法和规则方面存在差异。

所以我必须创建一个程序,在这个程序中,用户输入一天鸡的鸡蛋数量,总数,并给出几十个鸡蛋。

这是我现在的代码

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();

2 个答案:

答案 0 :(得分:1)

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

Console.Write("How many eggs did chicken 1 lay?");

您必须将string从控制台ReadLine投放到int

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

答案 1 :(得分:1)

实际上我能够一起完成已完成的项目

最终代码如下

        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();