程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-06(1)

Java知识点总结(一)

发布于2020-11-19 20:30     阅读(1221)     评论(0)     点赞(5)     收藏(2)


1.强制类型转换

两条路线(从小到大):

byte——short——int——long——float——double

char——int——long——float——double

a. 小转大

public class Test03 {
    public static void main(String[] args) {
        // 强制类型转换,小转大
        int a = 10;
        double b = a;
        System.out.println(b);  //输出: 10.0
    }
}

b. 大转小(少用,可能造成精度丢失。)

public class Test04 {
    public static void main(String[] args) {
        // 大转小
        int c = 5;
        byte d = (byte)c;
        System.out.println(d); //输出:5
    }
}

2. Scanner 键盘输入

注意:先写new Scanner,按alt + enter自动补齐,创建一个input类。

public class TestScanner {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String name = input.next();
        System.out.println("您的名字是"+name);
        int age = input.nextInt();
        System.out.println("您的年龄是"+age);
    }
}
// 输出名字和年龄

3. 复合/赋值运算符

符号包括: +,-,*,/,%,= , +=, -=, *=, /=, %=

public class Test04 {
    public static void main(String[] args) {
        int a = 10;
        double b = 10;
        System.out.println(a+b); // 20.0
        System.out.println(a-b); // 0.0
        System.out.println(a/b); // 1.0
        System.out.println(a%b); // 0.0

        // 如何互换ab值?
        int c = 1;
        int d = 2;
        System.out.println("初始值c是"+c);
        System.out.println("初始值d是"+d);
        int k = 0 ;
        k = c;
        c = d;
        d = k;

        System.out.println("新值c是"+ c);
        System.out.println("新值d是"+ d);
    }
}

4. 关系/逻辑运算符

与非或 ,对应, &!| 使用时&& ||成对出现

 // 注意: 对字符串进行判断,anHao.equals("宝塔镇河妖")
 //  三目运算符
        String  min;
        min = 3>5?"呵呵":"喜喜";
        System.out.println(min); // 输出喜喜
public class Test05 {
    public static void main(String[] args) {
        int a =10;
        int b = 10;
        boolean boo = (a>b)&&(b<a); //false 有一个错就 false
        System.out.println(boo);
        boolean aoo = (a>b)||(b<a); //false 有一个对就true
        System.out.println(aoo);
        boolean coo = !(a>b)||(b<a);// true 负负得正
        System.out.println(coo);
       boolean doo = (12==0)&&(1/0< 1); // false 前面错了,后面就不再运行
       // boolean doo = (1/0< 1)&&(12==0);  报错,1/0不存在。
        System.out.println(doo);      
   }

       

5. 流程控制if else

  // if else流程控制语句
        System.out.println("请输入您的成绩");
        Scanner scanner = new Scanner(System.in);
        int score = scanner.nextInt();
        if(score>90) {
            System.out.println("你好牛逼");
        } else {
            System.out.println("小伙子,洗洗睡吧");

流程图如下:

在这里插入图片描述

原文链接:https://blog.csdn.net/SoULikeMe/article/details/109753462



所属网站分类: 技术文章 > 博客

作者:java战神

链接:http://www.javaheidong.com/blog/article/793/8fc76de95026f6111baf/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

5 0
收藏该文
已收藏

评论内容:(最多支持255个字符)