zoukankan      html  css  js  c++  java
  • [Java] 语法基础

    1.常量

    final int MAX = 100;

    2.变量

    关键字 所占位数 取值范围
    boolean 8 true/false
    byte 8 -2^7 ~ 2^7-1
    short 16 -2^15 ~ 2^15-1
    int  32 -2^31 ~ 2^31-1
    long 64 -2^63 ~ 2^63-1
    float 32 3.4*10^-38 ~ 3.4*10^38
    double 64 1.7*10^-38 ~ 1.7*10^38
    char 16 Unicode码中0~65536所有代表的数值

    3.运算符

    二元算数运算符 +,-,*,/,%

    一元算数运算符 ++, --

    位于运算数之前的一元运算符,表示“先自增自减后再参与其他运算”

    位于运算数之后的一元运算符,表示“先参与其他运算后再自增自减”

    eg:

    int a = 5;

    int b = ++a;  //b=6

    int c = --a;   //c=5

    int d = a++;  //d=5

    int e = a--;    //e=6

            //a=5

    4.关系运算符

    >,<,>=,<=,==,!=

    浮点数不能做==运算,而应用fabs(A-B) < epsilon

    5.逻辑运算符

    &&,||,!

    6.条件运算符

    c=a>b?a:b

    7.语句

    选择

    if(){}else if(){}else{}

    switch(){case 常量:statement;default:statement;}

    循环 for,while,do...while

    跳转 break,continue,return

    eg1:

    statement1;

    tab:{

    if(true)

      break tab;

    statement2; //不会执行

    }

    statement3;

    eg2:

    int i, j, sum;

    one: for(i=1; i<10; i++){

      for(j=1; j<10; j++){

        if(j>i){

          System.out.println();

          continue one;

        }

        System.out.print(i+j);

      }

    }

    8.输出语句

    System.out.println();

    System.out.print();

    eg:

    int a = 0;

    System.out.println("abc123");

    System.out.println(a);

    System.out.println("abc123"+a);

    9.输入语句

    System.in.read();      //读取一个字符的ASCII码,返回至整型形式

    System.in.read(byte b[]);  //读取一行字符到数组byte b[]中,返回字符数组的个数

    准输入与标准输出有一点不同,就是输入涉及异常处理,所以必须添加异常处理语句。最简单的异常处理方法是在main()头部添加 throws Exception

    eg:

    public static void main(String[] args) throws Exception{

     byte b[] = new byte[64];

     int a; 

     a = System.in.read(b);

     for (int i = 0; i < a; i++){

      System.out.println((char)b[i]);

      }

    }

    10.其他输入方法

    Scanner 类。在Scanner 类中有方法 netInt()用于接收数字,nextChar()用于接收字符。

    eg:

    import java.util.*;

    public class Example{

      public static void main(String[] args){

        int a, b;

        Scanner s = new Scanner(System.in);

        a = s.nextInt();

        b = s.netInt();

        System.out.println(a+b);

      }

    }

    11.数组

    数组定义:

    int a[] = new int[5];

    int a[][] = new int[3][5];

    数组赋值:

    数组定以后,系统给予数值的每个元素一个默认值,这称为自动初始化的初值,如表:

    元素类型 初值
    整型 0
    浮点型 0.0
    布尔型 false
    字符型 ''

    int a[] = {1,2,3};

    int b[][] = new int[2][];

    b[0] = new int[3];

    b[1] = new int[4];

  • 相关阅读:
    一:Go编程语言规范--块、声明、作用域
    三:shell运算符
    二:shell之bash变量
    一:Shell基础
    Linux vim(4)
    二:C语言(分之结构)
    一:c语言(数据类型和运算符)
    吐槽一下百度系网站图片的一些问题
    深入理解querySelector(All)
    当fixed元素相互嵌套时chrome下父元素会影响子元素的层叠关系
  • 原文地址:https://www.cnblogs.com/feifeidxl/p/4718714.html
Copyright © 2011-2022 走看看