java学习笔记

import sun.management.Agent;

import javax.print.attribute.standard.Chromaticity;
import java.io.*;
import java.util.Locale;
import java.util.Scanner;

/**
 * 你好,世界
 *
 * @author yh
 * @date 2023/06/10
 */
public class Hello_World {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Main{" +
                "name='" + name + '\'' +
                ", id=" + id +
                '}';
    }

    private int id;

    public static void main(String[] args) {
        Hello_World main = new Hello_World();
        System.out.println("Hello world!");
        main.setName("yh");
        main.setId(1112);
        System.out.println(main.toString());

    }
}

/**
 * 打印
 *
 * @author yh
 * @date 2023/06/10
 */
class print {
    public static void main(String[] args) {
        System.out.println("电脑主机");
        System.out.println("电脑显示器");
        System.out.println("电源");
        System.out.println("\n水壶\n");
        System.out.println("\t茶叶");

    }
}

class area {

    public static void main(String[] args) {
        final double PI = 3.14;
        int r = 3;
        double area = PI * r * r;
        double circle = PI * r * 2;
        System.out.println(area + "\t" + circle);
        System.out.println(013);//11
        System.out.println(0x13);//19
        System.out.println(0b101);//5
        System.out.println(3.23e2);//323
        System.out.println(++r);
        System.out.println(r++);
        System.out.println(r);
        System.out.println(--r);
        System.out.println(r++);
        System.out.println(r);
    }
}

class test1 {
    public static void main(String[] args) {
        boolean a = false;
        boolean b = true;
        System.out.println(a & b);
        System.out.println(a | b);
        System.out.println(a && b);
        System.out.println(a || b);
    }
}

class test2 {
    public static void main(String[] args) {
        int a = 7;
        int b = 8;
        System.out.println(a & b);
        System.out.println(a | b);
        System.out.println(a ^ b);
        System.out.println(~b);
    }
}

class test3 {
    public static void main(String[] args) {
        int score = 200;
        String flag = score < 60 ? "不及格" : "及格";
        System.out.println(flag);
        int x = 0;
        int num = x > 0 ? x : (x == 0 ? 0 : -1);
        System.out.println(num);
    }
}

class test4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入你的的名字:");
        String name = sc.nextLine();
        System.out.println("hello:" + name);

    }
}

class test5 {
    public static void main(String[] args) {
        final double PI = 3.14;
        for (int i = 0; i < 10; i++) {
//            double r=Math.random()*10;
            double r = 2;
            double area = Math.PI * r * r;
            double circle = Math.PI * r * 2;
//            System.out.println(area+"\t"+circle);
//            String s = area >= circle ? (area == circle ? "面积等于周长" : "面积大于周长") : "面积小于周长";
//            System.out.println(s);
            System.out.println(area >= circle ? (area == circle ? "面积等于周长" : "面积大于周长") : "面积小于周长");
        }
    }
}

class test6 {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            int age = (int) (Math.random() * 100);
//            System.out.println(age);
            if (age <= 18) {
                System.out.println("青年");
            } else if (age > 18 && age <= 60) {
                System.out.println("中年");
            } else {
                System.out.println("老年");
            }
        }
    }
}

class test7 {
    public static void main(String[] args) {
        int age = (int) (Math.random() * 10);
        switch (age) {
            case 1:
                System.out.println(1);
                break;
            case 2:
                System.out.println(2);
                break;
            case 3:
                System.out.println(3);
                break;
            case 4:
                System.out.println(4);
                break;
            case 5:
                System.out.println(5);
                break;
            case 6:
                System.out.println(6);
                break;
            case 7:
                System.out.println(7);
                break;
            case 8:
                System.out.println(8);
                break;
            case 9:
                System.out.println(9);
                break;
            case 10:
                System.out.println(10);
                break;

        }
    }
}

