1.编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值
package a; public class aaa { public static void main(String[] args) { int a[]= {10,20,30,40,50}; for(int i :a) { System.out.println(i); } } }
2.编写一个简单程序,要求数组长度为5,动态赋值,并在控制台输出该数组的值。
package prac; import java.util.Scanner; public class prac2 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("请输入数组的值"); int []a=new int[5]; for (int i = 0; i < a.length; i++) { a[i]=input.nextInt(); } System.out.println("数组的值为"); for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } }
3.定义字符型数组,分别存储c、h、 i、n、a 并在控制台输出
package prac; public class prac3 { public static void main(String[] args) { // TODO Auto-generated method stub String []f= {"c","h","i","n","a"}; for (int e = 0; e< f.length; e++) { System.out.println(f[e]); } } }
4.输入5个学生成绩,求总分和平均分
package prac; import java.util.Scanner; public class prac4 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); int sum=0; int []a=new int[5]; for (int i = 0; i < a.length; i++) { System.out.println("请输入第"+(i+1)+"个学生的成绩"); a[i]=input.nextInt(); } for (int i = 0; i < a.length; i++) { sum+=a[i]; } System.out.println("sum="+sum); System.out.println("aver="+sum/a.length); }
5.定义数组{12,53,23,44,53} 用for和foreach分别输出,再倒序输出
package prac; public class prac5 { public static void main(String[] args) { // TODO Auto-generated method stub int []a= {12,53,22,44,53}; /* System.out.println("正常输出为"); for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } System.out.println("倒序输出为"); for (int i =a.length-1; i>=0; i--) { System.out.println(a[i]); } */ for (int i : a) { System.out.println(i); }
} }
6定义一个整型数组,赋值(动态静态都可以)后求出奇数个数和偶数个数
package prac; public class prac6 { public static void main(String[] args) { // TODO Auto-generated method stub int []a= {1,2,3,4,5,6,8,9,10,12,13}; int j=0; int o=0; for (int i = 0; i < a.length; i++) { if (a[i]%2==0) { o++; }else { j++; } } System.out.println("奇数为"+j+"个"); System.out.println("偶数为"+o+"个"); } }
7.生成一个100长度数组,里面的数分别是1-100,并输出
package prac; public class prac7 { public static void main(String[] args) { // TODO Auto-generated method stub int []a=new int[100]; for (int i =1; i < a.length+1; i++) { a[i-1]=i; } System.out.println("输出为"); for (int i = 0; i < a.length; i++) { System.out.println(i+1); } } }
8.定义一个double数组,存放10个学生的成绩,给所有同学加5分,不能超过100分。
package prac; import java.util.Scanner; public class prac8 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("输入学生成绩"); double []a=new double[10]; for (int i = 0; i < a.length; i++) { a[i]=input.nextDouble(); } for (int i = 0; i < a.length; i++) { if (a[i]+5>=100) { System.out.println(100); }else if(a[i]+5>0) System.out.println(a[i]+5); } } }
标签:int,System,++,Java7,println,public,out From: https://www.cnblogs.com/xc20/p/17327800.html