Scanner对象和流程控制
Scanner对象
import java.util.Scanner;//导包
Scanner scanner = new Scanner(System.in);//创建对象
scanner.close(); //关闭调用资源
next()
- 一定要读取到有效字符后才可以结束输入。
- 对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。
- 只能输入有效字符后才将其后面的输入的空格作为分隔符或结束符。
- next() 不能得到带有空格的字符串。
public static void main(String[] args) {
//创建一个扫描器对象,用于接受键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用extLine方式接收:");
//判断用户有没有输入字符串
if(scanner.hasNext()){
//使用next方式接收
String str = scanner.next();
System.out.println("输入的内容为:"+ str);
}
//凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
scanner.close();
}
输出结果:
使用next方式接收:
Hello Word
输入的内容为:Hello
nextLine()
- 以Enter(回车)为结束符,nextLine() 方法返回的数是输入回车之前的所有字符。
- 可以获得空白。
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextLine方式接收:");
if(scanner.hasNextLine()){
String sc = scanner.nextLine();
System.out.println("输入的内容为:" + sc);
}
scanner.close();
}
输出结果:
使用nextLine方式接收:
Hello Word
输入的内容为:Hello Word
不使用判断语句
public static void main(String[] args) {
//从键盘录入数据
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个数据:");
String str = scanner.nextLine();
System.out.println("输入的数据为:" + str);
scanner.close();
}
输出结果:
请输入一个数据:
Hello Word
输入的数据为:Hello Word
判断输入的数据
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//从键盘接收数据
int i = 0;
float f = 0.0f;
System.out.println("请输入整数:");
//如果...那么
if (scanner.hasNextInt()) {
i = scanner.nextInt();
System.out.println("整数数据:" + i);
} else {
System.out.println("你输入的不是整数数据!");
}
System.out.println("请输入整数:");
//如果...那么f
if (scanner.hasNextFloat()) {
f = scanner.nextFloat();
System.out.println("小数数据:" + f);
} else {
System.out.println("你输入的不是小数数据!");
}
scanner.close();
}
输出结果
//输入正确数字
请输入整数:
10
整数数据:10
请输入整数:
20.1
小数数据:20.1
//输入错误的数字
请输入整数:
20.1
你输入的不是整数数据!
请输入整数:
小数数据:20.1
输入多个数字,求其平均数
public static void main(String[] args) {
//输入多个数字,并求其平均数,每输入一个数字用回车确定,通过输入非数字来结束并输出执行结果;
Scanner scanner = new Scanner(System.in);
//和
double sum = 0;
//计算输入了多少个数字
int m = 0;
System.out.println("请输入数字:");
//通过求和判断是否还有输入,并在里面对每一次进行求和统计
while (scanner.hasNextDouble()) {
double x = scanner.nextDouble();
m = m + 1;//m++;
sum = sum + x;
System.out.println("你输入了第" + m + "个数据,当前的结果sum=" + sum);
}
System.out.println(m + "个数的和为" + sum);
System.out.println(m + "个数的平均数为" + (sum / m));
scanner.close();
}
输出结果
请输入数字:
20
你输入了第1个数据,当前的结果sum=20.0
30
你输入了第2个数据,当前的结果sum=50.0
40
你输入了第3个数据,当前的结果sum=90.0
50
你输入了第4个数据,当前的结果sum=140.0
60
你输入了第5个数据,当前的结果sum=200.0
a
5个数的和为200.0
5个数的平均数为40.0
流程控制
顺序结构
从上到下依次执行
选择结构
if单选择结构
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容:");
String s = scanner.nextLine();
//equals:判断字符串是否相等
if (s.equals("Hello")) {
System.out.println(s);
}
System.out.println("End");
scanner.close();
}
输出结果
请输入内容:
Hello
Hello
End
请输入内容:
Word
End
if双选择结构
public static void main(String[] args) {
//考试成绩大于60及格 , 小于60不及格。
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩");
int score = scanner.nextInt();
if (score > 60) {
System.out.println("及格");
} else {
System.out.println("不及格");
}
scanner.close();
}
输出结果
请输入成绩
80
及格
请输入成绩
50
不及格
if多选择结构
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩");
int score = scanner.nextInt();
if (score == 100) {
System.out.println("恭喜满分");
} else if (score <= 100 && score >= 90) {
System.out.println("A级");
} else if (score < 90 && score >= 80) {
System.out.println("B级");
} else if (score < 80 && score >= 70) {
System.out.println("C级");
} else if (score < 70 && score >= 60) {
System.out.println("D级");
} else if (score < 60 && score >= 0) {
System.out.println("不及格");
} else {
System.out.println("输入的成绩不合法");
}
scanner.close();
}
输出结果
请输入成绩
100
恭喜满分
请输入成绩
90
A级
请输入成绩
80
B级
请输入成绩
70
C级
请输入成绩
60
D级
请输入成绩
50
不及格
请输入成绩
120
输入的成绩不合法
嵌套的if结构
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩");
int score = scanner.nextInt();
if (score <= 100 && score >= 60) {
if (score <= 100 && score >= 90) {
System.out.println("A级");
} else if (score < 80 && score >= 70) {
System.out.println("C级");
} else if (score < 70 && score >= 60) {
System.out.println("D级");
}
} else if (score < 60 && score > 0) {
System.out.println("不及格");
} else {
System.out.println("输入的成绩不合法");
}
scanner.close();
}
switch多选择结构
public static void main(String[] args) {
//case穿透
char grade = 'B';
switch (grade) {
case 'A':
System.out.println("优秀");
break;//可选
case 'B':
System.out.println("良好");
break;//可选
case 'C':
System.out.println("及格");
break;//可选
case 'D':
System.out.println("再接再厉");
break;//可选
case 'E':
System.out.println("挂科");
break;//可选
default:
System.out.println("未知等级");
}
}
输出结果
良好
public static void main(String[] args) {
//JDk 7新特性,表达式可以是字符串!!!
//字符串的本质是还数字
//反编译 Java---class(字节码文件)---反编译(IDEA)
String name = "落yu";
switch (name){
case "陌生":
System.out.println("陌生");
break;
case "落yu":
System.out.println("落yu");
break;
default:
System.out.println("?????");
}
}
输出结果
落yu
反编译
- 打开项目结构
- 找到文件输出目录
- 把class文件复制到项目中打开
文件输出目录
循环结构
While 循环
public static void main(String[] args) {
//输出1~10
int i = 0;
while (i < 10) {
i++;
System.out.println(i);
//输出结果
/* 1
2
3
4
5
6
7
8
9
10*/
}
}
public static void main(String[] args) {
//死循环
while (true){
//等待客户链接
//定时检查
//........
}
}
public static void main(String[] args) {
//计算1+2+3+···+100
int i = 0;
int sum = 0;
while (i <= 100) {
sum = sum + i;
i++;
}
System.out.println(sum); //5050
}
do...while循环
public static void main(String[] args) {
int i = 0;
int sum = 0;
do {
sum = sum + i;
i++;
} while (i <= 100);
System.out.println(sum); //5050
}
public static void main(String[] args) {
int a = 0;
while (a<0){
System.out.println(a);
a++;
}
System.out.println("==================");
do {
System.out.println(a);
a++;
} while (a<0);
}
//结果
//==================
//0
for循环
public static void main(String[] args) {
int a = 1;//初始化条件
while (a <= 100) {//条件判断
System.out.println(a);//循环体
a += 2;//迭代
}
System.out.println("while循环结束");
//100.for
//初始化条件//条件判断//迭代
for (int i=1;i<=100;i++){
System.out.println(i);
}
System.out.println("for循环结束");
//死循环
for (;;){ }
}
public static void main(String[] args) {
//九九乘法表
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(i + "*" + j + "=" + (j * i) + "\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循环
public static void main(String[] args) {
int[] numbers = {10,20,30,40,50}; //定义一个数组
for (int i=0;i<5;i++){
System.out.println(numbers[i]);
}
System.out.println("=================");
//遍历数组元素
for (int x:numbers){
System.out.println(x);
}
}
/*
10
20
30
40
50
=================
10
20
30
40
50
*/
break continue
break
public static void main(String[] args) {
int i = 0;
while (i<=100){
i++;
System.out.println(i);
if (i==30){
break;//终止循环
}
}
System.out.println("123");
}
continue
public static void main(String[] args) {
int i = 0;
while (i<=100){
i++;
if(i%10 == 0){
System.out.println();
continue;//跳过本次循环
}
System.out.println(i);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人