香雨站

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 102|回复: 0

Java-基础

[复制链接]

2

主题

3

帖子

7

积分

新手上路

Rank: 1

积分
7
发表于 2023-6-9 11:41:38 | 显示全部楼层 |阅读模式
一.命名规则
// 类 大驼峰
MyCalss
// 函数
myfuction
// 变量 英文$_数字 小驼峰
age
myAge二.赋值运算符
int age = 18;
age += 2;
age++;
age -= 2;
age /= 2;
age *= 2;
age %= 2;  // 取余数三.接受输入
import java.util.Scanner;

public class Dome {
    public static void main(String[] args) {
        // 键盘输入功能
        Scanner scanner = new Scanner(System.in); // 固定写法
        System.out.println("请输入你的姓名:");
        String name = scanner.next();  // 请输入文本
        String age = scanner.nextInt();  // 请输入整数
        String amount = scanner.nextDouble();  // 请输入小数
        System.out.println("您输入的名字是:" + name);
        /*
        请输入你的姓名:
        4564
        您输入的名字是:4564
         */
    }
}四.基本数据类型
// 不同类型运算
public class Dome {
    public static void main(String[] args) {
        double score = 45.43;
        int up_score = 3;
        double score2 = score + up_score;
        System.out.println("总额:" + score2);
    }
}

1.如果一个操作数据类型为double型,则整个表达式可提升为double型
2.满足自动类型转换的条件
  a.两种类型都要兼容
    i.数值类型(整形和浮点型)互相兼容
  b.目标类型大于源类型
    v.比如double型大于 int 型
// 八个基本数据类型
1. 整数
public class Dome {
    public static void main(String[] args) {
        // 八个基本数据类型
        /*
            1个字节 byte -128 -》 +127
            2个字节 short -32768 -》 32767
            4个字节 int -21亿 -》+ 21亿
            8个字节 long -21。。。亿 -》 + 21。。。亿
         */
        byte b1 = 127;
        System.out.println(b1);
        System.out.println(Integer.MAX_VALUE);  // 查看最大数字
        System.out.println(Integer.MIN_VALUE);  // 查看最小数字
    }
}
2.小数
public class Dome {
    public static void main(String[] args) {
        /* 小树
            4个字节 float
            8个字节 double
         */
        float f = 3.14f;
        System.out.println(Float.MAX_VALUE);
        // 3.4028235E38  -> 3 后面 38 个0
        System.out.println(Double.MAX_VALUE);
        // 1.7976931348623157E308  -> 1 后面 308个 0
    }
}
3.字符
public class Dome {
    public static void main(String[] args) {
        /* 字符
            2个字符 char 0-65535  唯一一个没有负数概念的
         */
        char c1 = Character.MAX_VALUE;
        char c2 = Character.MIN_VALUE;
        System.out.println((int)c1);
        // 65535
        System.out.println((int)c2);
        // 0
    }
}
4.布尔
public class Dome {
    public static void main(String[] args) {
        /* 布尔
            1个字符 boolean 不是数值概念,而是真假状态
         */
        boolean b1 = Boolean.FALSE;
        boolean b2 = Boolean.TRUE;
        System.out.println(b1);
        // false
        System.out.println(b2);
        // true
    }
}五.类型强制转换
public class Dome {
    public static void main(String[] args) {
        /*
            类型转换
         */
        char c1 = '中';
        int i1 = 1;
        char c2 = (char) (c1 + i1);
        System.out.println(c2);
    }
}六.条件判断
public class Dome {
    public static void main(String[] args) {
        /*
            if else
         */
        if (true) {
            System.out.println("我是真");
        } else if (true) {
            System.out.println("我是第二个条件");
        } else {
            System.out.println("我是假");
        }
        /*
         如果条件后代码只有一行,可以省略 {}
         */
        if (true) System.out.println("我是真");
        else if (true) System.out.println("我是第二真");
        else System.out.println("我是假");
    }
}七.逻辑判断
public class Dome {
    public static void main(String[] args) {
        /*
            与 或 非
         */
        int res1 = 90, res2 = 90;  // 多个同类型定义可以一行
        if (res1 >= 90 && res2 >= 90) System.out.println("我都是90分");
        if (res1 >= 100 || res2 >= 90) System.out.println("我是与,达到其中一个");
        int flag = ~res1;
        boolean fl = flag <= 100;
        if (!fl) System.out.println("我是非");
    }
}八.字符串的比较
import java.util.Scanner;

public class Dome {
    public static void main(String[] args) {
        /*
            字符串的比较
         */
        Scanner scanner = new Scanner(System.in);
        String res2 = scanner.next(); // 只能接收字符串,不能接收字符

        if (res2 == "y") {
            System.out.println("我是字符和字符串比较结果 true");
        }else{
            System.out.println("我是字符和字符串结果 false");
        }

        // equals 应使用这个方法
        if (res2.equals("y")){  // 这里注意要用双引号 ""
            System.out.println("equals结果 true");

        }else {
            System.out.println("equals结果 false");

        }


    }
}
Connected to the target VM, address: '127.0.0.1:53863', transport: 'socket'
y
我是字符和字符串结果 false
equals结果 true
Disconnected from the target VM, address: '127.0.0.1:53863', transport: 'socket'九.switch选择结构
import java.sql.PreparedStatement;
import java.util.Scanner;

public class Dome {
    public static void main(String[] args) {
        /*
            switch 适用于等值判断
         */
        int res = 5;
        switch (res) {
            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;
            default:
                System.out.println("默认情况");
                break;
        }
    }
}
Connected to the target VM, address: '127.0.0.1:51927', transport: 'socket'
第五种情况
Disconnected from the target VM, address: '127.0.0.1:51927', transport: 'socket'
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|香雨站

GMT+8, 2025-3-15 05:08 , Processed in 0.708694 second(s), 24 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.. 技术支持 by 巅峰设计

快速回复 返回顶部 返回列表