java第八次作业
第一题 编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)[必做题]?
package kg; public class test1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[]={10,20,30,40,50}; for (int i : a) { System.out.println(i); } } }
第二题 将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。(知识点:数组复制) [必做题]?
package kg; public class test2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub char []a={'n','e','u','s','o','f','t','e'}; char []b=new char[a.length]; System.arraycopy(a, 0, b, 0,a.length); for (char c : b) { System.out.print(c); } } }
第三题 给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。(知识点:Arrays.sort排序、冒泡排序)
import java.util.Arrays; public class test3 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int m=0; int a[]={1,6,2,3,9,4,5,7,8}; Arrays.sort(a); for (int i : a) { System.out.print(i); } System.out.println(); for(int b=8;b>=0;b--){ for (int c = 0; c <b; c++) { if(a[c]>a[c+1]){ m=a[c+1]; a[c+1]=a[c]; a[c]=m; } } } for (int y : a) { System.out.print(y); } System.out.println(); } }
第四题 输出一个double型二维数组(长度分别为5、4,值自己设定)的值。(知识点:数组定义和创建、多维数组初始化、数组遍历)
public class test4 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub double a[][]=new double[5][4]; for (int b = 0; b <5; b++) { for (int i = 0; i < 4; i++) { if(i<=b) a[b][i]=b; } } for (int c = 0; c <5; c++) { for (int d = 0; d <4; d++) { System.out.print(a[c][d]+" "); } System.out.println(); } }
第五题 在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。(知识点:数组遍历、数组元素访问) [必做题]?
public class test5 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int m=0; int x=0; int a[]={18,25,7,36,13,2,89,63}; for(int b=0;b<7;b++){ if(a[b]>a[b+1]){ x=a[b]; } } for (int i : a) { if(i!=x) m++; if(i==x){ m++; break; } } System.out.println("最大的数是"+x+"它排在第"+m+"个"); } }
作业
第一题 将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问)
import java.util.Scanner; public class work { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("请输入十个数字"); int []a=new int[10]; for(int b=0;b<10;b++){ a[b]=input.nextInt(); } System.out.print("输入的数字顺序为"); for (int m : a) { System.out.print(m); } System.out.println(); int c=0; for(int n=9;n>=0;n--){ for(int d=0;d<n;d++){ c=a[d]; a[d]=a[d+1]; a[d+1]=c; } } System.out.print("变化后为"); for (int i : a) { System.out.print(i); } } }
第二题 将一个数组中的重复元素保留一个其他的清零。(知识点:数组遍历、数组元素访问)
import java.util.Scanner; public class work2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("请输入十个数字"); int []a=new int[10]; for(int b=0;b<10;b++){ a[b]=input.nextInt(); } for(int c=0;c<10;c++){ for (int i = c+1; i < 10; i++) { if(a[c]==a[i]) a[i]=0; } } for (int i : a) { System.out.print(i+" "); } } }
第三题 给定一维数组{ -10,2,3,246,-100,0,5},计算出数组中的平均值、最大值、最小值。(知识点:数组遍历、数组元素访问)
public class work3 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[]={ -10,2,3,246,-100,0,5}; int m=a[0]; int s=a[0]; int h=0; double p=0; for(int b=0;b<7;b++){ if(a[b]>m) m=a[b]; if(a[b]<s) s=a[b]; h=h+a[b]; } p=h/7; System.out.println("最大值为"+m+"最小值为"+s+"和为"+h+"平均值为"+p); } }
第四题 使用数组存放裴波那契数列的前20项 ,并输出 1 1 2 3 5 8 13 21
public class work4 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int []b=new int[20]; for (int a = 0; a <20; a++) { if(a==0||a==1) b[a]=1; else b[a]=b[a-1]+b[a-2]; } for (int i : b) { System.out.print(i+" "); if(i==21) break; } } }
第五题 生成一个长度为10的随机整数数组(每个数都是0-100之间),输出,排序后,再输出
import java.util.Random; public class work5 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int []a=new int[10]; for(int b=0;b<10;b++){ Random r=new Random(); a[b]= r.nextInt(100); } int c=0; for(int n=9;n>=0;n--){ for(int d=0;d<n;d++){ c=a[d]; a[d]=a[d+1]; a[d+1]=c; } } for (int i : a) { System.out.print(i+" "); } } }