第七周上机
1.编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值。
package com.fj.sjlx; public class Sjlx { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int []a={10,20,30,40,50}; System.out.println(a[0]); System.out.println(a[1]); System.out.println(a[2]); System.out.println(a[3]); System.out.println(a[4]); } }
2.编写一个简单程序,要求数组长度为5,动态赋值,并在控制台输出该数组的值。
package com.fj.sjlx; import java.util.Scanner; public class Sjlx2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("请输入五个数"); Scanner input=new Scanner(System.in); int []a=new int[5]; for (int i = 0; i < a.length; i++) { a[i]=input.nextInt(); } for (int i = 0; i < a.length; i++) { System.out.print(a[i]+"、"); } } }
3.定义字符型数组,分别存储c、h、 i、n、a 并在控制台输出
package com.fj.sjlx; public class Sjlx3 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String a[]={"c","h","i","n","a"}; for (int j=0;j<a.length;j++){ System.out.println(a[j]); } } }
4.输入5个学生成绩,求总分和平均分
package com.fj.sjlx; import java.util.Scanner; public class Sjlx4 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("请输入五个学生的成绩"); Scanner input=new Scanner(System.in); double []a=new double[5]; double sum=0; double pjs; for (int i = 0; i < a.length; i++) { a[i]=input.nextDouble(); sum+=a[i]; } pjs=sum/5; System.out.println("五个学生成绩总和为:"+sum); System.out.println("五个学生平均成绩为:"+pjs); } }
5.定义数组{12,53,23,44,53} 用for和foreach分别输出,再倒序输出(for)
package com.fj.sjlx; public class Sjlx5 { /** * @param args */ public static void 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.print(a[i]+" "); } System.out.println(); System.out.println("foreach遍历为:"); for (int i : a) { System.out.print(i+" "); } System.out.println(); System.out.println("倒序输出为:"); for (int j = a.length-1; j >=0; j--) { System.out.print(a[j]+" "); } } }
作业:
1.定义一个长度为10的整型数组,赋值(动态静态都可以)后求出奇数个数和偶数个数
package com.fj.home; import java.util.Scanner; public class Homework { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("请输入十个整数:"); Scanner input=new Scanner(System.in); int []a=new int [10]; int js=0,os=0; for (int i = 0; i < a.length; i++) { a[i]=input.nextInt(); if(a[i]%2==0){ os++; }else{ js++; } } System.out.println("奇数个数和为:"+js); System.out.println("偶数个数和为:"+os); } }
2.生成一个100长度数组,里面的数分别是1-100,并输出
package com.fj.home; import java.util.Scanner; public class Homework { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); int[] a = new int[100]; for (int i = 0; i < a.length; i++) { for (int j = 1; j < 101; j++) { a[i] = j; System.out.println(a[i] + " "); } break; } } }
3.定义一个double数组,存放10个学生的成绩,给所有同学加5分,不能超过100分。
package com.fj.home; import java.util.Scanner; public class Homework { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("请输入学生成绩:"); Scanner input=new Scanner(System.in); double []a = new double[10]; for (int i = 0; i < a.length; i++) { a[i]=input.nextInt(); if (a[i] < 95) { a[i] += 5; } else { a[i] = 100; } } System.out.println("加分后成绩为:"); for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } }