赞
踩
7-2 将数组中的数逆序存放 (20 分)
本题要求编写程序,将给定的n个整数存入数组中,将数组中的这n个数逆序存放,再按顺序输出数组中的元素。
输入在第一行中给出一个正整数n(1≤n≤10)。第二行输入n个整数,用空格分开。
在一行中输出这n个整数的处理结果,相邻数字中间用一个空格分开,行末不得有多余空格。
- 4
- 10 8 1 2
2 1 8 10
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner sc =new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
int i,j;
for(i=0;i<n;i++){
a[i]=sc.nextInt();
}
System.out.print(a[n-1]);
for(j=n-2;j>=0;j--){
System.out.print(" "+a[j]);
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。