第七周作业
- 编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值。
package com.wyn; public class HomeWork { /** * @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 : a) { System.out.println(i); } } }
2.编写一个简单程序,要求数组长度为5,动态赋值,并在控制台输出该数组的值。
package com.wyn; import java.util.Scanner; public class HomeWork2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner s = new Scanner(System.in); int a[] = new int[5]; for(int i = 0 ; i<5 ; i++){ a[i] = s.nextInt(); } for (int i : a) { System.out.println(i); } } }
3.定义字符型数组,分别存储c、h、 i、n、a 并在控制台输出
package com.wyn; import java.util.Scanner; public class HomeWork3 { public static void main(String[] args) { // TODO Auto-generated method stub char a[] = new char[] {'c','h','i','n','a'}; for (char c : a) { System.out.println(c); } } }
4.输入5个学生成绩,求总分和平均分
package com.wyn; import java.util.Scanner; public class WorkHome4 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner s = new Scanner(System.in); int a[] = new int[5]; int sum=0; for(int i = 0 ; i<5 ; i++){ a[i] = s.nextInt(); } for (int i : a) { sum=sum+i; } System.out.println("zongfen"); System.out.println(sum); System.out.println("pingjunfen"); System.out.println(sum/5.0); } }
5.定义数组{12,53,23,44,53} 用for和foreach分别输出,再倒序输出
package com.wyn; import java.util.Scanner; public class HomeWork5 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner s = new Scanner(System.in); int a[] = new int[] {12,53,23,44,53}; for(int i = 0 ; i<5 ; i++){ System.out.println(a[i]); } for (int i : a) { System.out.println(i); } for(int i = 4 ; i>=0 ; i--){ System.out.println(a[i]); } } }
作业:
1.定义一个整型数组,赋值(动态静态都可以)后求出奇数个数和偶数个数
package com.wyn; import java.util.Scanner; public class HomeWork6 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner s = new Scanner(System.in); int a[] = new int[10]; int jishu=0; int oushu=0; for(int i = 0 ; i<10 ; i++){ a[i] = s.nextInt(); if(a[i]%2==0){ oushu++; }else{ jishu++; } } System.out.println("oushu"); System.out.println(oushu); System.out.println("jishu"); System.out.println(jishu); } }
2.生成一个100长度数组,里面的数分别是1-100,并输出
package com.wyn; import java.util.Scanner; public class HomeWork7 { /** * @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; } for(int i = 0 ; i<100 ; i++){ System.out.println(a[i]); } } }
3.定义一个double数组,存放10个学生的成绩,给所有同学加5分,不能超过100分。
package com.wyn; public class HomeWork8 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub double a[] = new double[] {99,98,65,76,87,76,54,76,34,38}; for(int i =0 ; i<10 ; i++){ if(a[i]>95){ continue; }else{ a[i]=a[i]+5; } } for(int i = 0 ; i<10 ; i++){ System.out.println(a[i]); } } }