1.编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值
2.package fkx; 3. 4.public class text1 { 5. 6. public static void main(String[] args) { 7. // TODO Auto-generated method stub 8.int a[]= {10,20,30,40,50}; 9.for(int i=0;i<a. length;i++) { 10. System.out.println(a[i]); 11.} 12. } 13. 14.}
2.编写一个简单程序,要求数组长度为5,动态赋值,并在控制台输出该数组的值
package fkx; import java.util.Scanner; public class text2 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("请输入五个数"); int []a= new int[5]; 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 fkx; public class text3 { public static void main(String[] args) { // TODO Auto-generated method stub String[] str={"c","h","i","n","a"}; for (int i=0;i< str.length;i++) { System.out.println(str[i]); } } }
4.输入5个学生成绩,求总分和平均分
package fkx; import java.util.Scanner; public class text4 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); double avg = 0; double sum = 0; double [] a = new double[5]; System.out.println("请输入5个学生的成绩"); for(int i=0;i<a.length;i++) { a[i]= input.nextDouble(); sum +=a[i]; avg =sum/5; } System.out.println(sum); System.out.println(avg); } }
5.定义数组{12,53,23,44,53} 用for和foreach分别输出,再倒序输出
package fkx; public class text5 { 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]+" "); } } }
6定义一个整型数组,赋值(动态静态都可以)后求出奇数个数和偶数个数
package text; import java.util.Scanner; public class abc { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); int []a=new int[5]; System.out.println("请输入数"); for(int i=0;i<a.length;i++) { a[i]=in.nextInt(); } int m=0,n=0; for(int b=0;b<a.length;b++) { if(a[b]%2==0) { m++; } else { n++; } } System.out.println("奇数为"+n+"个,偶数为"+m+"个"); } }
7.生成一个100长度数组,里面的数分别是1-100,并输出
package text; import java.util.Scanner; public class abc { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); int []a=new int[100]; System.out.println("请输入数"); for(int i=0;i<a.length;i++) { a[i]=i+1; System.out.println(a[i]); } } }
8.定义一个double数组,存放10个学生的成绩,给所有同学加5分,不能超过100分
package text; import java.util.Scanner; public class abc { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); double []a=new double[10]; System.out.println("请输入成绩"); for(int i=0;i<a.length;i++) { a[i]=in.nextDouble(); } System.out.println("加分后为"); for(int m=0;m<a.length;m++) { if(a[m]<100-5) { a[m]=a[m]+5; } else { a[m]=100; } System.out.println(a[m]); } } }
标签:java,Scanner,int,System,new,第六次,public,String From: https://www.cnblogs.com/zhaorui123456789/p/17382911.html