基本语法
包括:输入和输出、if语句、switch语句、while循环、do-while循环、for循环、break和continue、遍历数组、数组排序、多维数组
输出结果都写在了注释中
输入和输出
public class demo1 {
// 输出
public static void main(String[] args) {
System.out.print("A,");
System.out.print("B,");
System.out.print("C.");
// println是print line的缩写,表示输出并换行
// 如果输出后不想换行,可以用print()
System.out.println();
System.out.println("END");
// A,B,C.
// END
//格式化输出
double d = 3.1415926;
System.out.printf("%.2fn", d);
// 3.14
System.out.printf("%.4fn", d);
// 3.1416
// hex表示把一个整数格式化成十六进制
int n = 12345000;
System.out.printf("n=%d, hex=%08x", n, n);
// n=12, hex=0000000c
}
}
占位符 | 对应数据类型 | 描述 |
---|---|---|
%d | int | 有符号十进制整数 |
%u | unsigned int | 无符号十进制整数 |
%f | float , double | 浮点数 |
%e | float , double | 科学记数法表示的浮点数 |
%g | float , double | 根据数值自动选择%f 或%e 中的较短者 |
%c | char | 字符 |
%s | char * | 字符串 |
%p | 指针类型 | 指针地址 |
%x | unsigned int | 无符号十六进制整数 |
%o | unsigned int | 八进制整数 |
%lu | unsigned long int | 无符号长整型 |
%ld | long int | 长整型 |
%% | 无 | 打印百分号本身 |
%lf | double | 双精度浮点数。主要用于 printf 系列函数输出 double 类型数据,与%f 功能相同;在 scanf 系列函数中用于读取 double 类型的数据 |
import java.util.Scanner;
public class demo2 {
// 输入
public static void main(String[] args) {
// 创建Scanner对象,并传入System.in
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine(); // 读取一行输入并获取字符串
int age = scanner.nextInt(); // 读取一行输入并获取整数
System.out.printf("Hi, %s, you are %dn", name, age);
}
}
if条件判断
public class demo3 {
// if条件判断
public static void main(String[] args){
int n = 90;
if(n > 90){
System.out.println("优秀");
}else if(n > 60){
System.out.println("及格");
}else{
System.out.println("不及格");
}
// 及格
// 浮点数在计算机中常常无法精确表示,并且计算可能出现误差,因此,判断浮点数相等用==判断不靠谱
double x = 1 - 9.0 / 10;
if (x == 0.1) {
System.out.println("x is 0.1");
} else {
System.out.printf("x is not 0.1,x = %fn",x);
}
// x not 0.1,x = 0.100000
// 正确的方法是利用差值小于某个临界值来判断
if(Math.abs(x-0.1)<0.00001){
System.out.println("x is 0.1");
}else{
System.out.println("x is not 0.1");
}
// x is 0.1
// 判断引用类型相等
String s1 = "hello";
String s2 = "HELLO".toLowerCase();
System.out.println(s1);
System.out.println(s2);
if (s1 == s2) {
System.out.println("s1 == s2");
} else {
System.out.println("s1 != s2");
}
// hello
// hello
// s1 != s2
// 要判断引用类型的变量内容是否相等,必须使用equals()方法
if (s1.equals(s2)) {
System.out.println("s1 equals s2");
} else {
System.out.println("s1 not equals s2");
}
// s1 equals s2
}
}
switch多重选择
import java.util.Scanner;
public class demo4 {
// switch多重选择
public static void main(String[] args){
int op = 2;
switch(op){
case 1:
System.out.println("Selected 1");
break;
case 2:
System.out.println("Selected 2");
break;
case 3:
System.out.println("Selected 3");
break;
default:
System.out.println("Selected other");
break;
}
// Selected 2
// 使用switch时,注意case语句并没有花括号{},而且,case语句具有“穿透性”
switch (op) {
case 1:
System.out.println("Selected 1");
case 2:
System.out.println("Selected 2");
case 3:
System.out.println("Selected 3");
default:
System.out.println("Selected other");
}
// Selected 2
// Selected 3
// Selected other
// 从Java 12开始,switch语句升级为更简洁的表达式语法
// 使用类似模式匹配(Pattern Matching)的方法,保证只有一种路径会被执行,并且不需要break语句
String fruit = "apple";
switch (fruit) {
case "apple" -> System.out.println("Selected apple");
case "pear" -> System.out.println("Selected pear");
case "mango" -> {
System.out.println("Selected mango");
System.out.println("Good choice");
}
default -> System.out.println("No fruit selected");
}
// Selected apple
// 使用新的switch语法,不但不需要break,还可以直接返回值
int opt = switch(fruit){
case "apple" -> 1;
case "pair","mango" -> 2;
default -> {
int code = fruit.hashCode();
yield code; // switch语句返回值
}
}; // 赋值语句要以;结束!
System.out.println("opt = " + opt);
// opt = 1
// 石头剪刀布游戏
System.out.println("请选择:");
System.out.println(" 1: 石头");
System.out.println(" 2: 剪刀");
System.out.println(" 3: 布");
// 用户输入:
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
// 计算机随机数 1, 2, 3:
int random = (int)Math.random()*3+1; // TODO:关于Math.random()函数的使用
switch (choice) {
case 1 -> {
switch(random){
case 1 -> System.out.println("平局");
case 2 -> System.out.println("你赢了");
case 3 -> System.out.println("你输了");
}
}
case 2 -> {
switch(random){
case 1 -> System.out.println("你输了");
case 2 -> System.out.println("平局");
case 3 -> System.out.println("你赢了");
}
}
case 3 -> {
switch(random){
case 1 -> System.out.println("你赢了");
case 2 -> System.out.println("你输了");
case 3 -> System.out.println("平局");
}
}
}
}
}
while,do-while循环
public class demo5 {
// while,do-while循环
public static void main(String[] args){
// 使用while循环计算从20到100的和
int sum = 0;
int m = 20;
int n = 100;
while(m<=n){
sum += m;
m++;
}
System.out.println(sum);
System.out.println(m);
// 4860
// 101
//使用do-while循环计算从20到100的和
int sum1 = 0;
int m1 = 20;
do {
sum1 += m1;
m1++;
} while (m1 <= n);
System.out.println(sum1);
System.out.println(m1);
// 4860
// 101
}
}
for循环
public class demo6 {
// for循环
public static void main(String[] args){
int[] arr = {1,4,9,16,25};
int sum = 0;
for(int i = 0;i<arr.length;i++){
sum += arr[i];
}
System.out.println(sum);
// for each循环,可以更简单地遍历数组
int sum1 = 0;
for(int n:arr){
System.out.print(n + " ");
sum1 += n;
}
System.out.println(); // 换行
System.out.println(sum1);
// 1 4 9 16 25
// 55
}
}
break和continue
public class demo7 {
// break和continue
public static void main(String[] args) {
for (int i=1; i<=3; i++) {
System.out.println("i = " + i);
for (int j=1; j<=3; j++) {
System.out.println("j = " + j);
if (j >= i) {
break;
}
}
// break跳到这里
System.out.println("breaked");
}
// i = 1
// j = 1
// breaked
// i = 2
// j = 1
// j = 2
// breaked
// i = 3
// j = 1
// j = 2
// j = 3
// breaked
for (int i=1; i<=10; i++) {
if (i % 2 == 0) { // 偶数时
continue; // continue语句会结束本次循环
}
System.out.print(i + " ");
}
// 1 3 5 7 9
}
}
遍历数组
import java.util.Arrays;
public class demo8 {
//遍历数组
public static void main(String[] args) {
// Arrays.toString(),可以快速打印数组内容
int[] ns = { 1, 1, 2, 3, 5, 8 };
System.out.println(Arrays.toString(ns));
// [1, 1, 2, 3, 5, 8]
}
}
数组排序
import java.util.Arrays;
public class demo9 {
// 冒泡排序
public static void main(String[] args){
int[] ns = { 28, 12, 89, 73, 65, 18, 96, 50, 8, 36 };
for(int i = 0;i<ns.length-1;i++){
for(int j = 0;j<ns.length-i-1;j++){
if(ns[j] > ns[j+1]){
int temp = ns[j];
ns[j] = ns[j+1];
ns[j+1] = temp;
}
}
}
System.out.println(Arrays.toString(ns));
int[] ns1 = { 28, 12, 89, 73, 65, 18, 96, 50, 8, 36 };
// [8, 12, 18, 28, 36, 50, 65, 73, 89, 96]
Arrays.sort(ns1); // sort(),内置的排序函数
System.out.println(Arrays.toString(ns1));
// [8, 12, 18, 28, 36, 50, 65, 73, 89, 96]
}
}
多维数组
import java.util.Arrays;
public class demo10 {
// 多维数组
public static void main(String[] args){
// 二维数组
int[][] ns = { //[行数][列数]
{1,2,3,4}, // 逗号
{5,6,7},
{9,10,11,12}
}; // 分号
System.out.println(ns.length); //length为行数
// 3
int[] arr = ns[0];
// 实际上arr0就获取了ns数组的第0个元素。
// 因为ns数组的每个元素也是一个数组,因此,arr0指向的数组就是{ 1, 2, 3, 4 }
System.out.println(arr.length);
// 4
//for each循环遍历二维数组
for(int[] ar:ns){
for(int n:ar){
System.out.print(n + ",");
}
System.out.println();
}
// 1,2,3,4,
// 5,6,7,
// 9,10,11,12,
// Arrays.deepToString()遍历
System.out.println(Arrays.deepToString(ns));
// [[1, 2, 3, 4], [5, 6, 7], [9, 10, 11, 12]]
//三维数组
int[][][] nss = {
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
},
{
{10, 11},
{12, 13}
},
{
{14, 15, 16},
{17, 18}
}
};
System.out.println(Arrays.deepToString(nss));
// [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11], [12, 13]], [[14, 15, 16], [17, 18]]]
}
}
Comments NOTHING