入门JAVA语言基础

155 阅读2分钟

1.public static void main(String[ ] args )

关键字public 称为访问修饰符( access modifier ),修饰符用于控制程序的其他部分对这段代码的访问级别。

关键字class 表明Java程序中的全部内容都包含在类中。

关键字class 后面紧跟类名。Java 中定义类名的规则很宽松,名字必须以字母开头,后面可以跟字母和数字的任意组合。长度基本上没有限制,不能使用Java保留字。

标准的命名规范为:类名是以大写字母开头的名词。如果名字由多个单词组成,每个单词的第一个字母都应该大写

主方法入口:所有的 Java 程序由 public static void main(String[] args) 方法开始执行,JavaWeb应用的类却基本没有main方法

System.out.println 的功能是:将一个文本行输出到控制台上

源代码的文件名必须与公共类的名字相同,并用.java作为扩展名。

每条语句都必须以分号结束

修饰符

像其他语言一样,Java可以使用修饰符来修饰类中方法和属性。

主要有两类修饰符

访问控制修饰符 :default, public , protected, private

非访问控制修饰符 : final, abstract, static, synchronized

2.JAVA数据类型

(1)基本数据类型

整数类型(byte[1]、short[2]、int[4]、long[8]

浮点型 float[4]、double[8]

字符型 char[2] 单个字符'a'

布尔型 boolean[1] 存放true、false

(2)引用数据类型

类 class

路口 interface

数组 [ ]

3.语句

(1)switch

输入1-9的数字,转化为对应英文输出

import java.util.Scanner;
public class ShuZi{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        switch(n){
            case 1:
                System.out.println("one");
                break;
            case 2:
                System.out.println("two");
                break;
            case 3:
                System.out.println("three");
                break;
            case 4:
                System.out.println("four");
                break;
            case 5:
                System.out.println("five");
                break;
            case 6:
                System.out.println("six");
                break;
            case 7:
                System.out.println("seven");
                break;
            case 8:
                System.out.println("eight");
                break;
            case 9:
                System.out.println("nine");
                break;
            default:
                System.out.println("out");
        }
    }
}

(2)for

输出1+2+...+100

public class LeiJia{
    public static void main(String[] args) {
        int num = 0;
        for(int i = 1;i <= 100;i++){
            num =  num + i;
        }
            System.out.println(num);
    }
}

(3)if...else

输入三个整数,输出其中最大值

import java.util.Scanner;
public class BiJiao{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int A = sc.nextInt();
        int B = sc.nextInt();
        int C = sc.nextInt();
        if (A > B && A > C){
            System.out.println(A);
        }else if(B > A && B > C){
            System.out.println(B);
        }else if(C > A && C > B){
            System.out.println(C);
        }
    }
}

(4)while

输出1-n的数字中,各个位数之和为13的整数个数

import java.util.Scanner;
public class l {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int count = 0;
        int i = 0;
        while (i <= n) {
            i++;
            int a = i % 10;  //个位
            int b = (i / 10) %10;  //十位
            int c = ( i /100) % 10; //百位
            int d = i /1000; //千位
            int e = a + b + c + d ;
            if(e == 13){
                count ++;
            }
        }
        System.out.println(count);
    }
}

image.png

4.操作符

  • 加法 - 相加运算符两侧的值
  • 减法 - 左操作数减去右操作数
  • 乘法 - 相乘操作符两侧的值

/ 除法 - 左操作数除以右操作数

% 取余 - 左操作数除以右操作数的余数

++ 自增: 操作数的值增加1

-- 自减: 操作数的值减少1

5.类型转换

LQ@SO(BLQ6O_YF~WOWQJ)OM.png

image.png