第七次
1.编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值。
package sjw; public class word1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[]={10,20,30,40,50}; for (int i = 0; i <5; i++) { System.out.println(a[i]); } } }
2.编写一个简单程序,要求数组长度为5,动态赋值,并在控制台输出该数组的值
package sjw; import java.util.Scanner; public class work2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int []a=new int[5]; Scanner input=new Scanner(System.in); for (int i = 0; i < a.length; i++) { a[i]=input.nextInt(); } for (int i = 0; i < a.length; i++) { System.out.println("输出"+a[i]); } } }
3.定义字符型数组,分别存储c、h、 i、n、a 并在控制台输出
package sjw; public class work3 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub char []a={'c','h','i','n','a'}; for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } }
4.输入5个学生成绩,求总分和平均分
package sjw; import java.util.Scanner; public class work4 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int sum=0; double ave=0; int []a=new int[5]; Scanner input=new Scanner(System.in); for (int i = 0; i < a.length; i++) { a[i]=input.nextInt(); } for (int i = 0; i < a.length; i++) { sum+=a[i]; } ave=sum/5.0; System.out.println("总成绩为"+sum); System.out.println("平均成绩为"+ave); } }
5.定义数组{12,53,23,44,53} 用for和foreach分别输出,再倒序输出
package sjw; public class work53 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int []a={12,53,23,44,53}; for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } for (int i = 4; i >=0; i--) { System.out.println("倒叙输出"+a[i]); } } }
package sjw; public class work53 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int []a={12,53,23,44,53}; for (int i : a) { System.out.println(i); } for (int i = 4; i >=0; i--) { System.out.println("倒叙输出"+a[i]); } } }
1.定义一个整型数组,赋值(动态静态都可以)后求出奇数个数和偶数个数
package sjw; import java.util.Scanner; public class work2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[]=new int[5]; Scanner input=new Scanner(System.in); int j=0,k=0; for (int i = 0; i < a.length; i++) { a[i]=input.nextInt(); } for (int i = 0; i < a.length; i++) { if(a[i]%2==1){ j++; } if(a[i]%2==0){ k++; } } System.out.println("奇数个数为"+j); System.out.println("偶数个数为"+k); } }
2.生成一个100长度数组,里面的数分别是1-100,并输出
package sjw; public class work53 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[]=new int[100]; for(int i=0;i<100;i++){ a[i]=i+1; System.out.println(a[i]); } } }
3.定义一个double数组,存放10个学生的成绩,给所有同学加5分,不能超过100分。
package sjw; import java.util.Scanner; public class work2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub double a[]=new double[10]; double sum=0; Scanner input=new Scanner(System.in); for (int i = 0; i < a.length; i++) { System.out.println("输入第"+(i+1)+"个学生成绩"); a[i]=input.nextDouble(); } for (int i = 0; i < a.length; i++) { sum=a[i]+5; if(sum>=100){ System.out.println("加分后成绩为100分"); } else { System.out.println("加5分后成绩为"+sum+"分"); } }} }