1、Java输入和输出
1、输入
1.通过import语句导入java.util.Scanner类(import是导入某个类的语句,必须放到Java源代码的开头)
2.创建Scanner对象并传入System.in。System.out代表标准输出流,而System.in代表标准输入流。直接使用System.in读取用户输入虽然是可以的,但需要更复杂的代码,而通过Scanner就可以简化后续的代码。
3.有了Scanner对象后,要读取用户输入的字符串,使用scanner.nextLine(),要读取用户输入的整数,使用scanner.nextInt()。Scanner会自动转换数据类型,因此不必手动转换。
2、输出
1、格式化输出
格式化输出使用System.out.printf(),通过使用占位符%?
2、Java 条件语句 if
1、单分支if语句 if
2、分支if结构 if…else
3、多分支结构 if…else if…else
一旦其中一个 else if 语句检测为 true,其他的 else if 以及 else 语句都将跳过执行。
4、嵌套if
3、switch case 多重选择语句
根据某个表达式的结果,分别去执行不同的分支。
1.switch语句根据switch (表达式)计算的结果,跳转到匹配的case结果,然后继续执行后续语句,直到遇到break结束执行。
2.如果计算的结果没有匹配到任何case,那么,switch语句不会执行任何语句。这时,可以给switch语句加一个default,当没有匹配到任何case时,执行default。
3.如果当前匹配成功的 case 语句块没有 break 语句,则从当前 case 开始,后续所有 case 的值都会输出,如果后续的 case 语句块有 break 语句则会跳出判断。
public class Main {
public static void main(String[] args) {
int option = 99;
switch (option) {
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("Not selected");
break;
}
}
}
Java12 开始switch可以使用全新的语法:
public class Main {
public static void main(String[] args) {
String fruit = "pple";
switch (fruit) {
case "apple" -> System.out.println("Selected apple");
case "pear" -> System.out.println("Selected pear");
case "mango","dfsadf" -> {
System.out.println("Selected mango");
System.out.println("Good choice!");
}
default -> System.out.println("No fruit selected");
}
}
}
4、Java 循环结构
1、for循环
1、普通for循环
public class Main {
public static void main(String[] args) {
// for(初始化; 布尔表达式; 更新) {
// //代码语句
// }
for (int x = 1; x < 5; x++) {
System.out.print("守望先锋: " + x);
System.out.print("\n");
}
int[] ns = {1, 4, 9, 16, 25};//数组
for (int i = 0; i < ns.length; i++) {
System.out.println(ns[i]);
}
}
}
Java5 引入了一种主要用于数组的增强型 for 循环。
2、增强型 for 循环(for each循环)主要用于数组。
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
for (int x : numbers) {
System.out.print(x);
System.out.print(",");
}
System.out.print("\n=========\n");
String[] names = {"James", "Larry", "Tom", "Lacy"};
for (String name : names) {
System.out.print(name);
System.out.print(",");
}
}
}
2、while循环
while语句的特点是先判断,后执行,若条件不成立,有可能一次也不执行。
public class Main {
// while( 布尔表达式 ) {
// //循环内容
//}
public static void main(String[] args) {
int x = 5;
while( x < 10 ) {
System.out.println("守望先锋: " + x );
x++;
}
}
}
3、do…while循环
do…while语句的特点是先执行,后判断,不管条件是否成立,至少执行循环体一次。
public static void main(String[] args) {
int x = 10;
do {
System.out.print("守望先锋: " + x);
x++;
System.out.print("\n");
} while (x < 20);
}
}
4、循环嵌套
- 一个循环语句的循环体内包含另外一个循环语句,这种编程方式称为“循环的嵌套”。
- 循环嵌套时,外层循环执行一次,内层循环从头到尾执行一遍。
- 3种循环(while循环、do-while循环和for循环)可以自身嵌套,也可以互相嵌套,最常见的是在for循环中嵌套for循环。
5、break 关键字和continue 关键字
1、break 关键字
break 主要用在循环语句或者 switch 语句中,用来跳出整个语句块。
break 跳出最里层的循环,并且继续执行该循环下面的语句。
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
// x 等于 30 时跳出循环
if( x == 30 ) {
break;
}
System.out.print( x );
System.out.print("\n");
}
2、continue 关键字
continue 适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。
在 for 循环中,continue 语句使程序立即跳转到更新语句。
在 while 或者 do…while 循环中,程序立即跳转到布尔表达式的判断语句。
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
for (int x : numbers) {
if (x == 30) {
continue;
}
System.out.println(x);
}
}
}