麻烦总计

时间:2017-04-24 00:31:59

标签: c# c#-4.0

无法为总数添加列号,方块和多维数据集的摘要。我应该看的任何指针?下面是我的代码的副本。

 static void Main(string[] args)
        {
            int number;
            int total = 0;
            Console.WriteLine("number\t" + "square\t" + "cube");
            Console.WriteLine("-----------------------------");

            for (int i = 0; i <= 20; i += 2)
            {
                number = i;
                int k = 0;
                do
                {
                    Console.Write(number + "\t");
                    number *= i;
                    total += number;
                    k++;

                } while (k < 3);

                Console.WriteLine("Total is",total);
                Console.WriteLine();
            }

            Console.WriteLine("---------------------------------------");

2 个答案:

答案 0 :(得分:1)

如果我理解你想要什么,一种方法是跟踪数组中每个电源(1,2和3)的运行总计,然后在结尾显示这些值。

阵列将有3个索引,每次我们增加“功率”。我们将数字提高到,我们将该值添加到数组中的相应索引。

例如:

static void Main(string[] args)
{
    // This array will hold three items:
    //  - totals[0] = numberTotal
    //  - totals[1] = squareTotal
    //  - totals[2] = cubeTotal
    var totals = new int[3];

    Console.WriteLine("number\t" + "square\t" + "cube");
    Console.WriteLine("-----------------------------");

    for (int number = 0; number <= 20; number += 2)
    {
        // Grab a copy of 'number' so we don't modify the loop variable
        var thisNumber = number;

        for(int powerIndex = 0; powerIndex < 3; powerIndex++)
        {
            // Write this number to screen
            Console.Write($"{0:n0}\t", thisNumber);

            // Add this number to the current number in 'power' index
            totals[powerIndex] += thisNumber;

            // Power up
            thisNumber *= number;
        }

        Console.WriteLine();
    }

    Console.WriteLine("-----------------------------");
    Console.WriteLine("{0:n0}\t{1:n0}\t{2:n0}\t", totals[0], totals[1], totals[2]);

    // Alternatively, if you're using C#6.0, you could write:
    Console.WriteLine($"{totals[0]:n0}\t{totals[1]:n0}\t{totals[2]:n0}\t");

    Console.Write("\nDone!\nPress any key to exit...");
    Console.ReadKey();
}

输出:

enter image description here

答案 1 :(得分:0)

您的代码中有两个错误:

首先是关于Console.WriteLine。为了使它工作,你应该传递参数,例如这里显示的(它不是唯一的方法,但它是最简单的)

function operateClaw(func){
    var readCount = 0;
    for(var isRead in qdata){
        readCount++;
        if(qdata[isRead]['reading'] == true){
            return;
        }else if(readCount == 5){
            func();
        }
    }
}

function drop () {
    if(isDropping){ return; } //prevent multiple clicks
    stopSwingClaw();
    isDropping = true;  
    MCI = setInterval(moveDown,20); //start heartbeat
}

function dropClaw () {
    operateClaw(drop);
}

Secon one更多关于算法。当您将总数添加到数字时,请检查。如果你仔细观察,你会发现你没有添加你显示的相同号码,但是你的添加号码是*我!这是一个很大的错误,但要解决它只是交换那两行:

Console.WriteLine("Total is" + total);

我相信修复了每个问题,希望它有所帮助: - )

完整代码:

Console.Write(number + "\t");
total += number;
number *= i;
k++;