栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

java笔记2.03

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java笔记2.03

数组基本操作

public class b {
    public static void main(String[] args) {
        int[] a = new int[10];
        for (int i = 0; i < 10; i++) {
            a[i] = i;
        }
        System.out.println("这个变量a存放在栈区" + a);

        System.out.println("数组才存放在堆区");
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
        System.out.println("数组引用,b等同于a");

        int b[] = a;
        for (int i = 0; i < 10; i++) {
            System.out.println(b[i]);
        }


		System.out.println("数组的初始化");
        int c[] = new int[] { 1, 2, 3, 4, 5 };
        for (int i = 0; i < 5; i++) {
            System.out.println(c[i]);
        }


		//b = null;// 释放数组
        //System.out.println(b[0]);
    }
}

数组最大值

public class b {
    public static void main(String[] args) {
        int[] a = new int[10];
        for (int i = 0; i < a.length; i++) {
            a[i] = i + 1;
        }

        int max = 0;
        for (int i = 0; i < a.length; i++) {
            max = max < a[i] ? a[i] : max;
        }
        System.out.println(max);
    }
}

方法:
也就是cpp中的函数

public class b {
    public static void main(String[] args) {
        judge(10);
        judge(1);

    }

    public static void judge(int a) {
        if (a % 2 == 0) {
            System.out.println("是偶数");
        } else {
            System.out.println("是奇数");
        }
    }
}
public class b {
    public static void main(String[] args) {

        // 非静态需要对象实例化才能调用
        b test = new b();
        test.judge(19);

        // 当然,public也就只是一个权限修饰符
        test.judge2(20);
    }

    void judge2(int a) {
        if (a % 2 == 0) {
            System.out.println("是偶数");
        } else {
            System.out.println("是奇数");
        }
    }

    public void judge(int a) {
        if (a % 2 == 0) {
            System.out.println("是偶数");
        } else {
            System.out.println("是奇数");
        }
    }
}

方法重载要求:参数不同,参数类型,参数个数,参数顺序

public class b {
    public static void main(String[] args) {
        b test = new b();

        test.function();
        test.function(1);
        test.function(1, 2);

    }

    public void function() {
        System.out.println("这是重载函数");
    }

    public void function(int a) {
        System.out.println("这是重载函数" + a);
    }

    public void function(int a, int b) {
        System.out.println("这是重载函数" + a + " " + b);
    }
}
疑问:Java中没有指针,那么如何实现引用传递?

例如
这种C++效果怎么实现?

#include
using namespace std;
void function(int * a)
{
	*a=100;
}
int main()
{
	int a=10;
	cout< 

这就很烦,很难搞,java中并不存在这种功能
要么搞成数组
要么就自己写个类
要么给他返回

这。。。。有点复杂啊

public class b {
    public static void change(int[] a) {
        a[0] = 100;
    }

    public static int change2() {
        return 999;
    }

    public static void main(String[] args) {
        // 方法1:用数组,数组是引用方式
        int[] a = new int[1];
        a[0] = 10;
        System.out.println(a[0]);
        change(a);
        System.out.println(a[0]);

        // 方法2:用函数返回
        int a2 = 10;
        System.out.println(a2);
        a2 = change2();
        System.out.println(a2);

        // 方法3:用自定义的类,类里面自己写方法,或者对类进行操作
        myint a3 = new myint();
        a3.num = 10;
        System.out.println(a3.num);
        a3.change();
        System.out.println(a3.num);


		// 写一个对类进行操作的方法,用类对方法操作
		change3(a3);
		System.out.println(a3.num);
    }
    public static void change3(myint num){
		num.num=1024;
    }

}

class myint {
    int num;

    public void change() {
        num = 99;
    }
}

案例,减肥计划

import java.util.Scanner;

public class mytest{
    public static void main(String[] args) {
        Scanner scanf=new Scanner(System.in);
        while (true) {
            int a = scanf.nextInt();

            switch (a) {
                case 1:
                    System.out.println("周一跑步");break;
                case 2:
                    System.out.println("周二游泳");break;
                case 3:
                    System.out.println("周三举重");break;
                case 4:
                    System.out.println("周四篮球");break;
                case 5:
                    System.out.println("周五自行车");break;
                case 6:
                    System.out.println("周六乒乓球");break;
                case 7:
                    System.out.println("周天射击");break;
                default:
                    System.out.println("傻x别看了,输错了");
            }
        }
    }
}

案例:逢7过

import java.util.Scanner;

public class mytest{
    public static void main(String[] args) {
        for (int i=0;i<100;i++)
        {
            if (i%10==7||i/10==7||i%7==0)
                System.out.println(i);
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/947688.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号