第七次Java作业
1.编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值。
public class diyi { /** * @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,动态赋值,并在控制台输出该数组的值。
import java.util.Scanner; public class dier { /** * @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 并在控制台输出
public class disan { /** * @param args */ 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个学生成绩,求总分和平均分
import java.util.Scanner; public class disi { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int []a=new int[5]; int sum=0; 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/a.length); } }
5.定义数组{12,53,23,44,53} 用for和foreach分别输出,再倒序输出
public class diwu { /** * @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]); } System.out.println(); for (int i : a) { System.out.println(i); } System.out.println(); for(int j=a.length-1;j>=0;j--){ System.out.println(a[j]); } } }
1.定义一个整型数组,赋值后求出奇数个数和偶数个数
import java.util.Scanner; public class disi { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int []a=new int[5] ; int jishu=0,oushu=0; 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++) { if(a[i]%2==0){ oushu++; } if(a[i]%2!=0){ jishu++; } } System.out.println("奇数个数:"+jishu); System.out.println("偶数个数:"+oushu); } }
2.定义一个数组,求数组中的最大值和最小值
public class dier { /** * @param args */ 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.print(a[i]+" "); if(i%10==0){ System.out.println(); } } } }
3.定义一个double数组,存放10个学生的成绩,给所有同学加5分,不能超过100分。
import java.util.Scanner; public class tuxing { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub double[]a=new double[10] ; 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++) { if(a[i]+5<100){ a[i]+=5; } if(a[i]+5>=100){ a[i]=100; } System.out.println(a[i]); } } }