作业大致意思为创一个包含至少8位数的数组,数字随意,
要求定义(好像是要让我定义的意思)add 和 last 两个函数。
add:与数组中加上一个数 但不return(反馈?)新加的数
last:将这个数组随机排列并打印
我有点懵,希望大佬们给点提示。
我用的是Java 14.0,Eclipse启动器
我现在不知道在写啥:
package Array1;
import java.util.Arrays;
class add{
//data
private String int[];
//这里然后怎么定义呀?
public class HelloWorld {
public static void main(String[] args) {
int[] a = new int[8];
Arrays.fill(a, 1);
//这个有8个1的数组创是创出来了,但然后咋用上面的函数呀?
}
}
import java.util.Arrays;
class Q1098522 {
static void printArray(int[] arr)
{
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + "\t");
System.out.println();
}
public static void main(String[] args) {
Last8 example = new Last8();
printArray(example.last());
for (int i = 1; i <= 20; i++)
{
example.add(i);
printArray(example.last());
}
}
}
class Last8 {
private int[] arr = new int[8];
private int count = 0;
public void add(int x)
{
arr[count++ % 8] = x;
}
public int[] last()
{
int[] a = new int[8];
for (int i = 0; i < 8; i++)
{
a[i] = arr[(count + i + 8) % 8];
}
return a;
}
}
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 1 2
0 0 0 0 0 1 2 3
0 0 0 0 1 2 3 4
0 0 0 1 2 3 4 5
0 0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
2 3 4 5 6 7 8 9
3 4 5 6 7 8 9 10
4 5 6 7 8 9 10 11
5 6 7 8 9 10 11 12
6 7 8 9 10 11 12 13
7 8 9 10 11 12 13 14
8 9 10 11 12 13 14 15
9 10 11 12 13 14 15 16
10 11 12 13 14 15 16 17
11 12 13 14 15 16 17 18
12 13 14 15 16 17 18 19
13 14 15 16 17 18 19 20
直接贴程序被判有敏感词不能发,截图试试。
运行输出:
[1, 0, 0, 0, 0, 0, 0, 0]
[8, 1, 0, 0, 0, 0, 0, 0]