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++;
        }
    }
}
4

4 回答 4

1

可以使用Apache 的commons-lang中的RandomStringUtils.html#random(int, char...)来做到这一点。我不知道 JDK 中的单一方法解决方案。

于 2013-10-23T18:54:22.637 回答
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:13 Dif 符号为:13 每行输入符号数量:20 每行符号数量为:20 Finished Loop @#$%^& +=~<>@#$%^ & + patternArray.length 为:20

输出 Run2:输入要使用的符号数量:5 Dif 符号为:5 输入每行的符号数量:20 每行的符号数量为:20 完成循环 =~<>=~<>=~<>=~< >=~<> patternArray.length 为:20

这就是我假设你试图完成的事情。

于 2013-10-23T19:36:33.153 回答
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);

}

于 2013-10-23T19:38:58.613 回答
1

您可以使用以下内容。我对您的代码进行了一些重构。

我使用 a 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();
  }
}
于 2013-10-23T20:39:26.730 回答