一、用户交互Scanner
1、next(使用较少)
public static void main(String[] args) {
//从键盘接收数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收:");
//判断用户有没有输入字符串
if(scanner.hasNext()){
String str = scanner.next();//程序会等待用户输入完毕
System.out.println(str);
}
//凡是属于IO流的类如果不关闭会一直占用资源,要养成良好的习惯
scanner.close();
}
控制台:
使用next方式接收:
hello world //键盘输入内容
hello //输出内容
hello world //键盘输入内容
hello //输出内容
输入内容和输出内容不一致原因:
next():
(1)一定要读取到有效字符才可以结束输入,如果输入一行空白,控制台位置也不会结束输入
(2)在读取到有效字符以后,如果字符之前有空格,会自动去掉。如 hello则只会读取hello而没有空格部分
(3)在读取有效字符以后,如果有空格,会自动截取空格之前的字符,空格后的字符会自动去掉。如hello world则只会读取hello,即next()不能得到带空格的字符串
2、nextLine(使用较多)
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 world //键盘输入内容
hello world //输出内容
nextLine():
(1)会读取到按Enter键之前的所有字符串
(2)只输入一行空格也可以读取到
3、除此之外,还有nextInt、nextFloat、nextDouble等等
例子:输入多个数字,求和并求均值,每输入一个数字用回车确认,通过输入非数字字符来结束输入并输出结果
public static void main(String[] args) {
//输入多个数字,求和并求均值,每输入一个数字用回车确认,通过输入非数字字符来结束输入并输出结果
Scanner scanner = new Scanner(System.in);
//和
double sum = 0;
//个数
int m = 0;
System.out.println("输入数字:");
//循环获取
while (scanner.hasNextDouble()){
//用x接收输入的数字
double x = scanner.nextDouble();
m = m + 1;
sum = sum + x;
System.out.println("当前为第"+m+"个数,和为"+sum);
}
System.out.println(m+"个数的和为:"+sum);
System.out.println(m+"个数的均值为:"+(sum/m));
scanner.close();
}
结果为:
输入数字:
12
当前为第1个数,和为12.0
65
当前为第2个数,和为77.0
a
2个数的和为:77.0
2个数的均值为:38.5
二、Switch选择结构
case穿透现象:
public static void main(String[] args) {
String 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现象即为穿透现象。
原因:当case内没有break时,switch会将对应case及其后面所有的case项都走一遍,本例即从case “C”、case "D"都会过一遍,所以会输出及格和不及格
public static void main(String[] args) {
String 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 “C”,然后跳出选择结构。因此,凡是有case时,一定要写break。
三、循环结构
1、while、do…while区别
while:会先判断,符合条件才执行循环内语句
do…while:会先执行一次循环体,再做判断
public static void main(String[] args) {
/**
* while、do...while区别
*/
int i = 0;
while (i<0){
System.out.println(i);
}
System.out.println("==========");
do {
System.out.println(i);
}while (i<0);
}
结果为:
==========
0
由上述例子可知,执行的是下方do…while语句,因为while语句不成立也就不会执行,而do…while先执行do内语句再用while判断。
2、死循环
死循环的两种写法:
while(true){} //while的死循环写法
for(;;){} //for的死循环写法,括号内只有分号
3、例题:打印九九乘法表
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();
}
}
4、break、continue
- break:强制退出循环,不再执行循环中剩余的部分
- continue:终止某次循环,即该次循环中后面的部分不再循环,然后接着执行下一次的循环
见代码:
break:
public static void main(String[] args) {
int i = 0;
while (i<10){
i++;
if(i%3==0){
break;
}
System.out.println(i);
}
}
此时结果为:1,2
原因:break会强制跳出循环,当i=3时,符合if判断句,执行break,直接退出循环,循环结束。
continue
public static void main(String[] args) {
int i = 0;
while (i<10){
i++;
if(i%3==0){
continue;
}
System.out.println(i);
}
}
此时结果为:1、2、4、5、7、8、10
原因:当i=3,6,9时,符合if判断句,执行continue,跳出本次循环,不再执行后面的sout语句,直接跳到循环最开始即i++部分再次开始执行。
5、练习题:打印三角形
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();
}
}