将 Scratch 转化为算法

Converting Scratch to Algorithm

我第一次学习算法并尝试用 stratch 弄清楚。我正在关注 Stratch wiki 上的教程。我如何将其转换为算法?(带有流程图或正常步骤)。特别是循环。(我上传为图片)Please click here to see picture

我开始了:

第 1 步开始
Step2: İnt: delete all of numbers, iterator, amount,sum
第三步:你想要多少个号码?
Step4:initialize 总和=0,数量=0,迭代器=1
Step5: 输入元素值
第六步:通过在数组中使用循环找到总和并更新总和值,其中循环必须继续直到(元素数-1)次
Step7:avg=sum/no 个元素
第八步:打印平均值

我不认为这是真的。我的意思是我觉得有错误?谢谢你的时间。

从头开始

这是 Scratch 中变体 2 中的算法(参见下面的 Java 算法)。输出应该是相同的。

Java

这是 Java 中的算法,我在其中对步骤进行了评论,应该为您提供 step-by-step 如何在 Scratch 中执行此操作的指南。

我还实现了该算法的两个变体,以向您展示程序员在实现主要是时间(= 算法完成所需的时间)和 space(=您计算机上使用的内存)。

Please note: the following algorithms do not handle errors. E.g. if a user would enter a instead of a number the program would crash. It is easy to adjust the program to handle this but for simplicity I did not do that.

变体 1:将所有元素存储在数组中 numbers

此变体将所有数字存储在数组 numbers 中,并在末尾使用那些比变体 2 慢的数字计算 sum,因为算法遍历所有数字两次。好处是您将保留用户输入的所有数字,如果需要,您以后可以使用它,但您需要存储空间来存储这些值。

public static void yourAlgorithm() {
        // needed in Java to get input from user
        var sc = new Scanner(System.in);
        // print to screen (equivalent to "say"/ "ask")
        System.out.print("How many numbers do you want? ");
        // get amount of numbers as answer from user
        var amount = sc.nextInt();
        // create array to store all elements
        var numbers = new int[amount];
        // set iterator to 1
        int iterator = 1;
        // as long as the iterator is smaller or equal to the number of required numbers, keep asking for new numbers
        // equivalent to "repeat amount" except that retries are possible if no number was entered
        while (iterator <= amount) {
            // ask for a number
            System.out.printf("%d. number: ", iterator);
            // insert the number at position iterator - 1 in the array
            numbers[iterator - 1] = sc.nextInt();
            // increase iterator by one
            iterator++;
        }
        // calulate the sum after all the numbers have been entered by the user
        int sum = 0;
        // go over all numbers again! (this is why it is slower) and calculate the sum
        for (int i = 0; i < amount; i++) {
            sum += numbers[i];
        }
        // print average to screen
        System.out.printf("Average: %s / %s = %s", sum, amount, (double)sum / (double)amount);
    }

变体2:输入新数字时计算总和

该算法不存储用户输入的数字,而是立即使用输入来计算总和,因此速度更快,因为只需要一个循环,并且不需要存储数字,因此需要更少的内存。 如果您不需要用户稍后输入的所有数字,这将是最好的解决方案(最快,需要的内存最少 space/)。

// needed in Java to get input from user
        var sc = new Scanner(System.in);
        // print to screen (equivalent to "say"/ "ask")
        System.out.print("How many numbers do you want? ");
        // get amount of numbers as answer from user
        var amount = sc.nextInt();
        // set iterator to 1
        int iterator = 1;
        int sum = 0;
        // as long as the iterator is smaller or equal to the number of required numbers, keep asking for new numbers
        // equivalent to "repeat amount" except that retries are possible if no number was entered (e.g. character was entered instead)
        while (iterator <= amount) {
            // ask for a number
            System.out.printf("%d. number: ", iterator);
            // get number from user
            var newNumber = sc.nextInt();
            // add the new number to the sum
            sum += newNumber;
            // increase iterator by one
            iterator++;
        }
        // print average to screen
        System.out.printf("Average: %s / %s = %s", sum, amount, (double)sum / (double)amount);

变体 3:结合两种方法

您也可以结合这两种方法,即。 e.在第一个循环中计算总和,并将值另外存储在数字数组中,以便以后需要时可以使用它。

预期输出