2016-02-28 57 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

您需要使用布爾檢查您的括號是開還是沒有。