Java 在数组中查找连续的重复整数

Java 在数组中查找连续的重复整数,java,arrays,Java,Arrays,我有一个问题,我需要请求用户输入他们希望滚动模具的次数,并创建和打印具有所请求滚动的阵列。到目前为止,我可以创建数组,但是问题的另一部分是,每当有连续的重复滚动时,我必须在它们周围加上括号。例如,输入11,创建数组 {1,2,1,4,4,6,2,3,5,5}将打印12 1(4)6 2 3(5 5) 到目前为止我已经写过了 import java.util.Scanner; import java.util.Random; public class HW0603 { public st

我有一个问题,我需要请求用户输入他们希望滚动模具的次数,并创建和打印具有所请求滚动的阵列。到目前为止,我可以创建数组,但是问题的另一部分是,每当有连续的重复滚动时,我必须在它们周围加上括号。例如,输入11,创建数组

{1,2,1,4,4,6,2,3,5,5}将打印12 1(4)6 2 3(5 5)

到目前为止我已经写过了

import java.util.Scanner;
import java.util.Random;

public class HW0603 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("How many times would you like to roll: ");
        System.out.println();
        int x = input.nextInt();
        run(rolls(x), x);
    }

    public static int[] rolls(int x) {
        Random random = new Random();
        int y[] = new int[x];
        for (int i = 0; i < x; i++) {
            int z = random.nextInt(6) + 1;
            y[i] = z;
        }
        return y;
    }

    public static void run(int a[], int b) {
        for (int i = 1; i < b; i++) {
            System.out.print(a[i] + " ");
        }
    }
}
import java.util.Scanner;
导入java.util.Random;
公共类HW0603{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.print(“您希望滚动多少次:”);
System.out.println();
int x=input.nextInt();
运行(转鼓(x),x);
}
公共静态int[]卷(int x){
随机=新随机();
int y[]=新的int[x];
对于(int i=0;i

至于括号,我真的不知道如何开始。使用if语句对我不起作用,我的if语句变体似乎给了我越界的错误,因为我将
a[I]
a[I+1]
a[I-1]
进行比较。有谁能给我一个开始的地方或一些提取连续重复项的提示吗?

您需要将当前项与下一项进行比较 如果相等,则打印“(然后打印项目 使您已打开的标志
处于打开状态(,这样您就不会重新打开(再次,为了避免这种情况:
1(2(2…
,然后当curr!=next时,根据该标志打印项目或打印项目,然后关闭”)”

循环结束时

打印从循环
;i
,并检查是否已打开“(”

您的
run()
方法如下

static boolean paranOpened = false;
public static void run(int a[], int b) {
    for (int i = 0; i < b - 1; i++) {
        if (a[i] == a[i + 1]) {
            if (!paranOpened) {
                paranOpened = true;
                System.out.print(" (");
            }
            System.out.print(a[i] + " ");
        } else {
            System.out.print(a[i] + " ");
            if (paranOpened) {
                System.out.print(") ");
                paranOpened = false;
            }
        }
    }// for loop

    // print last item in array @(b-1)
    System.out.print(a[b - 1] + " ");

    // check if opened ( , then close it
    if (paranOpened) {
        System.out.print(") ");
    }
}// run()
static boolean=false;
公共静态无效运行(int a[],int b){
对于(int i=0;i

这是一个快速的解决方案,可能会有更好的算法

在这里,我尝试创建一个清晰易读的示例:

示例代码:


程序的第一个问题是run方法中的计数器启动 从1开始,该值应为零。当前程序不打印数组的第一个元素

然后,您需要用下一个元素检查每个元素,看看它们是否重复,是否在括号中打开,反之亦然

最后一个元素不需要检查,所以在循环外打印它,如果需要,关闭括号

顺便说一下,您不需要传递数组大小元素,只需使用array.length方法即可

public static void run(int a[], int b) 
    {
        boolean pOpen = false;//keep track if parenthesis is open
        for (int i = 0; i<a.length; i++) 
        {
            if (i < a.length-1)//prevent out of bound exception
            {
                if (a[i] == a[i+1] && !pOpen )// check if it is needed to `open or close the parenthesis`
                {
                    System.out.print("(");
                    pOpen = true;
                }
                System.out.print(a[i] + " ");
                if (a[i] != a[i+1] && pOpen)
                {
                    System.out.print(")");
                    pOpen = false;
                }

            }

        }
        System.out.print(a[a.length-1]);//print the last element
        if (pOpen)//close the parenthesis if open
        {
            System.out.print(")");
        }
    }   
公共静态无效运行(int a[],int b)
{
boolean pOpen=false;//如果括号打开,请跟踪

for(int i=0;i遍历数组并保留一个布尔值,用于标记括号是否已打开

import java.util.*;

class Ideone
{

    public static int[] rolls(int x) {
        Random random = new Random();
        int y[] = new int[x];
        for (int i = 0; i < x; i++) {
            int z = random.nextInt(6) + 1;
            y[i] = z;
        }
        return y;
    }

    public static void run(int a[], int b) {
        StringBuilder sb     = new StringBuilder();
        String        out    = "";
        boolean       parens = false;
        for (int j = 0; j < a.length; j++)
        {
            out = "" + a[j]; //by default just add an element

            //check for duplicate and build parenthesis
            if (j + 1 < a.length && a[j] == a[j+1]) //duplicate found
            {
                if (!parens) // if no parenthesis
                {
                    parens = true; //start parenthesis
                    out = "( " + a[j];
                }
            }
            else
            {
                if (parens) //if parenthesis already started
                {
                    out    = a[j] + " )";
                    parens = false; //stop parenthesis
                }
            }
            sb.append(" " + out);
        }

        // if the last element occured multiple times
        if (parens) //should end parens
        {
            sb.append(a[a.length-1] + " )");
        }

        //print out the result
        System.out.println(sb.toString());
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner input = new Scanner(System.in);
        System.out.print("How many times would you like to roll: ");
        System.out.println();
        int x = input.nextInt();
        run(rolls(x), x);
    }
}
import java.util.*;
表意文字
{
公共静态int[]卷(int x){
随机=新随机();
int y[]=新的int[x];
对于(int i=0;i
您需要使用布尔值来检查括号是开的还是否。

如果您想查看
a[I+1],我会将
b
替换为
a.length
您只能循环到
a.length-1
非常感谢您,我尝试使用值进行此操作,但经常出现越界现象,布尔值的使用现在对我来说真的很有意义。我应该制作一个[I]==a[I+1]来检查连续的重复项和bo
public static void run(int a[], int b) 
    {
        boolean pOpen = false;//keep track if parenthesis is open
        for (int i = 0; i<a.length; i++) 
        {
            if (i < a.length-1)//prevent out of bound exception
            {
                if (a[i] == a[i+1] && !pOpen )// check if it is needed to `open or close the parenthesis`
                {
                    System.out.print("(");
                    pOpen = true;
                }
                System.out.print(a[i] + " ");
                if (a[i] != a[i+1] && pOpen)
                {
                    System.out.print(")");
                    pOpen = false;
                }

            }

        }
        System.out.print(a[a.length-1]);//print the last element
        if (pOpen)//close the parenthesis if open
        {
            System.out.print(")");
        }
    }   
import java.util.*;

class Ideone
{

    public static int[] rolls(int x) {
        Random random = new Random();
        int y[] = new int[x];
        for (int i = 0; i < x; i++) {
            int z = random.nextInt(6) + 1;
            y[i] = z;
        }
        return y;
    }

    public static void run(int a[], int b) {
        StringBuilder sb     = new StringBuilder();
        String        out    = "";
        boolean       parens = false;
        for (int j = 0; j < a.length; j++)
        {
            out = "" + a[j]; //by default just add an element

            //check for duplicate and build parenthesis
            if (j + 1 < a.length && a[j] == a[j+1]) //duplicate found
            {
                if (!parens) // if no parenthesis
                {
                    parens = true; //start parenthesis
                    out = "( " + a[j];
                }
            }
            else
            {
                if (parens) //if parenthesis already started
                {
                    out    = a[j] + " )";
                    parens = false; //stop parenthesis
                }
            }
            sb.append(" " + out);
        }

        // if the last element occured multiple times
        if (parens) //should end parens
        {
            sb.append(a[a.length-1] + " )");
        }

        //print out the result
        System.out.println(sb.toString());
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner input = new Scanner(System.in);
        System.out.print("How many times would you like to roll: ");
        System.out.println();
        int x = input.nextInt();
        run(rolls(x), x);
    }
}