java第七次作业
1.编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值
package NineDay; public class Test1 { public static void main(String[] args) { int [] arr = {10,20,30,40,50}; for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } }
2.编写一个简单程序,要求数组长度为5,动态赋值,并在控制台输出该数组的值。
package NineDay; import java.util.Scanner; public class Test2 { public static void main(String[] args) { System.out.println("输入五个数"); Scanner sc = new Scanner(System.in); int [] arr = new int[5]; for (int i = 0; i < arr.length; i++) { arr[i] = sc.nextInt(); } for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } }
3.定义字符型数组,分别存储c、h、 i、n、a 并在控制台输出
package NineDay; public class Test3 { public static void main(String[] args) { char [] crr = {'c','h','i','n','a'}; for (int i = 0; i < crr.length; i++) { System.out.print(crr[i]); } } }
4.输入5个学生成绩,求总分和平均分
package NineDay; import java.util.Scanner; public class Test4 { public static void main(String[] args) { System.out.println("输入学生成绩"); Scanner sc = new Scanner(System.in); double [] arr = new double[5]; double sum = 0; double avg = 0; for (int i = 0; i < arr.length; i++) { arr[i] = sc.nextInt(); sum += arr[i]; } avg = sum/arr.length; System.out.println("总分是:"+ sum +" "+"平均分是:"+avg); } }
5.定义数组{12,53,23,44,53} 用for和foreach分别输出,再倒序输出(for)
package NineDay; public class Test5 { public static void main(String[] args) { int [] arr = {12,53,23,44,53}; System.out.println("for遍历"); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]+" "); } System.out.println(); System.out.println("foreach遍历"); for (int i : arr) { System.out.print(i+" "); } System.out.println(); System.out.println("for倒序"); for (int i = 4; i >= 0; i--) { System.out.print(arr[i]+" "); } } }
作业
1.定义一个整型数组,赋值后求出奇数个数和偶数个数
package NineDay; public class Test6 { public static void main(String[] args) { int [] arr ={22,33,44,55,66,77,88,99,10,8}; int ou =0; int ji =0; for (int i = 0; i < arr.length; i++) { if(arr[i]%2==0){ ou++; }else{ ji++; } } System.out.println("偶数个数:"+ou+ " "+"奇数个数:"+ji); } }
2.生成一个100长度数组,里面的数分别是1-100,并输出
package NineDay; public class Test7 { public static void main(String[] args) { int [] arr = new int[100]; for (int i = 0; i < arr.length; i++) { arr[i]=i+1; } for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } }
3.定义一个double数组,存放10个学生的成绩,给所有同学加5分,不能超过100分。
package homework; import java.util.Scanner; public class tets20 { public static void main(String[] args) { System.out.print("输入学生成绩"); double[] drr = new double[10]; Scanner sc = new Scanner(System.in); for (int i = 0; i < drr.length; i++) { drr[i] = sc.nextDouble(); } for (int i = 0; i < drr.length; i++) { if (drr[i] < 100 && drr[i] + 5 > 100) { drr[i] = 100; System.out.print(drr[i] + " "); } else if (drr[i] < 100 && drr[i] + 5 < 100) { drr[i] = drr[i] + 5; System.out.print(drr[i] + " "); } } } }
posted on 2021-04-18 21:03 chenyulin11 阅读(38) 评论(0) 编辑 收藏 举报