用户交互Scanner
Scanner对象
- 之前我们学的基本语法中我们并没有实现程序与人的交互,但是Java给我们提供了一个工具类,用来实现程序与人的交互。java.util.Scanner是java5的新特征,我们可以通过Scanner类来获取用户的输入。
- 基本语法
Scanner s= new Scanner(System.in);
- 通过Scanner类的next()与nextline()方法获取输入的字符串,在读取前我们一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据。
- next():
- 一定要读取到有效字符后才可以结束输入。
- 对输入有效字符之前遇到的空白,next()方法会自动将其去掉。
- 只有输入有效字符之后才将其后面输入的空白作为分隔符或者结束符。
- next()不能得到带有空格的字符
public class Demo01 {
public static void main(String[] args) {
//创建一个扫描器对象,用于接收键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收:");
//判断用户有没有输入字符串
if(scanner.hasNext()){
//使用next方式接收
String str=scanner.next();//程序会等待用户输入完毕
System.out.println("输入的内容为:"+str);
}
//凡是属于IO流的类如果一直不关闭会一直占用资源,要养成好习惯用完就关闭
scanner.close();
}
}
输出结果
使用next方式接收:
hello world!
输入的内容为:hello
nextLine():
- 以Enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符。
- 可以获得空白
public class Demo02 {
public static void main(String[] args) {
//创建一个扫描器对象,用于接收键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收:");
//判断用户有没有输入字符串
if(scanner.hasNextLine()){
//使用next方式接收
String str=scanner.nextLine();//程序会等待用户输入完毕
System.out.println("输入的内容为:"+str);
}
//凡是属于IO流的类如果一直不关闭会一直占用资源,要养成好习惯用完就关闭
scanner.close();
}
}
运行结果
使用next方式接收:
hello world!
输入的内容为:hello world!
进程已结束,退出代码为 0
Scanner进阶操作:
package Scanner;
import java.util.Scanner;
public class Demo04 {
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
//从键盘接收数据
int i=0;
float f=0.0F;
System.out.println("请输入整数:");
//如果。。。那么。。。
if (s.hasNextInt()){
i = s.nextInt();
System.out.println("整数数据"+i);
}else {
System.out.println("输入的不是整数数据!:");
}
System.out.println("请输入小数");
//如果。。。那么。。。
if (s.hasNextFloat()){
f = s.nextFloat();
System.out.println("小数数据"+f);
}else {
System.out.println("输入的不是小数数据!:");
}
s.close();
}
}
正确输出
请输入整数:
10
整数数据10
请输入小数
1.1
小数数据1.1
进程已结束,退出代码为 0
请输入整数:
10.1
输入的不是整数数据!:
请输入小数
小数数据10.1
进程已结束,退出代码为 0
package Scanner;
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
System.out.println("请输入数据:");
//我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并执行结果:
Scanner s= new Scanner(System.in);
//和
double sum=0;
//计算输入了多少个数字
int m=0;
//通过循环判断是否还有输入,并在里面对每一次进行求和和统计
while (s.hasNextDouble()){
double x = s.nextDouble();
m++;
sum+=x;
System.out.println("你输入了第"+m+"个数据,然后当前结果为sum="+sum);
}
System.out.println(m+"个数的和为:"+sum);
System.out.println(m+"个数的平均值为:"+(sum/m));
s.close();
}
}
运行结果
请输入数据:
10
你输入了第1个数据,然后当前结果为sum=10.0
20
你输入了第2个数据,然后当前结果为sum=30.0
30
你输入了第3个数据,然后当前结果为sum=60.0
40
你输入了第4个数据,然后当前结果为sum=100.0
x
4个数的和为:100.0
4个数的平均值为:25.0
顺序结构
-
Java的基本结构就是顺序结构,除非特别指名,否则就按照顺序一句一句执行。
-
顺序结构是最简单的算法结构。
-
语句与语句之间,框与框之间是按从上到下的顺序执行的,它是由若干个依次执行的处理步骤组成的,它是任何一个算法都离不开的一种基本算法结构。
package Struct;
public class Demo01 {
public static void main(String[] args) {
//按照从上到下的顺序依次执行
System.out.println("hello1");
System.out.println("hello2");
System.out.println("hello3");
System.out.println("hello4");
System.out.println("hello5");
}
}
hello1
hello2
hello3
hello4
hello5
选择结构
package Struct;
import java.util.Scanner;
public class IfDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容:");
String s = scanner.nextLine();
//判断字符串是否相等,字符串不能用==进行判断
if (s.equals("hello")){
System.out.println(s);
}
System.out.println("End");
scanner.close();
}
}
请输入内容:
hello
hello
End
进程已结束,退出代码为 0
//输入不为hello时
请输入内容:
x
End
进程已结束,退出代码为 0
public class IfDemo02 {
public static void main(String[] args) {
//考试分数大于六十分就是及格,小于六十就是不及格。
Scanner s= new Scanner(System.in);
System.out.println("请输入成绩:");
int score = s.nextInt();
if (score>60){
System.out.println("及格");
}
else {
System.out.println("不及格");
}
s.close();
}
}
请输入成绩:
80
及格
请输入成绩:
33
不及格
public class IfDemo03 {
public static void main(String[] args) {
//考试分数大于六十分就是及格,小于六十就是不及格。
Scanner s= new Scanner(System.in);
System.out.println("请输入成绩:");
int score = s.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("成绩不合法");
}
s.close();
}
}
请输入成绩:
4
不及格
请输入成绩:
100
恭喜满分
请输入成绩:
102
成绩不合法
if语句最多有一个else语句,else语句在所有的else if 语句之后。
if语句可以有若干个else if语句,它们必须在else语句之前。
一旦其中一个else if语句检测为true,其他的else if以及else语句都将跳过执行。
cse穿透
public class SwitchDemo01 {
public static void main(String[] args) {
//case穿透
char grade='C';
switch (grade){
case 'A':
System.out.println("优秀");
case 'B':
System.out.println("良好");
case 'C':
System.out.println("及格");
case 'D':
System.out.println("再接再厉");
case 'E':
System.out.println("不及格");
}
}
}
及格
再接再厉
不及格
进程已结束,退出代码为 0
public class SwitchDemo01 {
public static void main(String[] args) {
//case穿透
char grade='f';
switch (grade){
case 'A':
System.out.println("优秀");
case 'B':
System.out.println("良好");
case 'C':
System.out.println("及格");
case 'D':
System.out.println("再接再厉");
case 'E':
System.out.println("不及格");
default:
System.out.println("未知等级");
}
}
}
未知等级
进程已结束,退出代码为 0
为了防止case穿透,我们使用break关键字
public class SwitchDemo01 {
public static void main(String[] args) {
//case穿透
char grade='C';
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;
}
}
}
优秀
//很好的防止了case穿透
循环结构
public class WhileDemo01 {
public static void main(String[] args) {
//输出0~100
int i=0;
while (i<100){
i++;
}
System.out.println(i);
}
}
运行结果
100
public class WhileDemo03 {
public static void main(String[] args) {
//计算1+2+3+...+100
int i=0;
int sum=0;
while (i<=100){
sum+=i;
i++;
}
System.out.println(sum);
}
}
结果为
5050
//while与do while的区别
public class DoWhileDemo01 {
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
public class ForDemo02 {
public static void main(String[] args) {
//练习1:计算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
2450
public class ForDemo03 {
public static void main(String[] args) {
//练习2:用while或for循环输出1——1000之间可以被五整除的数,并且每行输出3个
for (int i = 0; i <= 1000; i++) {
if (i % 5 == 0) {
System.out.print(i + "\t");
}
if (i % (5 * 3) == 0) {//每行
System.out.println();
}
}
}
}
0
5 10 15
20 25 30
35 40 45
50 55 60
65 70 75
80 85 90
95 100 105
110 115 120
125 130 135
140 145 150
155 160 165
170 175 180
185 190 195
200 205 210
215 220 225
230 235 240
245 250 255
260 265 270
275 280 285
290 295 300
305 310 315
320 325 330
335 340 345
350 355 360
365 370 375
380 385 390
395 400 405
410 415 420
425 430 435
440 445 450
455 460 465
470 475 480
485 490 495
500 505 510
515 520 525
530 535 540
545 550 555
560 565 570
575 580 585
590 595 600
605 610 615
620 625 630
635 640 645
650 655 660
665 670 675
680 685 690
695 700 705
710 715 720
725 730 735
740 745 750
755 760 765
770 775 780
785 790 795
800 805 810
815 820 825
830 835 840
845 850 855
860 865 870
875 880 885
890 895 900
905 910 915
920 925 930
935 940 945
950 955 960
965 970 975
980 985 990
995 1000
进程已结束,退出代码为 0
使用while循环
public class ForDemo03 {
public static void main(String[] args) {
//练习2:用while或for循环输出1——1000之间可以被五整除的数,并且每行输出3个
int i=0;
int k=3;
while (i<=1000){
if (i%5==0) {
System.out.print(i+"\t");
}
i++;
if(i%15==0) {
System.out.println();
}
}
}
}
打印99乘法表
public class ForDemo04 {
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
进程已结束,退出代码为 0
public class ForDemo05 {
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
public class BreakDemo01 {
public static void main(String[] args) {
int i=0;
while (i<100){
i++;
System.out.println(i);
if (i==30){
break;
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class ContinueDemo01 {
public static void main(String[] args) {
int i=0;
while (i<100){
i++;
if(i%10==0){
System.out.println();
continue;
}
System.out.print(i+"\t");
}
}
1 2 3 4 5 6 7 8 9
11 12 13 14 15 16 17 18 19
21 22 23 24 25 26 27 28 29
31 32 33 34 35 36 37 38 39
41 42 43 44 45 46 47 48 49
51 52 53 54 55 56 57 58 59
61 62 63 64 65 66 67 68 69
71 72 73 74 75 76 77 78 79
81 82 83 84 85 86 87 88 89
91 92 93 94 95 96 97 98 99
Java中go to语句的影子
public class LableDemo01 {
public static void main(String[] args) {
//打印101~105之间的质数
int count=0;
outer:for (int i=101;i<150;i++){
for (int j=2;j<i/2;j++){
if (i%j==0){
continue outer;
}
}
System.out.print(i+" ");
}
}
}
101 103 107 109 113 127 131 137 139 149
进程已结束,退出代码为 0
利用continue outer可以跳到指定的语句
练习
打印三角形
public class TestDemo01 {
public static void main(String[] args) {
//打印三角形 5行
for (int i = 1; i <=5; i++) {
for (int j=5;j>=i;j--){
System.out.printf(" ");
}
for (int j=1;j<=i;j++){
System.out.print("*");
}
for (int j=1;j<i;j++){
System.out.print("*");
}
System.out.println();
}
}
}
*
***
*****
*******
*********
特别感谢:
附上视频地址
狂神yyds
https://www.bilibili.com/video/BV12J41137hu?p=30