Java流程控制
Java流程控制
一、Scanner对象
我们可以通过Scanner类来获取用户的输入。
- 基本语法:
Scanner s = new Scanner(System.in);
- 通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前我们一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据。
next()方法
public class Demo1 {
public static void main(String[] args) {
//创建扫描器对象,用于接收键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收:");
//判断用户有没有输入字符串
if (scanner.hasNext()) {
String s = scanner.next();
System.out.println("输出的内容:" + s);
}
//关闭Scanner,凡是属于IO流的类如果不关闭会一直占用资源
scanner.close();
}
}
运行结果
使用next方式接收:
hello czw
输出的内容:hello
nextLine()方法
public class Demo2 {
public static void main(String[] args) {
//从键盘接收数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextLine方式接收:");
//判断是否还有输入
if (scanner.hasNextLine()) {
String str = scanner.nextLine();
System.out.println("输出的内容:" + str);
}
scanner.close();
}
}
运行结果
使用nextLine方式接收:
hello czw
输出的内容:hello czw
next()与nextLine()的区别
- **next(): **
输入有效字符后,后面输入的空格作为分隔符或者结束符。next() 不能得到带有空格的字符串
- nextLine():
以回车(Enter)作为结束符,回车前的字符串都能输出,包括空格
二、Scanner进阶
public class Demo3 {
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("请输入小数:");
if (scanner.hasNextFloat()) {
f = scanner.nextFloat();
System.out.println("小数数据:" + f);
}else {
System.out.println("输入的不是小数!");
}
scanner.close();
}
}
题目:输入多个数字,求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果
public class Demo4 {
public static void main(String[] args) {
// 我们输入多个数字,求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果
Scanner scanner = new Scanner(System.in);
System.out.println("请用键盘输入数字:");
//总和
double sum = 0;
// 计算输入了多少数字
int count = 0;
//通过循环判断是否还有输入,并在里面对每一次进行求和统计
while (scanner.hasNextDouble()) {
double x = scanner.nextDouble();
count ++;
sum = sum + x;
}
System.out.println("输入数字的个数:" + count);
System.out.println("总和:" +sum);
System.out.println("平均数:" + sum/count);
scanner.close();
}
}
运行结果
请用键盘输入数字:
1
2
3
4
5
6
7
a
输入数字的个数:7
总和:28.0
平均数:4.0
三、顺序结构
最简单的算法结构
四、if选择结构 (重要)
五、循环结构
- while循环
输出1-100的数字
public class Demo5 {
// 输出1-100的数字
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
System.out.println(i);
}
}
}
计算1+2+3+...+100
public class Demo6 {
public static void main(String[] args) {
// 计算 1 + 2 + 3 + ... + 99 + 100 = ?
int i = 1;
int sum = 0;
while (i <= 100){
sum = sum + i;
i++;
}
System.out.println(sum);
}
}
- do while 循环 (循环至少会执行一次)
- for循环
while 和 do while的区别
while先判断后执行。do while 先执行后判断。
for循环 (重要)
for 循环语句是支持迭代的一种通用结构,是最高效的。
练习1:计算0到100之间的奇数和偶数的和
public class ForDemo1 {
public static void main(String[] args) {
// 计算0到100之间的奇数和偶数的和
int oddSum = 0;
int evenSum = 0;
for (int i = 0; i <= 100; i++) {
if (i%2 != 0){
//奇数
oddSum += i;
}else{
evenSum += i;
}
}
System.out.println("奇数的和:" + oddSum);
System.out.println("偶数的和:" + evenSum);
}
}
运行结果
奇数的和:2500
偶数的和:2550
练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
public class ForDemo2 {
public static void main(String[] args) {
//用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
for (int i = 1; i <= 1000; i++) {
if (i%5 == 0){
System.out.print(i+"\t");
}
if (i%15 == 0){
System.out.println();
}
}
}
}
练习3:打印九九乘法表
public class ForDemo3 {
public static void main(String[] args) {
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(j + "*" + i + "=" + (j*i) +"\t");
}
System.out.println();
}
}
}
运行结果
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
增强for循环
break和continue
break强制跳出循环,continue 用于终止某次循环过程。
练习
打印三角形
public class testDemo1 {
public static void main(String[] args) {
// 打印 5行 三角形
for (int i = 1; i <= 5; i++) {
for (int j = 5; 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();
}
}
}
运行结果
*
***
*****
*******
*********
浙公网安备 33010602011771号