2016-02-28 56 views
2

我有一个问题,在这个问题中,我需要询问用户输入多少次他们希望掷骰子并创建并打印具有请求卷的数组。到目前为止,我可以创建数组,但问题的另一部分是每当有连续的​​重复卷时,我必须在它们周围放置括号。例如输入11,创建阵列在数组中查找连续的重复整数

{1,2,1,4,6,2,3,5,5}将打印1 2 1(4 4)6 2 3(5 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] + " "); 
     } 
    } 
} 

至于我真的不知道如何开始的括号。使用if语句对我不起作用,因为我将a[i]a[i+1]a[i-1]进行了比较,所以我的if语句变体似乎给了我无限的错误。任何人都可以给我一个地方开始或一些提示,以提取连续重复?

+3

我会用'a.length'替换'b',如果你想看'a [i + 1]',你只能循环到'a.length - 1' –

回答

1

需要当前项目与下一个项目 比较,如果相同,打印“(”然后打印你已经打开的项目 化妆标志paranOpened(,这样你就不会重开(再次,要避免这种情况:1 (2(2(2...,然后当CURR!=下一个,根据该标志任一打印的项目或打印的项目然后关闭“)”

在环

打印LAT项(b-1)的端部的是被排除在循环..;i < b - 1;..,并检查您是否已打开“(”

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() 

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

1

在这里,我试图创造一个干净和可读性例如:

样品代码:

public class HelloWorld { 

    public static void main(String[] args) { 
     int arr[] = { 1, 2, 1, 4, 4, 6, 2, 3, 5, 5, 5 }; 
     printConsecutiveInBrace(arr); 
    } 

    public static void printConsecutiveInBrace(int arr[]) { 
     int printFrom = 0; 
     for (int i = 0; i < arr.length; i++) { 
      if (i == arr.length - 1 || arr[i] != arr[i + 1]) { 
       print(arr, printFrom, i); 
       printFrom = i + 1; 
      } 
     }  
    } 

    public static void print(int arr[], int printFrom, int printTo) { 
     if (printFrom < printTo) //Here check: Consecutive Duplicate 
      System.out.print("("); 
     for (int i = printFrom; i <= printTo; i++) 
      System.out.print(arr[i] + " "); 
     if (printFrom < printTo) 
      System.out.print(") "); 
    } 
} 

输出:

1 2 1 (4 4) 6 2 3 (5 5 5) 
1

与你程序中的第一个问题是,在你的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(")"); 
     } 
    } 
+0

非常感谢,我试过了试图用数值试图这样做,并经常出界,布尔值的使用现在对我来说确实有意义。我应该做一个[i] == a [i + 1]来检查连续的重复项,并且在之前我使用[i] == a [i + 1]替代了前面的括号。 – STRAN

1

迭代你的数组并保留一个布尔值,标识括号是否已经打开。

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); 
    } 
} 
1

您需要使用布尔检查您的括号是开还是没有。