简体   繁体   English

无法打印阵列中的图案

[英]Trouble printing out pattern in array

I am trying to get the number of pattern to printout from the array but under my number of pattern no pairs were printed out this is an example of what i am trying to get 我正在尝试从阵列中获取要打印的图案数量,但是在我的图案数量下没有打印出对,这是我要获取的图案的一个示例

(Array: 2 7 2 3 1 5 7 4 3 6 
Number of patterns: 3)  

but I do not know what to write from beyond number of patterns 但是我不知道该写些什么来写

The code: 编码:

public class FindIt {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int Sum = 0;
        int[] InsertNumbers = new int[10];
        System.out.println("Sample output #1:");
        System.out.print("Array: ");
        for(int i = 0; i < 10; i++)
        {
            InsertNumbers[i]=(int)(Math.random()*10)+1;
            System.out.print(InsertNumbers[i] + " ");
        }
        System.out.println("");
        System.out.print("Array: ");
        for(int i = 0; i < 5; i++)
        {
            ComputePattern(InsertNumbers, Sum);
            System.out.print(InsertNumbers[i] + " ");
        }
        System.out.println("");
        System.out.print("Number of patterns: ");

    }
    public static void ComputePattern(int[] InsertNumbers, int Sum)
    {
        for(int i = 0; i < 2; i++)
        {
            InsertNumbers[i] = Sum;
            Sum = Sum + Sum;
        }
    }
}

It is quite hard to understand your code but here is what I can tell you. 很难理解您的代码,但是我可以告诉您这些。 You have managed to get to ask the user input but I feel that the following would be better. 您已经设法询问用户输入,但是我认为以下内容会更好。

Instead, try having two arrays, one which the user can input 10 integers, and the other array with the sum of the pairs, hence an array containing 5 integers. 相反,请尝试使用两个数组,其中一个用户可以输入10个整数,另一个数组包含对的总和,因此,一个数组包含5个整数。

With the help of a For Loop and a formula, you can use it to get the 2 consecutive values. 借助For循环和公式,您可以使用它来获取2个连续的值。 The first formula being x*2, the second being (x*2)+1. 第一个公式是x * 2,第二个公式是(x * 2)+1。 With x being 0 in the for loop, and loop it for 5 times. 在for循环中x为0,并将其循环5次。

Afterwards, you get the values of the x*2 and the (x*2)+1 in the array, and sum them together. 之后,您将获得数组中x * 2和(x * 2)+1的值,并将它们求和。

Then with the sum, you can then use it to calculate the count of patterns. 然后使用总和,然后可以使用它来计算模式计数。

Suggestion : Try to be consistent with your println and print. 建议:尝试与您的println和print保持一致。 It is quite confusing and I am not quite sure as to why you have set println for certain text and print for the rest. 这很令人困惑,对于您为什么要为某些文本设置println并为其余文本设置print,我也不太确定。

没有打印图案,因为在打印Number of patterns后没有打印语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM