java语法基础(一)


目录

一、首先我们编写一个java程序

经典Hello World!

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

二、语法基础

1变量

变量必须先定义才能使用,而且变量不能名字不能重复。

public class Main {
    public static void main(String[] args) {
        int a=1,b=2;
        System.out.println(a);
        System.out.println(b);
    }
}

image

2.常量

在java中我们的常量是用final修饰,可以类比c++中的const

final N = 1000;

3.类型转化

显示转化:int x = (int)’A’;
隐式转化:double x = 12, y = 2 * 2.3;

4.运算符

大家参考C++中的运算符即可

5.表达式

整数的四则运算

public class Main {
    public static void main(String[] args) {
        int a = 12 + 14 / 7 -3;
        System.out.println(a);
    }
}

浮点数的运算

public class Main {
    public static void main(String[] args) {
        double a = 1.3, b = 2.6;
        System.out.println(a * b);
        System.out.println(b - a);
        System.out.println(a + b);
        System.out.println(b / a);
    }
}

变量的自增自减

public class Main {
    public static void main(String[] args) {
        int a = 1;
        int b = a ++;
        System.out.println(a + " " + b);

        int c = ++ a;
        System.out.println(a + " " + c);
    }
}

image
我们通过输出就可以观察到,a++是先把值a的值赋给b然后再进行自加,++a是先进行自加然后再把值赋给c

三、输入输出

1.输入

方式一:效率比较低,在读入规模较小的时候使用

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);//创建一个对象
        String str = sc.next();//读入一个字符串
        int x = sc.nextInt();abc//读入一个整数
        float y = sc.nextFloat();//读入一个float类型的小数
        double z = sc.nextDouble();//读入一个double类型的小数
        String line = sc.nextLine();//读入一行字符串
        System.out.println(str + " " + x + " " + y + " " + z + line);
    }
}

方式二:效率比较高,在读入规模较大的时候使用 需要抛异常

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        System.out.println(str);
    }
}

这种读入的方式只能读字符串,所以我们如果要读整形或者小数点的话我们就要手动的转换

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int x = Integer.parseInt(br.readLine());
        int y = Integer.parseInt(br.readLine());
		double a = Double.parseDouble(br.readLine());
        System.out.println(x + y);
    }
}

但是呢,它一次只能读一行,所以我们读两个要分两行,那我们想要在一行内读入多个数据怎么办,那么这时就要进行分割一下

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] str = br.readLine().split(" ");
        int x = Integer.parseInt(str[0]);
        int y = Integer.parseInt(str[1]);
        System.out.println(x + y);
    }
}

2.输出

方式一:输出的效率低,在输出规模较小的时候使用

public class Main {
    public static void main(String[] args) {
        System.out.println(123);//加上换行
        System.out.println("Hello World!");
        System.out.print(123);//不加换行
        System.out.print("Hello World!/n");
        System.out.printf("%f %d/n",9.0, 12);//类似c语言的输出,输出小数统一用%f不用%lf
    }
}

方式二:输出的效率高,在输出的规模较大的时候使用 需要抛异常并且手动清理缓冲区

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        bw.write("Hello World!/n");
        bw.flush(); //需要手动刷新缓冲区,不刷新缓冲区的话会得不到输出
    }
}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/289624.html

(0)
上一篇 2022年9月15日
下一篇 2022年9月15日

相关推荐

发表回复

登录后才能评论