java学习记录day2

1.class关键字

public公共
对方法的封装,有private,protected,public三种访问权限
static静态
静态变量和方法,可以直接通过类名调用,而不需要创建对象,不属于对象
栈、堆、静态方法区(静态属性和方法,只加载一次,属于类)
通过堆内存和静态方法区实现静态变量和方法的共享
成员属性和成员方法属于对象,对象创建后,才会占用堆内存
void无返回值
main主函数
String字符串 String[]字符串数组
System系统类

2.实例化对象或创建对象

类名 对象名 = new 类名(参数);
静态方法调用示例
    class Hello{
        public static void sayHello(){
            System.out.println("Hello World!");
        }
        public static void main(String[] args){
            System.out.println("main方法");
            Hello.sayHello();
        }
    }

3.数据类型

基本数据类型:byte(范围-128~127) short int long float double boolean char

包装类:Byte Short Integer Long Float Double Boolean Character

基本数据类型和包装类之间的转换:自动类型转换

任务:获取八种数据类型的范围(通过包装类)

public class DataTypeRanges {
    public static void main(String[] args) {
        // byte
        System.out.println("byte Min Value: " + Byte.MIN_VALUE);
        System.out.println("byte Max Value: " + Byte.MAX_VALUE);
        // short
        System.out.println("short Min Value: " + Short.MIN_VALUE);
        System.out.println("short Max Value: " + Short.MAX_VALUE);
        // int
        System.out.println("int Min Value: " + Integer.MIN_VALUE);
        System.out.println("int Max Value: " + Integer.MAX_VALUE);
        // long
        System.out.println("long Min Value: " + Long.MIN_VALUE);
        System.out.println("long Max Value: " + Long.MAX_VALUE);
        // float
        System.out.println("float Min Value: " + Float.MIN_VALUE);
        System.out.println("float Max Value: " + Float.MAX_VALUE);
        // double
        System.out.println("double Min Value: " + Double.MIN_VALUE);
        System.out.println("double Max Value: " + Double.MAX_VALUE);
        // char (cast to int to print as numbers)
        System.out.println("char Min Value: " + (int) Character.MIN_VALUE);
        System.out.println("char Max Value: " + (int) Character.MAX_VALUE);
        // boolean (no specific range constants)
        System.out.println("boolean values: true or false");
    }
}

引用数据类型:类、接口、数组

字符串 数组 对象

示例

class Hello{
    public static void main(String[] args){
        byte a=1;
        byte b=2;
        byte c=a+b;
        System.out.println(c);
    }

以上代码会报错,因为byte类型不能进行运算,需要使用int类型进行运算;同时,byte类型变量的范围是-128~127,所以java对byte类型变量进行运算时,会自动转换为int类型,byte+byte返回值默认是int类型。应该纠正为:

byte c=(byte)(a+b);

或者

int c=a+b;

4.java基本语法

if else

int a=10;
if(a<15){
    //执行的代码
    }
else if(a<20){
    //执行的代码
    }
else{   
    //执行的代码
}

for循环

for(int i=0;i<10;i++){
    //执行的代码
}

while循环

int i=0;
while(i<10){
    //执行的代码
    i++;
}

do-while循环

int i=0;
do{
    //执行的代码
    i++;
}while(i<10);

switch语句

int a=10;
switch(a){
    case 10:
        //执行的代码
        break;
    case 20:
        //执行的代码
        break;
    default:
        //执行的代码
}

练习

输入三个数,判断是否是三角形

public class Triangle {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入三角形的三条边长:" );
        int a = in.nextInt();
        int b = in.nextInt();
        int c = in.nextInt();
        if (a + b > c || b + c > a || c + a > b) {
            System.out.println("这是一个三角形");
            if(a*a + b*b == c*c || b*b + c*c == a*a || c*c + a*a == b*b){
                System.out.println("这是一个直角三角形");
            }
            else if(a==b && b==c){
                System.out.println("这是一个等边三角形");
            }
            else if(a==b || b==c || c==a){
                System.out.println("这是一个等腰三角形");
            }
        } else {
            System.out.println("这不是一个三角形");
        }
    }

荷叶问题

public class Food {
        public static void main(String[] args) {
            int[] foodnumber = new int[10];
            foodnumber[9] = 1;
            for(int i=1;i<10;i++){
                foodnumber[10-i-1] = (foodnumber[10-i]+1)*2;
                System.out.println("第"+(10-i)+"天剩下"+foodnumber[10-i-1]+"个馒头");
            }
            int foodsum = (foodnumber[0]+1)*2;
            System.out.println("原来共"+foodsum+"个馒头");
        }
}

位运算

十进制转化成八进制、二进制、十六进制

import java.util.Scanner;

public class DecimalToBinary {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入一个十进制数:"); // 十进制数:10
        int decimal = in.nextInt();
        boolean flag=true;
        while(flag){
            System.out.println("请选择需要转化的进制(1for八进制,2for二进制,3for十六进制,0for退出):" );
            int choice = in.nextInt();
            if (choice == 1) {
                System.out.println("八进制数:" + toOctal(decimal)); // 八进制数:12
            } else if (choice == 2) {
                System.out.println("二进制数:" + toBinary(decimal)); // 二进制数:1010
            } else if (choice == 3) {
                System.out.println("十六进制数:" + toHex(decimal)); // 十六进制数:a
            } else {
                System.out.println("退出程序");
                flag=false;
            }
        }
    }
    public static String toOctal(int decimal) {
        StringBuilder result = new StringBuilder();
        while (decimal > 0) {
            int remainder = decimal & 7; // 取最低三位,即对8取余
            result.insert(0, remainder);
            decimal >>= 3; // 右移3位,相当于除以8
        }
        return result.toString();
    }