class test8 {
    public static void main(String[] args) {
        for (int i = 0; i < 12; i++) {
            int month = (int) (Math.random() * 12);
            switch (month) {
                case 1:
                case 2:
                case 3:
                    System.out.println("春天"+month);
                    break;
                case 4:
                case 5:
                case 6:
                    System.out.println("夏天"+month);
                    break;
                case 7:
                case 8:
                case 9:
                    System.out.println("秋天"+month);
                    break;
                case 10:
                case 11:
                case 12:
                    System.out.println("冬天"+month);
                    break;

            }
        }
    }
}
class test9{
    public static void main(String[] args) {
        int i=1;
        while (i <= 5) {
            System.out.println("I Love You Java:"+i);
            i++;
        }

    }
}
class test10{
    public static void main(String[] args) {
        int sum=0;
        for (int i = 0; i <= 100; i++) {
            sum+=i;
        }
        System.out.println(sum);
    }
}
class test11{
    public static void main(String[] args) {
        int i=0;
        do{
            System.out.println(i);
            i++;
            System.out.println(i);
        }while(i<1);
    }
}
class test12{
    public static void main(String[] args) {
        for (int i = 1; i <=120; i++) {
            System.out.print(i+"\t");
            if(i%5==0&&i!=0){
                System.out.print("\n");
            }

        }
    }
}
class test13{
    public static void main(String[] args) {
        for (int i = 100; i >=0; i--) {
            System.out.print(i+"\t");
            if(i%5==0){
                System.out.println();
            }

        }
    }
}
class test14{
    public static void main(String[] args) {
        for (int i = 0; i < 100; i+=10) {
            System.out.println(i);
        }
    }
}
class test15{
    public static void main(String[] args) {
        char a='a';
        char z='z';
        int a1=(int)a;
        int z1=(int)z;
        for (int i = a1; i <=z1 ; i++) {
            System.out.println((char)i);

        }

    }
}
class test16{
    public static void main(String[] args) {
        int a=(int)(char)'a';
        for (int i = a; i <a+26 ; i++) {
            System.out.print((char)i+"  ");

        }
    }
}
class test17{
    public static void main(String[] args) {

    }
}
class test18{
    public static void main(String[] args) {
        for (int i = 1; i <=10; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j+"*"+i+"="+i*j+"\t");
            }
            System.out.println();
        }
    }
}
class test19{
    public static void main(String[] args) {
        int flag= (int) Math.round((Math.random()*100));
        while (flag!=88){
            System.out.print(flag+"  ");
        flag= (int) Math.round((Math.random()*100));

        }
        System.out.println(flag);

    }
}
class  test20{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入一个数计算它是否为素数:");
        int num = sc.nextInt();
        String result = "";
        if (num <= 1) {
            result = "不是素数";
        } else {
            boolean isPrime = true;
            for (int i = 2; i <= Math.sqrt(num); i++) {
                if (num % i == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                result = "是素数:" + num;
            } else {
                result = "不是素数:" + num;
            }
        }
        System.out.println(result);
    }
}
class test21{
    public static void main(String[] args) {
        String str="HELLO java";
        System.out.println(str.toLowerCase());
        System.out.println(str.toUpperCase());
        System.out.println(str.toCharArray());
        System.out.println(str.contentEquals("hello"));
    }
}
class test22{

    public int add(int a ,int b){
        return a+b;
    }
    public double add (double a,double  b){
        return a+b;
    }
    public double add (double a,double b ,double c){
        return a+b+c;
    }
    public static void main(String[] args) {
        test22 test22 = new test22();
        System.out.println(test22.add(1, 2, 3));
        System.out.println(test22.add(1, 2));
        System.out.println(test22.add(1.2, 2.3));
    }
}
class test23{
    public static long fact(int a){
        if(a==1)return 1;
        else {return fact(a-1)*a;}
    }
    public static void main(String[] args) {
        System.out.println(fact(10));
    }
}
class test24{
    test24(){
        System.out.println("hello");
    }

    public static void main(String[] args) {
        System.out.println(new test24());
    }
}
class test25{
    int id ;
    String name;
    String phone;

    public test25(int id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "test25{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }

    public static void main(String[] args) {
        System.out.println(new test25(1, "yan","123"));
    }
}
class th1 extends Thread{
    @Override
    public void run(){
        for (int i = 0; i < 10; i++) {
            System.out.println(this.currentThread().getName()+"\t"+i);
        }
    }

    public static void main(String[] args) {
        // 调用线程
        th1 th1 = new th1();
        th1 th2 = new th1();
        th1.start();
        th2.start();
    }
}
class th2 implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName()+"\t"+i);
        }
    }

    public static void main(String[] args) {
        Thread th1 = new Thread(new th2(), "th3");
        Thread th2 = new Thread(new th2(), "th4");
        th1.start();
        th2.start();
    }
}

class file1{
    public static void main(String[] args) throws IOException {
        File file = new File("a.txt");
        //打开文件
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        fileOutputStream.write(65);
        fileOutputStream.close();
//        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
//        bufferedOutputStream.write(110);
//        bufferedOutputStream.write(120);
        bufferedOutputStream.flush();//flush方法用于强制将缓存中内容输出,缓存输出流会将输出的内容缓存在内存中,如果缓存区已满,那么缓存会自动将数据输出到目的地。如果缓存区数据未满,则需要调用flush方法,将缓存区数据输出到目的地。如果忘记调用flush方法,则不会将内容输出到目的地。
调用缓存输出流的close方法时会自动执行flush方法。
//        bufferedOutputStream.close();
//        System.out.println("file.getName() = " + file.getName());
//        System.out.println("file.getParent() = " + file.getParent());
//        System.out.println("file.getAbsoluteFile() = " + file.getAbsoluteFile());
//        System.out.println("file.exists() = " + file.exists());
//        System.out.println("file.canRead() = " + file.canRead());
//        System.out.println("file.canWrite() = " + file.canWrite());
        new Thread();


    }
}


class  ExA {  
     static  {  
         System.out.println( "父类--静态代码块" );  
     }  
   
     public  ExA() {  
         System.out.println( "父类--构造函数" );  
     }  
   
     {  
         System.out.println( "父类--非静态代码块" );  
     }  
   
     public  static  void  main(String[] args) {  
         new  ExB();  
     }  
}  
   
class  ExB  extends  ExA {  
     static  {  
         System.out.println( "子类--静态代码块" );  
     }  
     {  
         System.out.println( "子类--非静态代码块" );  
     }  
   
     public  ExB() {  
         System.out.println( "子类--构造函数" );  
     }  
}  
 
执行结果 
===== 
父类--静态代码块 
子类--静态代码块 
父类--非静态代码块 
父类--构造函数 
子类--非静态代码块 
子类--构造函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值