标签:mamicode print generated 动态 输出 java i++ sum oid
1 package G; 2 3 public class G1 { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 int[] a = new int[] { 10, 20, 30, 40, 50 }; 8 for (int i = 0; i < a.length; i++) { 9 System.out.println(a[i]); 10 } 11 } 12 13 }
编写一个简单程序,要求数组长度为5,动态赋值,并在控制台输出该数组的值。
1 package G; 2 3 import java.util.Scanner; 4 5 public class G2 { 6 7 public static void main(String[] args) { 8 try (// TODO Auto-generated method stub 9 Scanner input = new Scanner(System.in)) { 10 int a[]=new int[5]; 11 for (int i = 0; i < a.length; i++) { 12 a[i]=input.nextInt(); 13 } 14 for (int i = 0; i < a.length; i++) { 15 System.out.println(a[i]); 16 } 17 } 18 } 19 }
3.定义字符型数组,分别存储c、h、 i、n、a 并在控制台输出
1 package G; 2 3 public class G3 { 4 5 public static void main(String[] args) { 6 char[] arr = new char[] { ‘c‘, ‘h‘, ‘i‘, ‘n‘, ‘a‘ }; 7 for (char c : arr) { 8 System.out.println(c); 9 } 10 11 } 12 13 }
1 package G; 2 3 import java.util.Scanner; 4 5 public class G4 { 6 7 public static void main(String[] args) { 8 try (// TODO Auto-generated method stub 9 Scanner input = new Scanner(System.in)) { 10 int[]a=new int[5]; 11 System.out.println("输入五个学生成绩"); 12 int sum=0; 13 double pj=0; 14 for (int i = 0; i < a.length; i++) { 15 a[i]=input.nextInt(); 16 } 17 for (int i = 0; i < a.length; i++) { 18 sum+=a[i]; 19 } 20 pj=sum/5; 21 System.out.println("总分"+sum+"平均分"+pj); 22 } 23 } 24 25 26 }
5.定义数组{12,53,23,44,53} 用for和foreach分别输出,再倒序输出
1 package G; 2 3 public class G5 { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 int []a={12,53,23,44,53}; 8 for (int i = 0; i < a.length; i++) { 9 System.out.println(a[i]); 10 } 11 System.out.println("--------------"); 12 13 for (int i : a) { 14 System.out.println(i); 15 } 16 17 System.out.println("倒序"); 18 for (int i=a.length-1; i>=0;i--) { 19 System.out.println(a[i]); 20 } 21 } 22 23 24 }
6.定义一个整型数组,赋值(动态静态都可以)后求出奇数个数和偶数个数
1 package G; 2 3 public class G6 { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 int a[]={11,22,33,44,55}; 8 int c=0; 9 int b=0; 10 for (int i = 0; i < a.length; i++) { 11 if (a[i]%2==0) { 12 b++; 13 } 14 if (a[i]%2!=0) { 15 c++; 16 } 17 } 18 System.out.println("偶数个数为"+b); 19 System.out.println("奇数个数为"+c); 20 } 21 22 23 24 }
定义一个整型数组,赋值后求出奇数个数和偶数个数
1 package G; 2 3 public class G7 { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 int[] array = {0, 1, 2, 3, 4, 5}; 8 int odd = 0; 9 int even = 0; 10 for (int n : array) { 11 if (n % 2 != 0) { 12 odd++; 13 } 14 else { 15 even++; 16 } 17 } 18 System.out.println(odd); 19 System.out.println(even); 20 } 21 22 23 }
生成一个100长度数组,里面的数分别是1-100,并输出
1 package G; 2 3 public class G8 { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 int[] array = new int[100]; 8 for (int n = 0; n < 100; n++) { 9 array[n] = n + 1; 10 } 11 12 for (int n : array) { 13 System.out.println(n); 14 } 15 } 16 17 }
标签:mamicode print generated 动态 输出 java i++ sum oid
原文地址:https://www.cnblogs.com/gxl920/p/14682383.html