黑马程序员技术交流社区

标题: 急求感激万分 [打印本页]

作者: gaol    时间: 2016-9-20 22:23
标题: 急求感激万分
int[] array=new int[]{1,3,-1,5,-2};
现要创建一个新的数组newArray[],要求新数组中的元素与原数组中的元素逆序,并且如果原数组中的元素值小于0,在新数组中按0存储。编程出输入新数组中的元素。程序运行结果:
原数组为:
1 3 -1 5 -2
逆序处理后的数组为:
0 5 0 3 1
哪位大侠帮忙解决下 谢谢啦
作者: zhangjiesss    时间: 2016-9-21 12:49
public class DenoShow7 {

         public static void main(String[] args) {
               
                int[] array=new int[]{1,3,-1,5,-2};
               
                System.out.println("原数组为:");
               
                 for (int i = 0; i < array.length; i++) {
                       
                        System.out.print(array[i]+" ");       
                }
                 System.out.println();
                
                 //对数组进行反转
               
                 for (int i = 0,end=array.length-1; i < end; i++,end--) {
                       
                        int temp=array[i];
                          array[i]=array[end];
                         array[end]=temp;
                       
                 }
                 System.out.println("逆序处理后的数组为:");
                 
                 //判断条件,遍历存进新数组
               
                for (int i = 0; i < array.length; i++) {
                       
                        if (array[i]<0) {
                               
                                array[i]=0;
                        }
                        int[] newarray={array[i]};
                       
                        for (int j = 0; j < newarray.length; j++) {
                               
                                System.out.print(newarray[j]+" ");
                        }
                       
                }
       
        }

}

看一下,应该对你有帮助




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2