java黑马学习笔记

数组

变量存在栈中,变量值存放在堆中。

数组反转

在这里插入图片描述

	public class test{
	public static void main(String[] args){
	//目标:完成数组反转
	int[] arr = {10,20,30,40,50};
	for (int i = 0,j = arr.length - 1;i < j;i++,j--){
	int tep = arr[j]; //后一个值赋给临时变量
	arr[j] = arr[i];//前一个值赋给后一个值
	arr[i] = tep;//临时变量的值赋给前一个值
}
	for (int i = 0;i < arr.length;i++){
		System.out.print(arr[i] + "  " );
}
}
}

随机排名

![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/cb370ad452dd491e98561e44464d763b.png

public class speedkeydemo {
    public static void main(String[] args) {
        //目标:完成随机排名
        int[] codes = new int[5];
        Scanner sc = new Scanner(System.in);  //定义键盘输入 扫描器
        for (int i = 0; i < codes.length; i++) {
            System.out.println("请输入当前第" + (i + 1) + "个员工的工号:");
            int code = sc.nextInt();
            codes[i] = code; //对数组赋值
        }
        Random r = new Random(); //定义随机数
        for (int i = 0;i < codes.length;i ++){
        int index = r.nextInt(codes.length); //返回0 -4 下标
        int temp = codes[index]; //定义随机变量交换
        codes[index] = codes[i];
        codes[i] = temp;//按照随机数打乱数组数据
        }

        for (int i = 0; i < codes.length ; i++) {
            System.out.println(codes[i]+"");
        }
    }
}

Java方法

方法是一种语法结构,把一段代码封装成功能,重复调用,在类中是方法,在类外是函数

修饰符 返回值类型 方法名 (形参列表){
方法体代码(需要执行的功能代码)
return 返回值;
}

public class MethodDemo1 {
    public static void main(String[] args) {
        int rs = sum(10,20);
        System.out.println("和是"+ rs );


        int a1 = 20;
        int b1 = 30;
        int c1 = a1 + b1;
        System.out.println("和是" + c1 );
    }

    public static int sum(int a,int b){
        int c = a + b;
        return c;
    }
}
  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今晚梦里见i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值