這是在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++;
}
}
}
提供您想要實現的示例。 – Prateek
如何使用模數? –