1、变量、运算符、输入与输出
1、内置数据类型
2、常量
final int N=120;
3、类型转化
int x = (int)'A';
double x = 12, y = 4 * 3.3;
4、表达式
int a=1,b=2,c=3;
int x=a*b+c/a;
a++;
b--
5、输入(重点)
方式1,效率较低,输入规模较小时使用。
Scanner sc = new Scanner(System.in);
String str = sc.next();
int x = sc.nextInt();
float y = sc.nextFloat();
double z = sc.nextDouble();
String line = sc.nextLine();
方式2,效率较高,输入规模较大时使用。注意需要抛异常。
package com.yxc;
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);
}
}
6、输出(重点)
方式1,效率较低,输出规模较小时使用。
System.out.println(123);
System.out.println("Hello World");
System.out.print(123);
System.out.print("yxc\n");
System.out.printf("%04d %.2f\n", 4, 123.456D);
方式2,效率较高,输出规模较大时使用。注意需要抛异常。
package com.yxc;
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();
}
}
2、判断语句
1、if-else语句
package com.yxc;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
if (year % 100 == 0) {
if (year % 400 == 0)
System.out.printf("%d是闰年\n", year);
else
System.out.printf(&#