第七次作业
1.编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值。
package School.Day8; public class Test01 { public static void main(String[] args){ int[] x = {10,20,30,40,50}; for (int i = 0;i<x.length;i++) { System.out.println(x[i]); } } }
2.编写一个简单程序,要求数组长度为5,动态赋值,并在控制台输出该数组的值。
package School.Day8; import java.util.Scanner; public class Test02 { public static void main(String[] args){ Scanner s = new Scanner(System.in); int[] x = new int[5]; for (int i = 0;i<x.length;i++) { System.out.print("赋值: "); int a = s.nextInt(); x[i] = a; } for (int i = 0;i<x.length;i++) { System.out.println(x[i]); } } }
3.定义字符型数组,分别存储c、h、 i、n、a 并在控制台输出
package School.Day8; public class Test03 { public static void main(String[] args){ char[] x = {'C','h','i','n','a'}; for (int i = 0;i<x.length;i++) { System.out.print(x[i] + " "); } } }
4.输入5个学生成绩,求总分和平均分
package School.Day8; import java.util.Scanner; public class Test04 { public static void main(String[] args){ int[] x = new int[5]; int sum = 0; Scanner s = new Scanner(System.in); for (int i = 0;i<x.length;i++) { System.out.print("输入成绩: "); int a = s.nextInt(); x[i] = a; } for (int i =0;i<x.length;i++) { sum = sum + x[i]; } System.out.println("总和是: " + sum); System.out.println("平均是: " + sum / 5.0); } }
5.定义数组{12,53,23,44,53} 用for和foreach分别输出,再倒序输出
package School.Day8;
public class Test05 {
public static void main(String[] args){
int[] x = {12,53,23,44,53};
for (int i = 4;i>= 0;i--) {
System.out.println(x[i]);
}
System.out.println("==================");
int[] y = new int[5];
for (int i = 0;i<y.length;i++) {
y[i] = x[4-i];
}
for (int i : y) {
System.out.println(i);
}
}
}
6.定义一个整型数组,赋值(动态静态都可以)后求出奇数个数和偶数个数
package School.Day8;
import java.util.Scanner;
public class Test06 {
public static void main(String[] args){
int[] x = new int[10];
int ji = 0;
int ou = 0;
Scanner s = new Scanner(System.in);
for (int i = 0;i<x.length;i++) {
System.out.print("赋值: ");
int a = s.nextInt();
x[i] = a;
}
for (int i = 0;i<x.length;i++) {
if (x[i] % 2 != 0) {
ji = ji + 1;
}else{
ou = ou + 1;
}
}
System.out.println("有奇数: " + ji + "个");
System.out.println("有偶数: " + ou + "个");
}
}
7.生成一个100长度数组,里面的数分别是1-100,并输出
package School.Day8; public class Test07 { public static void main(String[] args){ int[] x = new int[100]; int y = 0; for (int i = 0;i<x.length;i++) { y = i + 1; System.out.println(y); } } }
8.定义一个double数组,存放10个学生的成绩,给所有同学加5分,不能超过100分
package School.Day8; import java.util.Scanner; public class Test08 { public static void main(String[] args){ double[] x = new double[10]; Scanner s = new Scanner(System.in); for (int i = 0;i<x.length;i++) { System.out.print("输入" + (i+1) +"号学生成绩: "); double c = s.nextDouble(); x[i] = c; } for (int i = 0;i<x.length;i++) { if (x[i] > 95) { System.out.println((i+1) + "号学生成绩大于95,不能加分!"); }else{ x[i] = x[i] + 5; } } for (int i = 0;i<x.length;i++) { System.out.println((i+1) + "号学生加分后成绩为: " + x[i]); } } }