    public static String toBinary(int decimal) {
        StringBuilder result = new StringBuilder();
        while (decimal > 0) {
            int remainder = decimal & 1; // 取最低位,即对2取余
            result.insert(0, remainder);
            decimal >>= 1; // 右移1位,相当于除以2
        }
        return result.toString();
    }

    public static String toHex(int decimal) {
        StringBuilder result = new StringBuilder();
        while (decimal > 0) {
            int remainder = decimal & 15; // 取最低四位,即对16取余
            if (remainder < 10) {
                result.insert(0, remainder);
            } else {
                result.insert(0, (char) ('a' + (remainder - 10)));
            }
            decimal >>= 4; // 右移4位,相当于除以16
        }
        return result.toString();
    }
}

5.集成开发环境

下载开发工具:IntelliJ IDEA Community Edition
IDEA、Eclipse、NetBeans
创建project,module,package
idea快捷键:
psvm创建主函数;sout输出

6.java数组

类名称 数组名称[] = new 类名称[数组长度];
或者 类名称[] 数组名称 = new 类名称[]{值1,值2,值3};

练习

 import java.util.Scanner;

public class Array {
    public static void main(String[] args) {
        System.out.printf("请输入数组个数:");
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        System.out.println("请输入数组的"+num+"个值");
        int[] arr = new int[num];
        for (int i = 0; i < num; i++) {
            arr[i] = input.nextInt();
        }
        System.out.println("已经接收到所有结果!");
        // 计算数组总和
        int sum = calculateSum(arr);
        System.out.println("数组总和为:" + sum);

        // 计算数组最大值和最小值
        int max = findMax(arr);
        int min = findMin(arr);
        System.out.println("数组最大值为:" + max);
        System.out.println("数组最小值为:" + min);

        // 统计数组中的奇数个数
        int oddCount = countOddNumbers(arr);
        System.out.println("数组中奇数的个数为:" + oddCount);

        //排序
        System.out.println("排序结果为:");
        sort(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
    public static int calculateSum(int[] arr) {
        int sum = 0;
        for (int num : arr) {
            sum += num;
        }
        return sum;
    }

    // 查找数组中的最大值
    public static int findMax(int[] arr) {
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        return max;
    }

    // 查找数组中的最小值
    public static int findMin(int[] arr) {
        int min = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] < min) {
                min = arr[i];
            }
        }
        return min;
    }

    // 统计数组中的奇数个数
    public static int countOddNumbers(int[] arr) {
        int count = 0;
        for (int num : arr) {
            if (num % 2 != 0) {
                count++;
            }
        }
        return count;
    }

    //给数组排序
    public static void sort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] > arr[j]) {
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }
}


7.成员方法和类方法

类方法:static修饰的方法,属于类,不属于对象,可以直接通过类名调用,不用创建对象

示例:

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

    public static void main(String[] args) {
        int result = Math.add(10, 20);
        System.out.println("10 + 20 = " + result);
    }
}

成员方法:非static修饰的方法,属于对象,需要创建对象后调用

示例:

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void sayHello() {
        System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
    }

    public static void main(String[] args) {
        Person person = new Person("Tom", 20);
        person.sayHello();
    }
}

练习

public class PrimeNumbers {

    public static void main(String[] args) {
        System.out.println("100以内的质数有:");
        printPrimeNumbers(100);
    }

    // 输出指定范围内的所有质数
    public static void printPrimeNumbers(int max) {
        for (int num = 2; num <= max; num++) {
            if (isPrime(num)) {
                System.out.print(num + " ");
            }
        }
        System.out.println(); // 换行
    }

    // 判断一个数是否为质数
    public static boolean isPrime(int num) {
        if (num <= 1) {
            return false;
        }
        if (num == 2) {
            return true;
        }
        if (num % 2 == 0) {
            return false;
        }
        for (int i = 3; i <= Math.sqrt(num); i += 2) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }
}

输出100以内的斐波那契数列

public class FibonacciNumbers {

    public static void main(String[] args) {
        System.out.println("100以内的斐波那契数列:");
        printFibonacciNumbers(100);
    }

    // 输出100以内的斐波那契数列
    public static void printFibonacciNumbers(int max) {
        int a = 1, b = 1;
        System.out.print(a + " " + b + " ");

        while (true) {
            int next = a + b;
            if (next > max) {
                break;
            }
            System.out.print(next + " ");
            a = b;
            b = next;
        }
        System.out.println(); // 换行
    }
}

打印形状

import java.util.Scanner;

public class Draw
{
    public static void drawing(char ch,int x){
        for(int i=1;i<x+1;i++){
            for(int j=0;j<i;j++) System.out.print(ch);
            System.out.println();
        }
        for(int i=x;i>0;i--){
            for(int j=0;j<i;j++) System.out.print(ch);
            System.out.println();
        }
        for(int i=x;i>0;i--){
            for(int j=0;j<i;j++) System.out.print(' ');
            for(int j=0;j<x-i+1;j++) System.out.print(ch);
            System.out.println();
        }
        for(int i=0;i<x;i++){
            for(int j=0;j<i+1;j++) System.out.print(' ');
            for(int j=0;j<x-i;j++) System.out.print(ch);
            System.out.println();
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.printf("请输入你想输入的字母:");
        char ch = sc.next().charAt(0);
        System.out.printf("请输入你要打印的形状的深度:");
        int x = sc.nextInt();
        drawing(ch,x);
    }
}

查找字符串在子串中出现的次数

public class SubstringCount {

8.面向对象

类:模板,描述对象的属性和行为

对象:类的实例,具有类的所有属性和行为

实例变量:对象拥有的变量,用于存储对象的状态信息

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值