第七次上机作业
1. 编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值.
package apex; public class apex { 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 apex; public class apex { public static void main(String[] args) { // TODO Auto-generated method stub int[] a = new int[5]; a[0] = 1; a[1] = 2; a[2] = 3; a[3] = 4; a[4] = 4; for (int i = 0; i < a.length; i++) System.out.println(a[i]); } }
3. 定义字符型数组,分别存储c、h、 i、n、a 并在控制台输出
package apex; public class apex { public static void main(String[] args) { // TODO Auto-generated method stub char[] a ={'c','h','i','n','a'}; for (int i = 0; i < a.length; i++) System.out.println(a[i]); } }
4. 输入5个学生成绩,求总分和平均分
package apex; import java.util.Scanner; public class apex { public static void main(String[] args) { // TODO Auto-generated method stub int[] a = new int[5]; Scanner sc = new Scanner(System.in); for (int i = 0; i < a.length; i++) { System.out.println("请输入第" + (i + 1) + "学生成绩"); a[i] = sc.nextInt(); } System.out.println("总分"); int sum = 0; for (int j = 0; j < a.length; j++) { sum += a[j]; } System.out.println(sum); System.out.println("平均分"); double x = sum / 5; System.out.println(x); } }
5. 定义数组{12,53,23,44,53} 用for和foreach分别输出,再倒序输出
package apex; import java.util.Scanner; public class apex { 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]); System.out.println(); for (int i = a.length-1; i >=0; i--) System.out.println(a[i]); } }
6. 定义一个长度为10的整型数组,赋值(动态静态都可以)后求出奇数个数和偶数个数
package apex; 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]); } } }
7. 生成一个100长度数组,里面的数分别是1-100,并输出
package apex; 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]); } } }
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 x = 0; x < a.length; x++) {
a[x] = input.nextDouble();
}
for (int x = 0; x < a.length; x++) {
a[x] = a[x] + 5;
if (a[x] > 100) {
a[x] = 100;
}
}
System.out.println("加5分后");
for (int x = 0; x < a.length; x++) {
System.out.print(a[x] + " ");
}
}
}