您的位置:首页 > 其它

编写程序,将一个数组中的元素倒排过来。例如原数组为1,2,3,4,5;则倒排后数组中的值为5,4,3,2,1。

2010-12-17 20:15 771 查看
public class _5{
public static void main(String[] argv){
int[] array2 = new int[50];
int i = 0,temp;
Scanner input = new Scanner(System.in);
boolean t = true;
//循环输入
while(t){
try{
array2[i] =input.nextInt();
}catch(Exception e){
t = false;
}
i++;
}
//初始化数组
int[] array = new int[i - 1];
i = 0;
//将array2中的值赋给array
while(i < array.length){
array[i] = array2[i];
i++;
}
i = 0;
int j = array.length - 1 ;
//将一个数组中的元素顺序输出
System.out.println("数组中的元素原样输出为:");
while(i < array.length){
System.out.println(array[i]);
i++;
}
//将一个数组中的元素倒序排列
i = 0;
while(i < array.length/2 ){
if(j > array.length/2 ){
temp = array[i];
array[i] = array[j];
array[j] = temp;
j--;
}
i++;
}
//将一个数组中的元素倒序输出
System.out.println("数组中的元素倒排过来输出为:");
i = 0;
while(i < array.length){
System.out.println(array[i]);
i++;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