java第七次作业
1.编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值。
packagesjlx; publicclasssjlx { /** * @paramargs */ publicstaticvoid main(String[] args) { // TODO Auto-generated method stub int []a=newint[]{10,20,30,40,50}; for (int i=0;i<a.length;i++){ System.out.println(a[i]); } } }
2.编写一个简单程序,要求数组长度为5,动态赋值,并在控制台输出该数组的值。
packagesjlx; importjava.util.Scanner; publicclass sjlx2 { /** * @paramargs */ publicstaticvoid main(String[] args) { // TODO Auto-generated method stub int []a=newint[5]; Scanner input=newScanner(System.in); for (int i=0;i<a.length;i++){ a[i]=input.nextInt(); System.out.println(a[i]); } } }
3.定义字符型数组,分别存储c、h、 i、n、a 并在控制台输出
packagesjlx; publicclass sjlx3 { /** * @paramargs */ publicstaticvoid main(String[] args) { // TODO Auto-generated method stub char []a=newchar[]{'c','h','i','n','a'}; for (int i=0;i<a.length;i++){ System.out.println(a[i]); } } }
4.输入5个学生成绩,求总分和平均分
packagesjlx; importjava.util.Scanner; publicclass sjlx4 { /** * @paramargs */ publicstaticvoid main(String[] args) { // TODO Auto-generated method stub int[]a=newint[5]; int sum=0; System.out.println("请输入5位学生的成绩"); Scanner input=newScanner(System.in); for(int i=0;i<a.length;i++){ System.out.println("请输入第"+(i+1)+"位学生成绩"); a[i]=input.nextInt(); sum+=a[i]; } System.out.println("总分为"+sum); doubleavg=sum/5; System.out.println("平均分为"+avg); } }
5.定义数组{12,53,23,44,53} 用for和foreach分别输出,再倒序输出(for)
packagesjlx; publicclass sjlx5 { /** * @paramargs */ publicstaticvoid main(String[] args) { // TODO Auto-generated method stub int []a={12,53,23,44,53}; System.out.println("for输出"); for (int i=0;i<a.length;i++){ System.out.println(a[i]); } System.out.println("foreach输出"); for (int i:a){ System.out.println(i); } System.out.println("for倒序输出"); for (int j=4;j>=0;j--){ System.out.println(a[j]); } } }
作业
1.定义一个长度为10的整型数组,赋值(动态静态都可以)后求出奇数个数和偶数个数
packagesjlx; publicclass Zy1 { /** * @paramargs */ publicstaticvoid main(String[] args) { // TODO Auto-generated method stub int []x={1,2,3,4,5,6,7,8,9,10}; int a=0,b=0; for (int i=0;i<x.length;i++){ if(x[i]%2==1){ a++;} if(x[i]%2==0){ b++; } } System.out.println("奇数的个数为"+a); System.out.println("偶数的个数为"+b); } }
2.生成一个100长度数组,里面的数分别是1-100,并输出
package exe2; public class text6 { public static void main(String[] args) { int a[]=new int [100]; int b=1; for(int i=0;i<100;i++){ a[i]=b; b++; System.out.print(a[i]+","); } } }
3.定义一个double数组,存放10个学生的成绩,给所有同学加5分,不能超过100分。
package exe2; import java.util.Scanner; public class text7 { public static void main(String[] args) { // TODO Auto-generated method stub double a[]=new double [10]; Scanner input=new Scanner(System.in); for(int i=0;i<10;i++){ a[i]=input.nextInt(); if(a[i]<95) a[i]+=5; if(a[i]>=95) a[i]=100; } for (int i = 0; i < a.length; i++) { System.out.println("加五分为"+a[i]); } } }