Stack Overflow Asked by surtz on December 20, 2020
I’m new to 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? 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:
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]
Correct answer by sourab maity on December 20, 2020
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]));
}
}
Answered by Lone wolf on December 20, 2020
1 Asked on February 28, 2021 by eric-cai
2 Asked on February 28, 2021
2 Asked on February 28, 2021 by kacie-freeman
2 Asked on February 28, 2021 by davidgyoung
appdelegate ios13 nsnotificationcenter swiftui uiscenedelegate
0 Asked on February 28, 2021 by aurreco
1 Asked on February 28, 2021 by user1323163
3 Asked on February 27, 2021 by amrit-singh
4 Asked on February 26, 2021 by girkovarpa
0 Asked on February 26, 2021 by lakshan-costa
0 Asked on February 26, 2021 by sean-page
1 Asked on February 25, 2021
2 Asked on February 25, 2021 by pranav
1 Asked on February 25, 2021 by telescope
1 Asked on February 25, 2021 by coderman
1 Asked on February 25, 2021 by mahmoud-harooney
4 Asked on February 25, 2021 by marius
1 Asked on February 25, 2021 by paperperson
Get help from others!
Recent Answers
© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP