前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java入门代码练习与记录-1

Java入门代码练习与记录-1

作者头像
Tommonkey
发布2023-03-20 10:53:29
1860
发布2023-03-20 10:53:29
举报
文章被收录于专栏:TommonkeyTommonkey

珍惜大学没压力的时光,一定要刻苦学习啊!

大学没好好学JAVA,工作后发现JAVA的重要性,这是我在工作之余,重新学习java时手敲的代码练习,这里做下记录。

代码语言:javascript
复制
// 入门Java代码练习与记录
// author: tommonkey

// if...else if的使用
/*
public class test {
    public static void main(String[] args){
        int year = 2001;
        if(year==2000)
            System.out.println("year is :"+year);
        else if (year==2001)
            System.out.println("over!");
    }
}
 */


// switch...case的使用
/*
public class test {
    public static void main(String[] args){
        int month = 8;
        switch(month)
        {
            case 1:System.out.println("1");break;
            case 2:System.out.println("2");break;
            case 3:System.out.println("3");break;
            case 8:System.out.println("8");break;
        }
    }
}
 */


// while循环的使用
/*
public class test {
    public static void main(String[] args){
        int i = 1;
        long sun = 0;
        while (i<100){
            sun+=i;
            i++;
        }
        System.out.println(sun);
    }
}
 */


// for循环的使用
/*
public class test {
    public static void main(String[] args){
        int sum = 0;
        for (int i=1;i<100;i++){
            sum+=i;
        }
        System.out.println(sum);
    }
}
 */


// do...while循环的使用
/*
public class test {
    public static void main(String[] agrs){
        int i = 1;
        do {
            i++;
        }while (i<100);
        System.out.println(i);
    }
}
 */


//方法的重载实列
/*
public class test {
    public static void main(String[] args) {
        int n1 = 5, n2 = 10;
        char c1 = 'a', c2 = 'b';
        float f1 = 0.5f, f2 = 0.1f;
        System.out.println(add(n1, n2));
        System.out.println(add(c1, c2));
        System.out.println((add(f1, f2)));
    }

    static int add(int a,int b){
        return a+b;
    }

    static double add(double a,double b){
        return a+b;
    }
}
 */


// 对象数组:对象可为数组的元素
/*
class A{
    int a,b;
    // 两个构析方法
    A(){
        a=0;b=0;
    }
    A(int i,int m){
        a=i;b=m;
    }
    void setA(int i,int m){
        a=i;b=m;
    }
}

class test{
    public static void main(String[] args){
        A[] a=new A[3];     // 创建对象数组,元素数量为3个,每个元素都是A的对象
        a[0]=new A();       // 初始化数组
        a[1]=new A(1,2);
        a[2]=new A();
        System.out.println(a[0].a+a[0].b);
        System.out.println(a[1].a+a[1].b);
        a[2].setA(10,11);
        System.out.println(a[2].a+a[2].b);
    }
}
 */


// 对象作为方法的参数
/*
class A{
    int a=10;
    void method1(A a){      //这里传入一个A类的一个对象进去
        System.out.println((a.a));
        a.a+=5;
        System.out.println(a.a);
    }
}

class test{
    public static void main(String[] args){
        A ao=new A();       // 这里创建了一个A类的一个对象
        System.out.println(ao.a);
        ao.method1(ao);     // 传入
        System.out.println(ao.a);
    }
}
 */


// 对象作为方法的返回值
/*
class A{
    int a;
    A(int i){
        a=i;
    }
    A method1(){        // 类名+方法名():意思是这个方法的返回类型是某个类的实例
        A a1=new A(a=10);
        return a1;      // 返回A类的对象:a1
    }
}

class test{
    public static void main(String[] args){
        A a2=new A(1);
        A a3;
        System.out.println(a2.a);
        a3=a2.method1();
        System.out.println(a3.a);
        a3=a3.method1();
        System.out.println(a3.a);
    }
}
 */


//  接口&实现堆栈和队列
/*
interface Access{
    void put(char ch);
    char get();
}

// 栈:
class Stack implements Access{
    private char[] array=new char[80];
    private int top=0;

    public void put(char ch){
        array[top++]=ch;
    }

    public char get(){
        return array[--top];
    }
}

// 队列:
class Quene implements Access{
    private char[] array=new char[80];
    private int readin=0,readout=0;

    public void put(char ch){
        array[readin++]=ch;
    }
    public char get(){
        return array[readout++];
    }
}

class test{
    public static void main(String[] args){
        Stack s=new Stack();
        s.put('1');
        s.put('2');
        s.put('3');
        s.put('4');
        s.put('5');
        System.out.println(s.get());
        System.out.println(s.get());
        System.out.println(s.get());
        System.out.println(s.get());
        System.out.println(s.get());

        Quene q=new Quene();
        q.put('1');
        q.put('2');
        q.put('3');
        q.put('4');
        q.put('5');
        System.out.println(q.get());
        System.out.println(q.get());
        System.out.println(q.get());
        System.out.println(q.get());
        System.out.println(q.get());
    }
}
 */


// 字符串常量的常用操作,String类,该类创建的对象是不允许改变的,创建:String s = new String("1111");
// StringBuffer是创建字符串变量!
/*
class test{
    public static void main(String[] args){

        String a = new String("world!");
        System.out.println(a);

        String s = "what's your name?";
        System.out.println("字符串长度为:"+s.length());
        System.out.println("字符串中是否含有y:"+s.indexOf('y'));
        System.out.println("从第五位开始向后查询,是否存在y:"+s.indexOf('y',5));
        System.out.println("是否存在name:"+s.indexOf("come"));
        System.out.println("是否相等:"+s.equals("what's wrong!"));
        System.out.println("是否相等:"+s.equalsIgnoreCase("what's wrong!"));
        System.out.println("截取字符串:"+s.substring(0,5));
        System.out.println("截取指定位置后面的所有字符串:"+s.substring(6));
        System.out.println("拼接字符串:"+s.concat("babiq!"));
        System.out.println(s.charAt(5));
        System.out.println("字符串是否以what开头:"+s.startsWith("what"));
    }
}
 */


// StringBuffer类的一些方法练习
// toString():将字符串变量转化为字符串常量,所有再输出前要提前使用此方法转换
class test{
    public static void main(String[] args){
        String str="beautiful";
        StringBuffer s1=new StringBuffer();
        StringBuffer s2=new StringBuffer(str);

        System.out.println("s1的长度:"+s1.length());
        System.out.println("s1的可容纳字数:"+s1.capacity());
        System.out.println("字符串变量转变为字符串常量:/"+s1.toString());
        System.out.println("s2可容纳字符长度:"+s2.length());
        s2.setLength(10);
        System.out.println("设置s2长度后,现在可容纳长度数:"+s2.length());
        System.out.println("变量转常量后打印输出:"+s2.toString());
        System.out.println("输出指定位置字符串:"+s2.charAt(5));
        s2.setCharAt(5,'A');  // 替换指定位置的字符
        System.out.println("输出修改后的字符串:"+s2.toString());
        s2.append("abcdef");
        System.out.println(s2.toString());
        s2.insert(5,'B');
        System.out.println("插入后的s2:"+s2.toString());
    }
}    
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-02-232,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 珍惜大学没压力的时光,一定要刻苦学习啊!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档