如何在数组中找到最长的运行时间,并将括号围绕它和所有其他在数组中运行?APCSA



任务是:编写一个程序,生成20次随机掷骰子的序列(值1 - 6)。用户将被问到是想看到所有的掷骰子还是最大的掷骰子。然后,通过将它们包含在括号中来打印指定运行的数组。

我已经尽了最大的努力来完成这个任务,但是我对如何在找到运行和找到大于2的运行长度后添加括号感到困惑。我也试着在网上找到其他程序,比如我的程序,但我无法在我的问题上找到帮助,无法在运行周围加上括号并找到所有运行。

查找所有运行的示例:1 2 (5 5)3 1 2 4 3 (2 2 2 2)3 6 (5 5 5 5)6 31 1查找最长运行时间的示例:1 2 5 5 3 1 2 4 3 (2 2 2 2 2)3 6 5 5 5 5 5 6 3 1

下面是我的代码:

进口java.util。*;公共类RunGenerator {

Scanner s = new Scanner(System.in);
static int max = 6;
static int min = 1;

public static void randomNumbers(int[]array)// generates the 20 random numbers from the die tosses using Math.random
{
for(int i = 0; i <= 20; i++)
{
array[i] = (int)Math.random()* (max-min +1) + min;
}
}
public static int findRun(int[]array)// finds runs among the array and returns the run surrounded by parentheses
{
for(int i = 0; i <= array.length; i++)
{
int count = 0;
int currentValue = array[i++];
int lastValue = array[i];
if(array[i] == array[i+1]) //checks to see if the element is equal to _____
{
System.out.println("(");

while(lastValue == currentValue) //while the two elements are equal, we do not close the parentheses
{
count++; //what do I do in here to check for further length??
}

System.out.println(")");
}
else
{
System.out.println(i + " " + (i+1) );
}

}


}
public static int findLargestRun(int[]array)
{
for(int i = 0; i <= array.length; i++)
{
if(array[i] == array[i++]) //checks to see if the element is equal to the adjacent element
{

while(array[i] == array[i++]) //while the two elements are equal, we do not close the parentheses
{

}
System.out.println("(" + i);
}
else
{
System.out.println(i + " " + (i+1) );
}
}

}

可能值得提供不同的方法来查找运行,打印()之间的运行,以及使用辅助类Run来打印最长运行,以存储运行数据:start,length,value:

static class Run {
int start;
int length = 1;
int value;
}
static List<Run> findRuns(int ... arr) {
List<Run> result = new ArrayList<>();

Run run = new Run();
run.start = 0;
run.value = arr[0];

int max = 1;

for (int i = 1; i < arr.length; i++) {
if (arr[i] != run.value) {
result.add(run);

run = new Run();
run.start = i;
run.value = arr[i];

} else {
run.length++;
if (max < run.length) {
max = run.length;
}
}
}
result.add(run);
System.out.println("Longest run: " + max);

return result;
}

打印方法使用流API +Collections.nCopies构建运行

static String printRuns(List<Run> runs) {
return runs.stream()
.map(r -> r.length > 1 
? "(" + String.join(" ", Collections.nCopies(r.length, Integer.toString(r.value))) + ")"
: Integer.toString(r.value)
)
.collect(Collectors.joining(" "));
}

最长运行的类似打印(可以是多个)

static String printLongestRuns(List<Run> runs) {
int max = runs.stream().mapToInt(r -> r.length).max().orElse(-1);

return runs.stream()
.map(r -> r.length > 1
? (r.length == max ? "(" : "") 
+ String.join(" ", Collections.nCopies(r.length, Integer.toString(r.value))) 
+ (r.length == max ? ")" : "")
: Integer.toString(r.value)
)
.collect(Collectors.joining(" "));
}

测试
System.out.println(printRuns(findRuns(1)));
System.out.println(printRuns(findRuns(2, 2, 5, 5, 3, 1, 2, 4, 3, 2, 2, 2, 2, 3, 6, 5, 5, 5, 5, 6, 3, 1)));
System.out.println(printLongestRuns(findRuns(2, 2, 5, 5, 3, 1, 2, 4, 3, 2, 2, 2, 2, 3, 6, 5, 5, 5, 5, 6, 3, 1)));

输出
Longest run: 1
1
Longest run: 4
(2 2) (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5 5 5) 6 3 1
Longest run: 4
2 2 5 5 3 1 2 4 3 (2 2 2 2) 3 6 (5 5 5 5) 6 3 1

一些需要修复的错误:

  • 你的代码需要一个程序开始的入口点。Java不是从程序的顶端开始的。java实例化所有自由*静态变量(在类中的函数之外),并在public static void main(String[] args) { /*here*/ }函数内的第一行开始执行。如果您在类的实例中工作(您不在这里),则非静态自由*变量将在该实例中实例化。TLDR:使扫描器static以及static void main(String[] args)功能。
  • 在for循环for(int i = 0; i <= array.length; i++)中,你尝试比较if(array[i] == array[i++])的值,这将在最后一个循环中导致if(array[array.length] == array[array.length+1])用arrayoutofbounds-exception使应用程序崩溃
  • 如果你创建一个函数与返回类型以外的void像你做了public static **int** findRun(int[]array)你不能编译没有错误,如果你不提供返回变量(return myint; // at the end of the function)
  • 如果你将基本变量传递给函数,它们在java中被复制,只有来自复杂类的对象被视为指针。→将function randomNumbers(int[]array)更改为function randomNumbers(),并使用静态全局数组代替,例如

也是一个警告:

  • Math.random()是一个伪随机-数字生成器在0.000之间…和0.999……为安全和科学功能选择其他真实/安全的随机生成器。

我希望这能帮助你开始。在这一点上,我不认为一个有效的代码能帮到你那么多。一旦你掌握了java的概念,你绝对可以做到。

最新更新