第七周上机练习+作业
上机练习
1.编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值。
package Test4; public class h1 { /** * @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 Test4; import java.util.Scanner; public class h1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[] a = new int[5]; Scanner input = new Scanner(System.in); for (int i = 0; i < a.length; i++) { a[i] = input.nextInt(); } for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } }
3.定义字符型数组,分别存储c、h、 i、n、a 并在控制台输出
package Test4; import java.util.Scanner; public class h1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String[] a = new String[5]; Scanner input = new Scanner(System.in); System.out.println("请分别输入字母:"); for (int i = 0; i < a.length; i++) { a[i] = input.next(); } for (int i = 0; i < a.length; i++) { System.out.print(a[i]); } } }
4.输入5个学生成绩,求总分和平均分
package Test4; import java.util.Scanner; public class h1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int sum = 0; int[] a = new int[5]; Scanner input = new Scanner(System.in); for (int i = 0; i < a.length; i++) { a[i] = input.nextInt(); sum += a[i]; } System.out.println("总成绩为:" + sum); System.out.println("平均成绩为:" + sum / 5); } }
5.定义数组{12,53,23,44,53} 用for和foreach分别输出,再倒序输出
package Test4; public class h1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[] = { 12, 53, 23, 44, 53 }; System.out.println("for输出为:"); for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } System.out.println("foreach输出为:"); for (int i : a) { System.out.println(i); } System.out.println("for倒叙输出为:"); for (int i = 4; i >= 0; i--) { System.out.println(a[i]); } } }
作业:
1.定义一个整型数组,赋值后求出奇数个数和偶数个数
package Test4; import java.util.Scanner; public class h1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int number1 = 0; int number2 = 0; int a[] = new int[5]; Scanner input = new Scanner(System.in); System.out.println("请输入5个数:"); for (int i = 0; i < a.length; i++) { a[i] = input.nextInt(); if (a[i] % 2 == 0) { number1++; } else { number2++; } } System.out.println("偶数个数有:" + number1); System.out.println("奇数个数有:" + number2); } }
2.定义一个数组,求数组中的最大值和最小值
package Test4; import java.util.Scanner; public class h1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int min = 0; int max = 0; int a[] = new int[5]; Scanner input = new Scanner(System.in); System.out.println("请输入5个数:"); for (int i = 0; i < a.length; i++) { a[i] = input.nextInt(); min = a[0]; max = a[0]; if (a[i] > max) { max = a[i]; } if (min > a[i]) { min = a[i]; } } System.out.println("最小值为:" + min); System.out.println("最大值为:" + max); } }
2.生成一个100长度数组,里面的数分别是1-100,并输出
package Test4; import java.util.Scanner; public class h2two { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); int a[] = new int[100]; for (int i = 0; i < a.length; i++) { a[i] = i + 1; } for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } }
3.定义一个double数组,存放10个学生的成绩,给所有同学加5分,不能超过100分。
package Test4; import java.util.Scanner; public class h3 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); double a[] = new double[10]; System.out.println("请输入10个学生成绩:"); for (int i = 0; i < a.length; i++) { a[i] = input.nextInt(); } System.out.println("将这10个学生成绩加5分后为:"); for (int i = 0; i < a.length; i++) { a[i] = a[i] + 5; if (a[i] >= 100) { a[i] = 100; } System.out.println(a[i]); } } }