您的位置:首页 - 教程 - Java - 正文
Java Cheatsheet示例代码

Hello, World.

public class learn {
    public static void main(String[] args){
        System.out.println("hi");
    }
}

 

Built-in data types:

int, double, boolean, char, String

 

类型转换:

字符串转为数字: 

        String s1 = "1234";
        String s2 = "1234.123";
        
        int a = Integer.parseInt(s1);
        double b = Double.parseDouble(s2);
        long c = Long.parseLong(s1);
        
        System.out.println(a); //1234
        System.out.println(b); //1234.123
        System.out.println(c); //1234

 

数字转换成字符串:

        double d = 123.4;
        String s = String.valueOf(d);
        System.out.println(s); //123.4

 

字符串加数字, 生成字符串

"1234"+99 --> "123499"

 

Switch:

switch(day){
  case 0: System.out.println("Sun"); break;
  case 1: System.out.println("Mon"); break;
  
}

 

数组:

double[] b = new double[N];

 

Input:

Scanner in=new Scanner(System.in);
String readLine = in.nextLine();
System.out.println(readLine);

 

字符串长度:

s.length()


评论: