Java For循环!!!打印出值

时间:2016-10-28 14:51:52

标签: java for-loop

编写一个程序,在一个数组中生成20个随机骰子抛出的序列 打印模具值,仅标记最长的运行,如下所示: 1 2 5 5 3 1 2 4 3(2 2 2 2)3 6 5 5 6 3 1 如果有多个最大长度的运行,请标记第一个。

我正在研究这个问题,这段代码一直有效,直到算上maxCount。 但是,我坚持打印出最终结果,这意味着我正在处理最后一个for循环以打印出问题所需的内容。 但是,结果并不是我想得到的。我怎么能搞清楚?

import java.util.Random;

public class AA {

public static void main(String[] args) {
    int count = 1;
    int maxCount = 1;
    int runEndsAt = 0;

    // create array
    int[] num = new int[20];
    // create random object
    Random numbers = new Random();
    for (int i = 0; i < 20; i++) {
        num[i] = numbers.nextInt(6) + 1;
        // added 1 b/c it starts from 0
    }

    boolean inRun = false;
    for (int i = 0; i < num.length; i++) {
        if (inRun) {
            if (num[i] != num[i - 1]) {
                /*
                 * System.out.print("|" + count +"|");
                 * System.out.print(") ");
                 */ inRun = false;

            }
            if (inRun) {
                System.out.print("|" + count + "|");
                count++;
            }
        }
        if (!inRun) {
            if (count > maxCount) {
                maxCount = count;
                runEndsAt = i;
            }
            count = 1;
            if (i < 19)
                // comparing index from i to i+1
                if (num[i] == num[i + 1]) {
                System.out.print("( ");
                inRun = true;
                }
        }
    }
    if (inRun) {
        /*
         * System.out.print("|" + count +"|"); System.out.print(" )");
         */ }

    for (int i = 0; i < num.length; i++) {
        if (i == runEndsAt - maxCount) {

            System.out.println("(");

            if (i == runEndsAt) {
                System.out.println(")");

            }
        }
    }
 }
}

2 个答案:

答案 0 :(得分:0)

如果您使用的是Java,则可能需要使用StringBuilder。 解决方案:

import java.util.stream.IntStream;

public class LongestRun {
    public static void main(String[] args) {

        // We are told that die tosses is an Array... so we create one.
        int[] tosses = IntStream.range(0, 20)
                        .map(i -> { return (int) (Math.random() * 6 + 1); } )
                        .peek(System.out::print)
                        .toArray();

        // Counting and generation of output:
        int length = 0, longest = 0, startPos = 0;
        StringBuilder sb = new StringBuilder(""+tosses[0]);
        for (int i=1; i<tosses.length; i++) {
            if (tosses[i-1] == tosses[i]) {
                length++;
            } else {
                length=0;
            }

            if (longest < length) {
                longest = length;
                startPos = i-length;
            }
            sb.append("" + tosses[i]);
        }
        sb.insert(startPos+longest+1, ")");
        sb.insert(startPos, "(");

        // Final result:
        System.out.println("Result:");
        System.out.println(sb.toString());
    }
}

答案 1 :(得分:-1)

听起来很有趣。这是我的解决方案:

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SO_40307704 {
  private final static int NUM_ROLLS = 100;
  private final static Pattern PATTERN = Pattern.compile("(\\d)(\\1)+");

  public static void main(String... args) {
    final Random r = new Random();
    final String rolls = Stream.generate(() -> r.nextInt(6) + 1)
        .limit(NUM_ROLLS)
        .map(i -> Integer.toString(i))
        .collect(Collectors.joining());
    Matcher m = PATTERN.matcher(rolls);
    int start = 1;
    int end = 0;
    while (m.find()) {
      if (m.end() - m.start() > end - start) {
        end = m.end();
        start = m.start();
      }
    }
    System.out.println(String.format(
        "%s(%s)%s",
        rolls.substring(0, start),
        rolls.substring(start, end),
        rolls.substring(end)));
  }
}

希望有所帮助