gpt4 book ai didi

java - 将相同的数字分组到数组列表中(Big Java Ex 7.5)

转载 作者:行者123 更新时间:2023-12-01 21:23:21 25 4
gpt4 key购买 nike

即使使用本书给出的一些伪代码作为提示(本章是关于数组和数组列表),我在弄清楚应该如何解决这个问题时遇到了一些麻烦。顺便说一句,这不是作业,我正在自学。

问题

A run is a sequence of adjacent repeated values. Write a program thatgenerates a sequence of 20 random die tosses and that prints the dievalues, marking the runs by including them in parentheses, like this:1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 3 1

书中提示

Set a boolean variable inRun to false.

For each valid index i in the array list If inRun

If values[i] is different from the preceding value Print ) inRun = False Else

If values[i] is the same as the followingvalue Print ( inRun = True Print Values; [i]

我不明白的是这个提示到底有什么帮助。假设我们有多个数字,如 2 2 2 2 这不会只是做类似 (2 2) 2 2

的事情吗?

这是我到目前为止正在尝试的

import java.util.ArrayList;
import java.util.Random;
class seq
{
private ArrayList<Integer> nums;
private Random randNum;
private boolean inRun;
public seq()
{
nums = new ArrayList<Integer>();
randNum = new Random();
inRun = false;
}

public void addToArrList()
{
for(int i = 0; i < 20 ; i++)
{
int addThisNum = randNum.nextInt(20)+1;
nums.add(addThisNum);
}
}

public void checkSides()
{
int count = 0;
for(int i = 0 ; i < nums.size(); i++)
{
if(nums.get(i) != nums.get(i-1))
{
inRun = false;
}
if(nums.get(i) == nums.get(i+1))
{
inRun = true;
}
}
}

public String toString()
{
String output = "Array is:";
if (output.)
}
}

另一次尝试

import java.util.Random;
import java.util.ArrayList;
class Seq
{
private ArrayList<Integer> nums;
private Random randNum;
public Seq()
{
nums = new ArrayList<Integer>();
randNum = new Random();
}
public void fillArrList()
{
for (int i = 0 ; i < 20 ; i++)
{
int thisRandNum = randNum.nextInt(20)+1;
nums.add(thisRandNum);
}
}

public String toString() {
StringBuilder result = new StringBuilder();
boolean inRun = false;
for (int i = 0; i < nums.size(); i++) {
if (i < nums.size() - 1 && nums.get(i).equals(nums.get(i + 1))) {
if (!inRun) {
result.append("(");
}
result.append(nums.get(i));
inRun = true;

} else {
result.append(nums.get(i));
if (inRun) {
result.append(")");
}
inRun = false;

}
}
return result.toString();
}
}

public class Sequence{
public static void main(String [] args)
{
Seq seqObj = new Seq();
seqObj.fillArrList();
System.out.println(seqObj.toString());
}
}

输出

85641520612614320473181113612

201362181920141020(1919)514920162914

最佳答案

我认为他们的意思是这样的:

@Override
public String toString() {
StringBuilder result = new StringBuilder();
boolean inRun = false;
for (int i = 0; i < nums.size(); i++) {
if (i < nums.size() - 1 && nums.get(i).equals(nums.get(i + 1))) {
if (!inRun) {
result.append("(");
}
result.append(nums.get(i));
inRun = true;

} else {
result.append(nums.get(i));
if (inRun) {
result.append(")");
}
inRun = false;

}
}
return result.toString();
}

此外,您可能希望生成 1 到 6 之间的随机数,即

int addThisNum = randNum.nextInt(6) +1; 

关于java - 将相同的数字分组到数组列表中(Big Java Ex 7.5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38775585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com