Java学习之小练习二

来源:互联网 发布:微信直接打开淘宝 编辑:程序博客网 时间:2024/04/30 23:43
package com.deu.homework;/** * 定义一个数组,并把数组元素遍历出来 * @author Administrator * */public class Demo {public static void main(String[] args) {int[] a ={1,3,4,7,8};for (int i = 0; i < a.length; i++) {System.out.println(a[i]);}}}

package com.deu.homework;/** * 定义一个数组,找出数组中的最大值 * @author Administrator * */public class Demo2 {public static void main(String[] args) {int[] a = {1,3,6,8,2,5};int max = a[0];for (int i = 0; i < a.length; i++) {if (a[i] > max) { max = a[i];}}System.out.println(max);}}

package com.deu.homework;/** * 定义一个数组,把数组反向打印 * @author Administrator * */public class Demo3 {public static void main(String[] args) {int[] a = {1,3,5,6,8,2,5};for (int i = a.length-1; i >=0 ; i--) {//数组反向输出的时候由于数组长度是从0开始的,所以长度要减1, i要大于等于0System.out.print(a[i]+" ");}}}

package com.deu.homework;/** * 定义一个数组,查找元素在数组中第一次出现的位置 * @author Administrator * */public class Demo4 {public static void main(String[] args) {int[] a = {1,2,3,4,5,2,3};int b = 3;for (int i = 0; i < a.length; i++) {if(a[i] == b){System.out.println(i);break;}}}}

0 0
原创粉丝点击