2013-10-23 59 views
0

這是在java中完成的。如何使用不同陣列中隨機選擇的字符填充陣列

所以我試圖創建一些帶有字符的圖案生成器。我現在的代碼將創建一個數組,它將從輸入的第一個數字開始無限地打印,直到最後一個數字,從第一個數組中取出字符。錯誤的是,如果我從接近尾聲開始,並告訴它複製更多; (比如從12個字符開始,複製另外15個字符,但原始數組只有14個字符)。如何在原始數組結束後繼續填充數組,例如從頭開始並繼續通過數組,直到填滿。\

import java.util.*; 
public class patternGenerator { 
    private static Scanner input; 
    public static void main(String[] args) {  
    char[] anArray = {'!','@','#','$','%','^','&','*','+','=','~','<','>','?'}; 
    int symbolsPerLine = 0, printed, dontStop = 1, difSymbols; 
    input = new Scanner(System.in); 
    System.out.println("Enter the amount of symbols to use: "); 
    difSymbols = input.nextInt(); 
    while (difSymbols > anArray.length) { 
     difSymbols = (anArray.length-1); 
    } 
    System.out.println("Dif symbols are: "+ difSymbols); 
    System.out.println("Enter amount of symbols per row: "); 
    symbolsPerLine = input.nextInt(); 
    System.out.println("Amount of symbols per line are: "+ symbolsPerLine); 
    char [] patternArray = new char[symbolsPerLine]; 
    System.arraycopy(anArray, difSymbols, patternArray, 0, symbolsPerLine); 
    System.out.print(patternArray); 
    while(dontStop == 1) { 
      System.out.print("\n"); 
      printed = 0; 
      for (int a = 0; a< patternArray.length; a++) { 
        System.out.print(patternArray[a]); 
       } 
       printed++; 
     } 
    } 
} 
+1

提供您想要實現的示例。 – Prateek

+2

如何使用模數? –

回答

1

你可以嘗試添加一個索引在你的源陣列和循環它作爲您填寫的模式陣列:

import java.util.*; 
public class patternGenerator { 
    private static Scanner input; 
    public static void main(String[] args) {  
    char[] anArray = {'!','@','#','$','%','^','&','*','+','=','~','<','>','?'}; 
    int symbolsPerLine = 0, printed, dontStop = 1, difSymbols; 
    input = new Scanner(System.in); 
    System.out.println("Enter the amount of symbols to use: "); 
    difSymbols = input.nextInt(); 
    while (difSymbols > anArray.length) { 
     difSymbols = (anArray.length-1); 
    } 
    System.out.println("Dif symbols are: "+ difSymbols); 
    System.out.println("Enter amount of symbols per row: "); 


    symbolsPerLine = input.nextInt(); 

    System.out.println("Amount of symbols per line are: "+ symbolsPerLine); 
    char [] patternArray = new char[symbolsPerLine]; 

    int srcIndex =0; 
    for(j=0; j < symbolsPerLine; j++) { 
     patternArray[j] = anArray[srcIndex]; 
     srcIndex++; 
     if(srcIndex> (anArray.length-1)) srcIndex =0; 

    } 

    System.out.print(patternArray); 

}

1

所以我注意到的第一件事是你有一個無限循環。你離開dontStop allways == 1,所以你的代碼永遠不會退出while循環。第二個System.arraycopy(anArray,difSymbols,patternArray,0,symbolsPerLine);當您使用大於anArray.length的數字時, 會出現超出界限的錯誤,因此您需要刪除此代碼。這裏是一個建議的解決方案:

 char [] patternArray = new char[symbolsPerLine]; 

     int length = symbolsPerLine; 

     int spot = 0; 



     if(symbolsPerLine<anArray.length){ 
      symbolsPerLine = anArray.length; 
     } 


     while(dontStop==1){ 

      for(int i= (anArray.length - difSymbols);i<anArray.length-1;i++){ 

       if(length==0){ 
        dontStop =0; 
        break; 
       } 


       patternArray [spot]= anArray [i]; 
       spot++; 
       length--; 
      } 

     } 

     for(int j=0;j<patternArray.length;j++){ 
      System.out.print(patternArray[j]); 
     } 

     System.out.println("\npatternArray.length is: "+ patternArray.length); 

該解決方案的輸出是: 輸出RUN1: Dif的符號是:13 輸入每行符號的量:每行符號的數量是: 20 成品環路 @#$%^ & + =〜<> @#$%^ & + patternArray.length是:20

輸出RUN2: 輸入符號的量使用: Dif的符號是:5 輸入每行符號的量:每行符號的數量是:20 成品環路 =〜<> =〜 <> =〜<> =〜<> =〜<> patternArray.length是:20

這就是我假設你正在試圖完成的任務。

+0

所以實際上這基本上是我想要它做的,但我實際上想要一個無限循環。我希望它可以無限打印,如下所示: $$ ########################### $$ next line: $$ $ ######################### $$$ 而且, –

1

您可以使用以下。我重新考慮了一下你的代碼。

我使用CharBuffer,因爲它有一些不錯的期貨像Buffer.hasRemaining()(尚處於緩衝某些地方)和Buffer.put(...)(複製到緩衝區)面臨這樣一個問題的時候,可以來得心應手。

在您的問題的標題中,您想隨機選擇字符,但在您的解決方案中,您只需在其上添加數組。我已經使用SecureRandom.nextIn(...)添加了一些可能的解決方案。

import java.nio.CharBuffer; 
import java.security.SecureRandom; 
import java.util.Scanner; 

public class PatternGenerator { 


    public static void main(String[] args) { 
    char[] anArray = {'!', '@', '#', '$', '%', '^', '&', '*', '+', '=', '~', '<', '>', '?'}; 
    int symbolsPerLine = 0, difSymbols = 0; 

    Scanner input = new Scanner(System.in); 

    System.out.println("Enter the amount of symbols to use: "); 
    difSymbols = input.nextInt(); 
    difSymbols = difSymbols < anArray.length ? difSymbols : anArray.length - 1; 
    System.out.println("Dif symbols are: " + difSymbols); 

    System.out.println("Enter amount of symbols per row: "); 
    symbolsPerLine = input.nextInt(); 
    System.out.println("Amount of symbols per line are: " + symbolsPerLine); 
    CharBuffer cb = CharBuffer.allocate(symbolsPerLine); 

    /* 
    * just a copy of the original array appended. 
    */ 

    int offset = 0; 
    int length = 0; 
    while (cb.hasRemaining()) { 
     length = (cb.remaining() > difSymbols ? difSymbols : cb.remaining()) - offset; 
     cb.put(anArray, offset, length); 
     offset = (offset + length) % difSymbols; 
    } 
    // prepare the buffer to be read 
    cb.flip(); 
    System.out.println("appended: " + cb); 

    /* 
    * use random characters now 
    */ 
    cb.clear(); 
    SecureRandom secureRandom = new SecureRandom(); 
    while (cb.hasRemaining()) 
     cb.put(anArray[secureRandom.nextInt(anArray.length)]); 
    cb.flip(); 

    System.out.println("random: " + cb); 


    input.close(); 
    } 
}