JAVA第七周作业0416
package com.homework7; //1.编写一个简单程序,要求数组长度为5, //静态赋值10,20,30,40,50,在控制台输出该数组的值。 public class Exe7_1_1 { public static void main(String[] args) { int m[] = {10,20,30,40,50}; for (int i : m) { System.out.println(i); } } }
package com.homework7; //2.编写一个简单程序,要求数组长度为5,动态赋值,并在控制台输出该数组的值。 import java.util.Scanner; public class Exe7_1_2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int []m = new int[5]; System.out.println("请输入5个数"); for (int i = 0; i < m.length; i++) { m[i] = input.nextInt(); } for (int i = 0; i < m.length; i++) { System.out.println(m[i]); } } }
package com.homework7; //3.定义字符型数组,分别存储c、h、 i、n、a 并在控制台输出 public class Exe7_1_3 { public static void main(String[] args) { char m[] = {'c','h','i','n','a'}; for (char c : m) { System.out.print(c + "\t"); } } }
package com.homework7; //4.输入5个学生成绩,求总分和平均分 import java.util.Scanner; public class Exe7_1_4 { public static void main(String[] args) { Scanner input =new Scanner(System.in); double sum = 0; double []m =new double[5]; for (int i = 0; i < m.length; ) { System.out.print("请输入第"+(i+1)+"个学生成绩:"); m[i] =input.nextDouble(); if(0>m[i] || m[i]>100){ System.out.println("输入不合法,请重新输入!"); continue; }else{ sum += m[i]; i++; } } System.out.println("总分是:"+sum); System.out.println("平均分是:"+sum/5); } }
package com.homework7; //5.定义数组{12,53,23,44,53} 用for和foreach分别输出,再倒序输出 public class Exe7_1_5 { public static void main(String[] args) { int m[] ={12,53,23,44,53}; System.out.print("for输出:\t"); for (int i = 0; i < m.length; i++) { System.out.print(m[i] + "\t"); } System.out.println(); System.out.print("foreach输出:"); for (int i : m) { System.out.print(i + "\t"); } System.out.println(); System.out.print("倒序输出:\t"); for (int i = m.length-1; i >=0 ; i--) { System.out.print(m[i] + "\t"); } } }
package com.homework7; //1.定义一个整型数组,赋值(动态静态都可以)后求出奇数个数和偶数个数 import java.util.Scanner; public class Exe7_2_1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int jisum =0; int ousum =0; int []m = new int[10]; System.out.println("请输入10个整数,判断奇偶个数:"); for (int i = 0; i < m.length; i++) { m[i] = input.nextInt(); if (m[i] == 0) { } else if(m[i] %2==0) { ousum ++; }else{ jisum ++; } } System.out.println("奇数有" + jisum + "个!"); System.out.println("偶数有" + ousum + "个!"); } }
package com.homework7; //2.生成一个100长度数组,里面的数分别是1-100,并输出 public class Exe7_2_2 { public static void main(String[] args) { int []m =new int[100]; for (int i = 0; i < 100; i++) { m[i] =i+1; } for (int i : m) { System.out.print(i +" "); if(i%10==0) System.out.println(); } } }
package com.homework7; //3.定义一个double数组,存放10个学生的成绩,给所有同学加5分,不能超过100分。 import java.util.Scanner; public class Exe7_2_3 { public static void main(String[] args) { Scanner input =new Scanner(System.in); double []m =new double[10]; for (int i = 0; i < m.length; ) { System.out.print("请输入第"+(i+1)+"个学生成绩:"); m[i] =input.nextDouble(); if(0>m[i] || m[i]>100){ System.out.println("输入不合法,请重新输入!"); continue; } m[i] +=5; if(m[i]>100) m[i]=100; i++; } for (double d : m) { System.out.println(d); } } }