Java第八次
作者:互联网
1.编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)[必做题]?
import java.util.Scanner; public class h331 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); int[]a=new int[5]; System.out.println("请输入五个数"); 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]); } }
2. 将一个字符数组的值(neusof)拷贝到另一个字符数组中。(知识点:数组复制) [必做题]?
import java.util.Iterator; import java.util.Scanner; public class h331 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub char[] a = { 'n', 'e', 'u', 's', 'o', 'f' }; char[] b = new char[a.length]; System.arraycopy(a, 0, b, 0, a.length); for (char i : b) System.out.println(i); } }
3. 给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。(知识点:Arrays.sort排序、冒泡排序)
import java.util.Arrays; import java.util.Iterator; import java.util.Scanner; public class h331 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[] a = {1,6,2,3,9,4,5,7,8}; Arrays.sort(a); for(int i:a) System.out.println(i); } }
import java.util.Arrays; import java.util.Iterator; import java.util.Scanner; public class h331 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[] a = { 1, 6, 2, 3, 9, 4, 5, 7, 8 }; for (int i = 0; i < a.length - 1; i++) { for (int j = 0; j < a.length - 1 - i; j++) { if (a[j] > a[j + 1]) { int temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } for (int x : a) System.out.println(x); } }
4. 输出一个double型二维数组(长度分别为5、4,值自己设定)的值。(知识点:数组定义和创建、多维数组初始化、数组遍历)
import java.util.Arrays; import java.util.Iterator; import java.util.Scanner; public class h331 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[][] = new int[5][4]; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { a[i][j] = j; } } for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j] + " "); } System.out.println(); } } }
5.在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。(知识点:数组遍历、数组元素访问) [必做题]?
import java.util.Arrays; import java.util.Iterator; import java.util.Scanner; public class h331 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[]={18,25,7,36,13,2,89,63}; int max=0; for (int i = 0; i < a.length; i++) { if(i==0) max=a[i]; if(a[i]>max) max=a[i]; } System.out.println("数组中最大数为"+max); for (int i = 0; i < a.length; i++) { if(a[i]==max) System.out.println("最大数下标为a["+i+"]"); } } }
6. 将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问)
import java.util.Arrays; import java.util.Iterator; import java.util.Scanner; public class h331 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; for (int i = 7; i >= 0; i--) { System.out.println(a[i]); } } }
7. 将一个数组中的重复元素保留一个其他的清零。(知识点:数组遍历、数组元素访问)
public class h332 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[]={1,2,3,4,5,5,3,6}; for (int i = 0; i < a.length; i++) { for (int j = i + 1; j < a.length; j++) if (a[i] == a[j]){ a[j] = 0; } } for (int i : a) { System.out.println(i); } } }
8.给定一维数组{ -10,2,3,246,-100,0,5},计算出数组中的平均值、最大值、最小值。(知识点:数组遍历、数组元素访问)
import java.util.Arrays; public class h332 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[]={-10,2,3,246,-100,5}; int max,min,sum=0; Arrays.sort(a); int []y=new int[a.length]; System.arraycopy(a, 0, y, 0, a.length); for(int i:y){ System.out.println(i); } for (int i = 0; i < y.length; i++) { sum+=y[i]; } System.out.println("______________"); System.out.println("最大值为max="+y[5]); System.out.println("最小值为min="+y[0]); System.out.println("平均值为"+(double)(sum/6)); } }
9、使用数组存放裴波那契数列的前20项 ,并输出 1 1 2 3 5 8 13 21
import java.util.Arrays; public class h332 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[]=new int[20]; for (int i = 0; i < a.length; i++) { if(i==0||i==1) a[i]=1; else a[i]=a[i-2]+a[i-1]; } for(int i:a) System.out.println(i); } }
10、生成一个长度为10的随机整数数组(每个数都是0-100之间),输出,排序后,再输出
import java.util.Arrays; import java.util.Random; public class h332 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[]=new int[10]; Random d=new Random(); System.out.println("随机生成数组为"); for (int i = 0; i < a.length; i++) { a[i] = d.nextInt(101); } for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } System.out.println("数组排序后为"); Arrays.sort(a); for(int j:a) System.out.println(j); } }
标签:Java,int,args,System,第八次,util,import,public 来源: https://www.cnblogs.com/hyp33/p/14705443.html