JAVA第七次作业
1.编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值。 package ffff; public class aaa { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int []a= {10,20,30,40,50}; for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } }
2.编写一个简单程序,要求数组长度为5,动态赋值,并在控制台输出该数组的值。 package ffff; import java.util.Scanner; public class two { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); int []a=new int[5]; for (int i = 0; i < a.length; i++) { a[i]=input.nextInt(); } } }
3.定义字符型数组,分别存储c、h、 i、n、a 并在控制台输出 package ffff; public abstract class three { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub char [] a={'c','h','i','n','a'}; for (char i = 0; i < a.length; i++) { System.out.println(a[i]); } } }
4.输入5个学生成绩,求总分和平均分 package ffff; public class five { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int []a={12,53,23,44,53}; for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } for (int i : a) { System.out.println(i); } for (int i =5; i>a.length; i--) { System.out.println(a[i]); } } }
5. 定义数组{12,53,23,44,53} 用for和foreach分别输出,再倒序输出
package ffff; import java.util.Scanner; public class apex { public static void main(String[] args) { // TODO Auto-generated method stub int[] b ={12,53,23,44,53}; for (int c = 0; c< a.length; c++) System.out.println(+a[c]); System.out.println(); for (int c = a.length-1; c >=0; c--) System.out.println(a[c]); } }
![](https://img2020.cnblogs.com/blog/2320122/202104/2320122-20210420180937723-582317072.png)
6. 定义一个长度为10的整型数组,赋值(动态静态都可以)后求出奇数个数和偶数个数
package ffff; import java.util.Scanner; public class apex { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("输入十个数"); int a = 0; int b = 0; int[] z = new int[10]; for (int i = 0; i < z.length; i++) { z[i] = input.nextInt(); if (z[i] % 2 == 0) { a++; } else { b++; } } System.out.println("奇数" + b); System.out.println("偶数" + a); } }
![](https://img2020.cnblogs.com/blog/2320122/202104/2320122-20210420181200192-588245794.png)
![](https://img2020.cnblogs.com/blog/2320122/202104/2320122-20210420181200192-588245794.png)
7. 生成一个100长度数组,里面的数分别是1-100,并输出
package ffff; import java.util.Scanner; public class apex { public static void main(String[] args) { // TODO Auto-generated method stub int[]a=new int[100]; for (int i = 0; i < a.length; i++) { a[i]=i+1; System.out.println(a[i]); } } }
![](https://img2020.cnblogs.com/blog/2320122/202104/2320122-20210420181311581-481382683.png)
8. 定义一个double数组,存放10个学生的成绩,给所有同学加5分,不能超过100分。
package apex;
import java.util.Scanner;
public class apex {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("输入10位学生成绩");
double[] a = new double[10];
for (int b = 0; b< a.length; b++) {
a[b] = input.nextDouble();
}
for (int b = 0; b < a.length; b++) {
a[b] = a[b] + 5;
if (a[b] > 100) {
a[b] = 100;
}
}
System.out.println("加5分后");
for (int b= 0; b< a.length; b++) {
System.out.print(a[b] + " ");
}
}
}