如何让计数器以1而不是0显示?

时间:2013-11-18 06:41:56

标签: c#

向屏幕显示消息,输入火鸡编号为0的重量

我怎么能改变这一点,让它从输入1号火鸡的重量开始,因为如果我在开始时分配值为1的turkeycounter,而忽略了一只火鸡的重量。因此,如果用户想要输入5只火鸡的重量,他们将只能输入权重.4。你可以告诉我一个新的!

    static void Main(string[] args)
    {
        int TurkeyNumber = 0;
        double TurkeyWeight = 0.00;
        int TurkeyCounter = 0;
        int G1 = 0, G2 = 0, G3 = 0, G4 = 0, G5 = 0, G6 = 0; 



        Console.Write("How many turkeys are you weighing? ");
        TurkeyNumber = Convert.ToInt32(Console.ReadLine());


        while (TurkeyCounter < TurkeyNumber)
        {
            Console.WriteLine("\nEnter the weight of turkey number {0}:",TurkeyCounter);
            TurkeyWeight = Convert.ToInt32(Console.ReadLine());
            if (TurkeyWeight > 12)
            {
                G1 = G1 + 1;       
            }
            if (TurkeyWeight > 10)
            {
                G2 = G2 + 1;
            }
            if (TurkeyWeight > 8)
            {
                G3 = G3 + 1;
            }
            if (TurkeyWeight > 6)
            {
                G4 = G4+ 1;
            }
            if (TurkeyWeight > 4)
            {
                G5 = G5 + 1;
            }
            if (TurkeyWeight <= 12)
            {
                G6 = G6 + 1;
            }





            TurkeyCounter++;             

        }



        Console.ReadKey();

    }
}

}

2 个答案:

答案 0 :(得分:6)

Console.WriteLine("\nEnter the weight of turkey number {0}:",TurkeyCounter+1);

答案 1 :(得分:3)

只需在显示+1时添加TurkeyCounter即可。

另一点是,如果一个项目的块,请不要分开。根据你的逻辑,假设火鸡的体重是7。因此,它添加了+1 G3G4G6。因此产生的火鸡计数将高于用户输入的数量。

此外,我建议您使用数组而不是使用唯一变量。所以你可以轻松地操纵数组。

static void Main(string[] args)
{
    int TurkeyNumber = 0;
    double TurkeyWeight = 0.00;
    int TurkeyCounter = 0;

    int[] G= new int[6];

    Console.Write("How many turkeys are you weighing? ");
    TurkeyNumber = Convert.ToInt32(Console.ReadLine());

    while (TurkeyCounter < TurkeyNumber)
    {
        Console.WriteLine("\nEnter the weight of turkey number {0}:",TurkeyCounter+1);
        TurkeyWeight = Convert.ToInt32(Console.ReadLine());

        if (TurkeyWeight > 12)
           G[0]++;       
        else if (TurkeyWeight > 10)
           G[1]++;
        else if (TurkeyWeight > 8)
           G[2]++;
        else if (TurkeyWeight > 6)
           G[3]++;
        else if (TurkeyWeight > 4)
           G[4]++;
        else if (TurkeyWeight <= 12) 
           G[5]++;

        TurkeyCounter++;             
    }
    Console.ReadKey();
}