A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Zengpenh 中级黑马   /  2018-1-17 01:10  /  1309 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

学习经历:
感觉今天啥也没干!只有一堆代码

/**
* 随机产生5个范围是[1,100]的整数,把这些数字中所有个位或十位为3的数字打印出来
*/
/**定义一个方法,求出给定的数字在给定int型数组中出现的次数,如果一次没有出现则返回0。
如:给定数字3 求出3在数组 int[] arr = {3,4,3,5,7,9};中出现的次数

* */
public class Day06Test02 {

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                 int[] arr = {3,4,3,5,7,9};
                int ciShu=ci(arr, 7);
                System.out.println(ciShu);
        }
        public static int ci(int[] arr,int key){
                int count=0;
                for(int x=0;x<arr.length;x++){
                        if(arr[x]==key){
                                count++;
                        }
                }
                return count;
        }

}





public class Day06Test01 {

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                Random random = new Random();
                int num1 = random.nextInt(100) + 1;
                int num2 = random.nextInt(100) + 1;
                int num3 = random.nextInt(100) + 1;
                int num4 = random.nextInt(100) + 1;
                int num5 = random.nextInt(100) + 1;
                int[] arr = { num1, num2, num3, num4, num5 };
                int count=0;
                for (int x = 0; x < arr.length; x++) {
                        if (arr[x] % 10 == 3 || arr[x] / 10 == 3) {       
                                System.out.print(arr[x]+" ");       
                        } else{
                                count++;
                        }       
                }
                if(count==arr.length){
                        System.out.println("这些数个位或十位都没有3");
                }
        }
}




/**定义一个方法,查找指定数字在数组中出现的位置(若出现多次,多次打印)
如:         数组[1232]要查找的数是2 则方法内部会打印索引值 1 ,3
        数组[1232] 要查找的数是5 则方法内部会打印 “数组中没有这个数字”
1.创建一个测试类,在测试类中书写上述代码
2.定义方法可以接受一个int[] brr和一个int key
3.在方法中定义标记 int count = 0 ;
4.方法中遍历brr 对每个元素进行判断是否等于key,如果等于就打印索引值,并count++
5.遍历brr完毕之后 判断count的值,如果还为默认值0,则表示brr中没有key,直接打印”数组中没有这个数字”
6.在主方法中传入数组 arr 和数字 2 或者5 进行测试

* */
public class Day06Test03 {

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                int[] arr={1,2,3,4,5,6,7,2,2,4,3,2,5,1,7};
                location(arr, 5);
        }
        public static void location(int[] arr,int key){
                int count=0;
                for(int a=0;a<arr.length;a++){
                        if(arr[a]==key){
                                System.out.print(a+" ");
                                count++;
                        }
                }
                if(count==0){
                        System.out.println("数组中没有这个数字");
                }
        }       
}



/**定义一个方法,实现同时求出两个整数的加、减、乘、除的结果,并同时把这个四个结果返回(把四个数放入到一个数组中并返回)
* */
public class Day06Test04 {

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                int[] arr=array(17, 3);
                System.out.println(Arrays.toString(arr));
        }
        public static int[] array(int num1,int num2){
                int add=num1+num2;
                int jian=num1-num2;
                int cheng=num1*num2;
                int chu=(int)(num1/num2);
                int[] arr={add,jian,cheng,chu};
                return arr;
        }

}


/**分析以下需求,并用代码实现
        1.键盘录入6个int类型的数据存数数组arr中
        2.将arr数组中的内容反转
        3.最后将数组最后一个角标为奇数的元素 和数组中第一个角标为奇数的元素交换
        4打印最终的数组(实现了1-4步之后的数组)

* */
public class Day06Test05 {

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                int[] arr=memory();                System.out.println(Arrays.toString(arr));
                reverse(arr);        System.out.println(Arrays.toString(arr));
                change(arr);
               
                print(arr);
               
        }
        //键盘录入6个int类型的数据存数数组arr中
        public static int[]  memory(){
                Scanner scanner=new Scanner(System.in);
                int[] arr=new int[6];
                for(int x=0;x<arr.length;x++){
                        System.out.print("请输入第"+(x+1)+"个数:");
                        int number=scanner.nextInt();
                        arr[x]=number;
                }
                return arr;
        }
        //将arr数组中的内容反转
        public static void reverse(int[] arr){
                for(int x=arr.length-1,y=0;x>y;x--,y++){
                        int temp=arr[y];
                        arr[y]=arr[x];
                        arr[x]=temp;
                }       
        }
        //最后将数组最后一个角标为奇数的元素 和数组中第一个角标为奇数的元素交换
        public static void change(int[] arr) {
                for (int x = 0; x < arr.length; x++) {
                        if (arr[x] % 2 != 0) {
                                for (int y = arr.length - 1; y >= 0; y--) {
                                        if (arr[y] % 2 != 0) {
                                                int temp = arr[x];
                                                arr[x] = arr[y];
                                                arr[y] = temp;
                                                return;
                                        } else {
                                                continue;
                                        }
                                }
                        } else {
                                continue;
                        }
                }
               
               
        }
        //4打印最终的数组(实现了1-4步之后的数组)
        public static void print(int[] arr){
                System.out.println(Arrays.toString(arr));
        }

}



4 个回复

倒序浏览
一大堆代码~~~~
回复 使用道具 举报
能敲出来就很不错啦,继续努力哦
回复 使用道具 举报
加油!!!!
回复 使用道具 举报
期待你每天的进步   温故而知新
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马