第七次作业
1.编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值。
package mmm; public class hhh { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[]=new int[]{10,20,30,40,50}; for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } }
2.编写一个简单程序,要求数组长度为5,动态赋值,并在控制台输出该数组的值。
package mmm; import java.util.Scanner; public class hhh { /** * @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 mmm; import java.util.Scanner; public class hhh { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub char a[]=new char[]{'c','h','i','n','a'}; for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } }
4.输入5个学生成绩,求总分和平均分
package mmm; import java.util.Scanner; public class hhh { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stu Scanner input=new Scanner(System.in); System.out.println("输入5个学生成绩"); int []a=new int[5]; int sum=0; int avenage=0; for (int i = 0; i < a.length; i++) { a[i]=input.nextInt(); } for (int i = 0; i < a.length; i++) { sum+=a[i]; } avenage=sum/a.length; System.out.println("总分为"+sum+""+"平均值为"+avenage); } }
5.定义数组{12,53,23,44,53} 用for和foreach分别输出,再倒序输出
package mmm; import java.util.Random; import java.util.Scanner; public class hhh { /** * @param args */ public static void main(String[] args) { int a[] = new int []{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 (int i = 4; i >= 0; i--) { System.out.println(a[i]); } } }
作业:
1.定义一个整型数组,赋值后求出奇数个数和偶数个数
package mmm; import java.util.Random; import java.util.Scanner; public class hhh { /** * @param args */ public static void main(String[] args) { int a[] = new int []{43,68,66,98,88}; int c = 0,b = 0; for (int i = 0; i < a.length; i++) { if (a[i]%2==0) { c++; } if (a[i]%2!=0) { b++; } } System.out.println("偶数的个数是"+c+"奇数的个数是"+b); } }
2.生成一个100长度数组,里面的数分别是1-100,并输出
package mmm; import java.util.Random; import java.util.Scanner; public class hhh { /** * @param args */ public static void main(String[] args) { int a[] = new int [100]; for (int i = 0; i < a.length; i++) { a[i]=i+1; } for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } }
3.定义一个double数组,存放10个学生的成绩,给所有同学加5分,不能超过100分。
package mmm; import java.util.Random; import java.util.Scanner; public class hhh { /** * @param args */ public static void main(String[] args) { Scanner input = new Scanner(System.in); 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++) { a[i] = a[i]+5; if (a[i]>=100) { a[i]=100; } } for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } }