1 用户交互Scanner
获取用户输入基本语法:Scanner scanner = new Scanner(System.in);
在读取前一般需要使用hasNext()和hasNextLine()判断是否还有输入的数据
通过Scanner类的next()与nextLine()方法获取输入的字符串
- next()
- 1、一定要读取到有效字符后才结束输入
- 2、在输入有效字符前遇到空格,会自动将其去掉
- 3、输入有效字符后,其后面输入的空格将作为分隔符或结束符
- 4、不能得到带有空格的字符串
- nextLine()
- 以Enter作为结束符,可以获得空白
- hasNext、next不能直接识别裸回车作为输入,hasNextLine和nextLine都可以直接识别裸回车
- hasNext、hasNextLine在返回一个boolean类型结果true的同时,会在堆空间中开辟一块专门用于存放刚刚输入的字符串,用于下次next或者nextLine:即下次next或者nextLine不需要再从键盘输入,相当于系统自动把刚刚输入的字符串再原封不动的输入了一遍。同时这个存储寿命=调用对象的寿命
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
//创建一个对象,用于接收键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收:");
//判断用户是否输入了字符串
if(scanner.hasNext()){
//使用next方式接受
String str = scanner.next();//等待用户输入
//输入hello world,输出hello
System.out.println("输出的内容为:"+str);
// String str2 = scanner.nextLine();
//输入hello world,输出hello world
// System.out.println("输出的内容为:"+str2);//
}
//凡是属于IO流的类如果不关闭会一直占用资源
scanner.close();
}
}
2 选择结构
if选择结构
- if(布尔表达式){//布尔表达式为true执行语句}
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入:");
String str = scanner.nextLine();
//equals:判断字符串是否相等
if(str.equals("yuans")){
System.out.println("字符串相等");
}
System.out.println("End");
scanner.close();
}
}
- if(布尔表达式){//布尔表达式为true执行语句}else{//布尔表达式为false执行语句}
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt();
//equals:判断字符串是否相等
if(score < 60){
System.out.println("成绩不合格");
}else{
System.out.println("成绩合格");
}
scanner.close();
}
}
- if(布尔表达式1){//布尔表达式1为true执行语句}else if(布尔表达式2){//布尔表达式2为true执行语句}else{//以上布尔表达式都为false执行语句}
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt();
//equals:判断字符串是否相等
if(score == 100){
System.out.println("满分");
}else if(score >= 80 && score < 100){
System.out.println("优秀");
}else if(score >= 60 && score < 80){
System.out.println("良好");
}else if(score >= 0 && score < 60){
System.out.println("未及格");
}else{
System.out.println("成绩不合法");
}
scanner.close();
}
}
- if嵌套:if(布尔表达式1){if(布尔表达式2){}}
switch选择结构
变量类型可以是byte、short、int、char
支持String类型
break退出switch选择,如果不使用break,将会继续执行之后的case语句
public class Demo {
public static void main(String[] args) {
char grade = 'C';
switch (grade){
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("挂科");
break;
default:
System.out.println("未知");
break;
}
}
}
3 循环结构
while循环
while(布尔表达式){}
先判断后执行,如果不满足条件将不能进入循环
只要布尔表达式为true,循环将一直执行
public class Demo {
public static void main(String[] args) {
//输出1-100
int i = 0;
while(i<100){
i++;
System.out.println(i);
}
}
}
do...while循环
do{//代码}while(布尔表达式)
先执行后判断,循环至少执行一次
public class Demo {
public static void main(String[] args) {
int i = 0;
do {
System.out.println(i);//输出0
i++;
System.out.println(i);//输出1
}while(i<0);
System.out.println(i);//输出1
}
}
for循环
for(初始化值;布尔表达式;迭代){}
public class Demo {
public static void main(String[] args) {
//0-100内的奇数和、偶数和
int oddSum = 0;
int evenSum = 0;
for(int i = 0;i < 100;i++){
if (i%2==1){
oddSum +=i;//奇数和
}else{
evenSum+=i;//偶数和
}
}
System.out.println("奇数和:"+oddSum);
System.out.println("偶数和:"+evenSum);
}
}
public class Demo {
public static void main(String[] args) {
//九九乘法表
int sum = 0;
for(int i = 1;i < 10;i++){
for(int j = 1;j <= i ;j++){
sum = i*j;
System.out.print(j+"*"+i+"="+sum+"\t");
}
System.out.println();
}
}
}
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
增强for循环
for(声明语句:表达式){}
主要用于数组或集合
- 声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。作用域限定在循环语句块,其值与数组元素的值相等
- 表达式:要访问的数组名,或是返回值为数组的方法
public class Demo {
public static void main(String[] args) {
int [] numbers = {1,2,3,4,5};//定义数组
for(int x:numbers){//将number里的每个值赋给x
System.out.println(x);
}
}
}
- break强制退出循环
- continue跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定
打印菱形
public class Demo {
public static void main(String[] args) {
for(int i = 1;i<=2;i++) {
for (int j = 2; j >= i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
for (int j = 1; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
for(int i = 1;i<=3;i++){
for(int j =1;j<i;j++){
System.out.print(" ");
}
for(int j =3;j>=i;j--){
System.out.print("*");
}
for(int j =3;j>i;j--){
System.out.print("*");
}
System.out.println();
}
}
}
*
***
*****
***
*