文章目录
源自视频课程的本人总结,推荐课程链接
Scanner对象
- java.util.Scanner特征,通过Scanner类获取用户的输入,实现人机交互
- 基本语法:
Scanner s = new Scanner(System.in);
- Scanner类可以通过next()和nextLine()获取输入的字符串,在读取前一定要使用hasNext()和hasNextLine()判断是否还有输入的数据
- next()
package Scanner;
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
//Scanner接收键盘输入
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收");
//判断是否有输入
if (scanner.hasNext()){ //输入内容为: hello wrold
String s = scanner.next(); //自动消除合法字符之前的空格,只要识别到合法字符之后遇见空格,则停止输入识别
System.out.println("输出的内容为:"+s); //hello
}
scanner.close();
}
}
- nextLine()
package Scanner;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
//接收键盘输入
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextline接收:");
//判断输入
if (scanner.hasNextLine()){ //输入内容为: hello wrold
String s = scanner.nextLine(); //nextLine不会消除前后的空格
System.out.println("输出内容为:"+s); // hello wrold
}
scanner.close();
}
}
- next()与nextLine()区别:
- next()一定要接收到有效字符后擦可以结束;
- next()对输入有效字符之前遇到的空白,会自动将空白抹去;
- next()只有在输入有效字符之后才将其后面输入的空白作为分隔符或结束符;
- next()不能得到带有空格的字符串
- nextLine()以Enter为结束符,可获得所有回车之前的字符,包括空白。
- Scanner类的其他方法
示例1:整数和小数,if…else
package Scanner;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
int i = 0;
float f = 0.0f;
//创建Scanner类接收键盘输入
Scanner scanner = new Scanner(System.in);
System.out.println("请输入整数");
//判断输入整数
if (scanner.hasNextInt()){
i = scanner.nextInt();
System.out.println("整数是:"+i);
}else{
System.out.println("输入的不是整数!");
}
System.out.println("请输入小数:");
//判断输入小数
if (scanner.hasNextFloat()){ //float类型高于int,因此存在自动转换
f = scanner.nextFloat();
System.out.println("小数是:"+f);
}else{
System.out.println("输入的不是小数!");
}
scanner.close();
}
}
示例2:输入多个数,while
package Scanner;
import java.util.Scanner;
public class Demo04 {
public static void main(String[] args) {
//计算输入的多个数的平均值,且每个输入数以回车确认数字输入,以非数字结束输入并执行输出结果
double sum = 0; //计算和
int m = 0; //统计输入数的个数
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数:");
while (scanner.hasNextDouble()){ //double类型最高
double x = scanner.nextDouble();
m = m + 1; //m++
sum = sum + x;
System.out.println("你输入第:"+m+"个数据,然后当前sum=:"+sum);
} //以非数字字符结束while循环
System.out.println(m +"个数的和为:"+sum);
System.out.println(m +"个数的平均值为:"+(sum/m));
scanner.close();
}
}
流程控制
1. 顺序结构
2. 选择结构:单选择结构、双选择结构、嵌套的if结构、Switch选择结构
A. 单选择结构:if…
package XunHuan;
import java.util.Scanner;
public class IfDemo01 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String s1 = s.nextLine();
if (s1.equals("hello")){
System.out.println(s1);
}
System.out.println("End");
s.close();
}
}
B. 双选择结构:if … else…
package XunHuan;
import java.util.Scanner;
public class IfDemo02 {
public static void main(String[] args) {
//判断成绩是否及格
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩");
double score = scanner.nextDouble();
if (score>=60){
System.out.println(score+"是及格的");
}else{
System.out.println(score+"是不及格的");
}
scanner.close();
}
}
C. 多选择结果:if…else if … else if…else if…else
package XunHuan;
import java.util.Scanner;
public class IfDemo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
//等级判断,注意逻辑的严密性
if (score==100){
System.out.println("恭喜满分!");
}else if(score>90 && score<100){
System.out.println("A级");
}else if(score>80 && score<=90){
System.out.println("B级");
}else if(score>70 && score<=80){
System.out.println("A级");
}else if(score>=60 && score<=70){
System.out.println("A级");
}else if(score<60 && score>=0){
System.out.println("不及格");
}else{
System.out.println("输入成绩不合法!");
}
scanner.close();
}
}
D. 嵌套的if结构
if(布尔值){
//布尔值为True
if(布尔值){
//布尔值为True
}
}
E. switch多选择结构
switch(expression){
case value: //value必须为常量
//语句
break; //可选
case value:
//语句
break; //可选
//可以有任意数量的case语句
default://可选
//语句
}
示例1:
package XunHuan;
public class SwitchDemo01 {
public static void main(String[] args) {
char grade = 'C';
//case具有穿透力,如果不加break,则穿透显示
//Switch:匹配一个具体的值
switch (grade){
case 'A':
System.out.println('A');
break;
case 'B':
System.out.println('B');
break;
case 'C':
System.out.println('C');
break;
case 'D':
System.out.println('D');
break;
default:
System.out.println("未知等级");
}
}
}
示例2:java7之后Switch支持字符串String类型
package XunHuan;
public class SwitchDemo02 {
public static void main(String[] args) {
String name = "小明";
switch (name){
case "小明":
System.out.println("A");
break;
case "小红":
System.out.println("B");
break;
default:
System.out.println("出错");
}
}
}
拓展: 反编译(IDEA),字符的本质还是数字
第一步:打开项目结构,找到.class位置
第二步:将.class文件复制到该文件夹下,注意以文件夹复制,而不是IDEA上复制,否则复制不过去
第三步:对比反编译和源代码–>hashCode反编译
3. 循环结构:while、do…while、for、break&continue
A. while
while(布尔表达式){
//循环 内容
}
- 只要布尔表达式一直为true,循环将一直执行下去;
- 通常情况下,会让设置表达式失效来结束循环;避免死循环
- 少部分情况下,需要一直执行下去,例如服务器请求监听;while(true){…}
示例1:while循环输出1~100
package XunHuan;
public class WhileDemo01 {
public static void main(String[] args) {
int i = 0;
//输出 1~100
while (i<100){
i ++; //迭代,避免死循环的表达式失效
System.out.println(i);
}
}
}
示例2:一直循环下去,例如服务器请求反应
package XunHuan;
public class WhileDemo02 {
public static void main(String[] args) {
//服务器请求反应
while(true){
//...
}
}
}
示例3:计算1+2+3+…+100
package XunHuan;
public class WhileDemo03 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
while (i<100){
i ++;
sum += i;
}
System.out.println("输出1+2+...+100的结果是:"+sum);
}
}
B. do…while
do{
//执行代码
}while(布尔表达式);
- do…while循环至少会被执行一次
- do…while与while的区别:①do…while先执行后判断,while先判断后执行;②do…while可以保证循环体至少被执行一次(主要区别)
示例1:计算1+2+3+…+100
package XunHuan;
public class DoWhileDemo01 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
//计算1+...+100的结果
do {
i++;
sum+=i;
}while(i<100);
System.out.println(sum);
}
}
示例2:对比while和do…while
package XunHuan;
public class DoWhileDemo02 {
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在分割线下输出
}
}
C.for循环
for(初始化;布尔表达式;更新){
//代码语句
}
- for循环流程:①首次执行初始化,可声明一种类型,但可初始化多个循环控制变量,也可以为空语句;②然后执行布尔表达式,判断为true,则执行循环体,false循环终止,也可以为空语句表示始终true;③执行一次循环之后,更新循环控制变量;再次检测布尔表达式,重复②③。
- 死循环
for(;;;){
//
}
示例1:for循环输出1-100
package XunHuan;
public class ForDemo01 {
public static void main(String[] args) {
//输出1-100
for(int i=1;i<=100; i++){ //快捷键100.for回车自动生成for语句
System.out.println(i);
}
}
}
示例2:计算0-100之间所有奇数和偶数的和
package XunHuan;
public class ForDemo02 {
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+" "+evenSum); //2500 2550
}
}
示例3:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
package XunHuan;
public class ForDemo03 {
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"); //数之间空tab
}
if (i%(5*3)==0){ //每行三个
System.out.println();
//System.out.print("\n");
}
}
//println输出完会换行
//print输出完不会换行
}
}
示例4:打印九九乘法表
package XunHuan;
/*
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
*/
public class ForDemo04 {
public static void main(String[] args) {
//打印九九乘法表
int ji = 0;
for (int i = 1; i <= 9; i++) { //第二个数,也是行
for (int j = 1; j <= i; j++){ //第一个数 ,也是列
ji = i * j;
System.out.print(j+"*"+i+"="+ji+"\t");
}
System.out.println();
}
}
}
D. 增强for循环,引用数据和集合
for(声明语句:表达式 ){
//执行语句
}
示例:增强for循环-数组
package XunHuan;
public class ForDemo05 {
public static void main(String[] args) {
//定义数组
int [] numbers = {10,20,30,40,50};
//for循环输出
for (int i = 0; i < 5; i++) {
System.out.println(numbers[i]);
}
System.out.println("============");
//增强for循环
for (int x:numbers){
System.out.println(x);
}
}
}
E.break&continue
- break用于while和switch,强制退出该循环,不执行循环中其他语句;
- continue:终止某次循环,即跳出循环体中尚未执行的语句,接着下一次是否执行循环的判定。
练习
打印大三角形
分析:先画出第1步,再画第2步,再画第3步(如图)
实现:
package XunHuan;
public class TestDemo {
public static void main(String[] args) {
//打印5行三角形
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--){ //实现第1步
System.out.print(" ");
}
for (int j = 1; j <= i; j++){ //实现第2步
System.out.print("*");
}
for (int j = 1; j < i; j++){ //实现第3步
System.out.print("*");
}
System.out.println();
}
}
}