简体   繁体   English

有人可以教我如何获得这个 output

[英]Can someone teach me how to get this output

I'm new to java.我是 java 的新手。 I'm trying to make my program output this [5,4] [3] [2,1] but instead I get [5,5] [4,4] [3,3] [2,2] [1,1].. what am I missing?我正在尝试使我的程序 output 这个 [5,4] [3] [2,1] 但我得到 [5,5] [4,4] [3,3] [2,2] [1, 1] ..我错过了什么? I tried to answer it on my own but I just can't think of the answer.我试图自己回答,但我就是想不出答案。

Here's my full code:这是我的完整代码:

   public static void main(String[]args){
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter Array Size:");
    int arrSize = sc.nextInt();
    System.out.println("The Size of your Array is "+ arrSize);
    int arr[] = new int[arrSize];
    System.out.println("Enter "+arrSize+" Elements of your Array: ");
    for(int i=0;i<arr.length;i++){
        arr[i] = sc.nextInt();
    }
    for(int i=0; i<arr.length;i++){
        System.out.print(arr[i] + " ");
    }
    System.out.println(" ");
    for(int i=arr.length-1; i>=0;i--){
        System.out.print(Arrays.asList(arr[i]+","+arr[i]));
    }

}

try this code试试这个代码

        Scanner sc=new Scanner(System.in);
        System.out.print("Enter Array Size:");
        int f,midd=0;
        int arrSize = sc.nextInt();
        System.out.println("The Size of your Array is "+ arrSize);
        int arr[] = new int[arrSize];
        System.out.println("Enter "+arrSize+" Elements of your Array: ");
        for(int i=0;i<arr.length;i++){
            arr[i] = sc.nextInt();
        }
        if(arrSize%2==0){
            f=0;
        }
        else {
            f=1;
            midd=(int)(arrSize/2/2)+1;
        }
        for(int i=arrSize-1; i>=0;){
            if(f==0)
            {
                System.out.print(Arrays.asList(arr[i]+","+arr[i-1]));
                i-=2;
            }
            else{
                if(midd==i){
                    System.out.print(Arrays.asList(arr[i]));
                    i--;
                }
                else {
                    System.out.print(Arrays.asList(arr[i]+","+arr[i-1]));
                    i-=2;
                }
            }
        }

provide this program inside main method Output:在主要方法 Output 中提供此程序:

Enter Array Size:5
The Size of your Array is 5
Enter 5 Elements of your Array: 
1
2
3
4
5
[5,4][3][2,1]

. .

Enter Array Size:4
The Size of your Array is 4
Enter 4 Elements of your Array: 
1
2
3
4
[4,3][2,1]

. .

Enter Array Size:7
The Size of your Array is 7
Enter 7 Elements of your Array: 
1
2
3
4
5
6
7
[7,6][5,4][3][2,1]

YOu can try the following.您可以尝试以下方法。 I think what you need is to split the array as a pair.我认为您需要将数组拆分为一对。

I am assuming you will have middle pair with one element in case of odd length and all pairs will have two elements in case of even length of the array.我假设在奇数长度的情况下,您将有一个带有一个元素的中间对,而在数组长度为偶数的情况下,所有对都将有两个元素。

public static void main(String[]args){
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter Array Size:");
    int arrSize = sc.nextInt();
    System.out.println("The Size of your Array is "+ arrSize);
    int arr[] = new int[arrSize];
    System.out.println("Enter "+arrSize+" Elements of your Array: ");
    for(int i=0;i<arr.length;i++){
        arr[i] = sc.nextInt();
    }
    for(int i=0; i<arr.length;i++){
        System.out.print(arr[i] + " ");
    }
    System.out.println(" ");
    int i=arr.length-1;
    for(; i>arr.length/2;i-=2){
        System.out.print(Arrays.asList(arr[i]+","+arr[i-1]));
    }
    if(arr.length %2 == 0){
        System.out.print(Arrays.asList(arr[i]+","+arr[i-1]));
        i-=2;
    }else{
        System.out.print(Arrays.asList(arr[i]));
        i-=1;
    }
    for(; i>0;i-=2){
        System.out.print(Arrays.asList(arr[i]+","+arr[i-1]));
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人可以教我如何将此段落条目编写为布尔表达式吗? - Can someone teach me how to write this Paragraph Entry as a boolean expression? 初学者。 有人可以教我如何计算内部循环被执行的次数吗? - Beginner. Can someone teach me how to count the number of times the inner loop has been excecuted? 有人可以告诉我为什么我得到这个输出吗? - Can someone tell me why I get this output? 有人可以教我如何解释 javax.net.debug 结果吗? - Could someone teach me how to interpret the javax.net.debug result? 有人可以告诉我如何从jtf1和jtf2获取输入并在jtf3上显示输出吗? - Can someone Tell me how can i get Input from jtf1 and jtf2 and Display the output on jtf3? 有人可以告诉我如何在Eclipse中显示菜单栏吗? - Can someone tell me how to get the menu bar to show in Eclipse? 教我为什么此代码无法检查我的输入是否为int,以及如何解决它 - Teach me why this code fails to check if my input is a int, and how I can fix it 有人可以向我解释rayCast的工作原理吗? - Can someone explain me how rayCast works? 有人可以向我解释这种方法如何工作吗? - Can someone explain to me how this method works? 有人可以向我解释为什么当我从Java运行相同的命令时为什么得到不同的输出 - Can someone please explain to me why I get a different output when I run the same command from Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM