【发布时间】:2018-10-01 21:28:26
【问题描述】:
我有 3 个数组 A、B 和 C。A 和 B 的大小为 6,C 的大小为 12。我必须将 A 和 B 组合成 C,但使用 A 的前 3 个整数,然后是第一个B 的 3 个整数,然后是 A 的下 3 个整数,最后是 B 的最后 3 个整数。例如:int A[]={1,2,3,7,8,9}, B[]={4, 5,6,10,11,12} 并用 C[]={1,2,3,4,5,6,7,8,9,10,11,12}
填充 C到目前为止,这是我的代码:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] A = new int[6];
int[] B = new int[6];
int[] C = new int[12];
int count = 0;
for (int i = 0; i < 6; i++) {
System.out.println("Array A: ");
A[i]=sc.nextInt();
System.out.println("Array B: ");
B[i]=sc.nextInt();
}
System.out.println("");
System.out.print("|");
for (int i = 0; i < 6; i++) {
System.out.print(A[i]+"|");
}
System.out.println("");
System.out.print("|");
for (int i = 0; i < 6; i++) {
System.out.print(B[i]+"|");
}
while(count < 3){
count++;
{
我真的迷路了这个
【问题讨论】:
-
从您的代码看来,您可以拥有多个
for-loops。在您的第一个for-loop中,从 0 循环到 2,存储第一个数组中的前 3 个。在您的第二个for-loop中,从 0 循环到 2,存储第二个数组中的前 3 个。在您的第三个for-loop中,从 3 循环到 5,将最后一个 3 存储在第一个索引中,依此类推。 -
首先尝试完成第一步:将
A的前三个元素复制到C的前三个位置。