Java流程控制
Java流程控制
用户交互Scanner
- 之前我们学的基础语法中我们并没有实现程序和人的交互,但是Java给我们提供了这样一个工具类,我们可以获取用户的输入。java.util.Scanner是Java5的新特征,我们可以通过Scanner类来获取用户的输入。
- 基本语法:
Scanner s = new Scanner(System.in)
- 通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前我们一般需要使用hasNext()与hasNextLine()判断是否还与输入的数据。
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();
}
}
public class Demo02 {
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();
}
}
-
next():
- 一定要读取到有效字符后才可以结束输入
- 对输入有效字符之前遇到的空白,next()方法会自动将其去掉
- 只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符
- next()不能得到带有空格的字符串
-
nextLine():
- 以Enter为结束符,也就是说,nextLine()方法返回的是输入回车之前的所有字符
- 可以获得空白
public class Demo03 {
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();
}
}
public class Demo04 {
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 Demo05 {
public static void main(String[] args) {
//我们可以输入多个数字,并求其和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果:
Scanner scanner = new Scanner(System.in);
//和
double sum = 0;
//计算输入了多少个数字
int m = 0;
//通过循环判断是否还有输入,并在里面对每一次进行求和和统计
while(scanner.hasNextDouble()){
double x = scanner.nextDouble();
m = m + 1;
sum = sum + x;
System.out.println("你输入了第"+m+"个数据,然后当前的结果sum为"+sum);
}
System.out.println(m+"个数的和为"+sum);
System.out.println(m+"个数的平均值为"+(sum/m));
scanner.close();
}
}
顺序结构
- JAVA的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句的执行
- 顺序结构是最简单的算法结构
- 语句与语句之间按从上到下的顺序进行,它是由若干依次执行的处理步骤组成的,它是任何一种算法都离不开的一种基本算法结构。
public class ShunXuDemo {
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");
}
}
选择结构
if单选择结构
判断一个东西是否可行,然后我们才去执行,这一过程在程序中用if语句来表示
语法:
if(布尔表达式){
//如果布尔表达式为true将执行的语句
}
public class IfDemo01 {
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();;
}
}
if双选择结构
- 语法:
if(布尔表达式){
//如果布尔值为true
}else{
//布尔值为false
}
public class IfDemo02 {
public static void main(String[] args) {
//考试分数大于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();;
}
}
if多选择结构
if(布尔表达式 1){
//如果布尔表达式1的值为true执行代码
}else if(布尔表达式 2){
//如果布尔表达式2的值为true执行代码
}else if(布尔表达式 3{
//如果布尔表达式3的值为true执行代码
}else{
//如果以上表达式都不正确执行代码
}
public class IfDemo03 {
public static void main(String[] args) {
//考试分数大于60几个,否则不及格
Scanner scanner = new Scanner(System.in);
/*
* if语句至多有一个else语句,else语句在所有else语句之后
* if语句可以有多个else if 语句,他们必须在else语句之前
* 一旦有一个else if语句执行为true,后面的else if语句与else语句都将跳过执行*/
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();;
}
}
嵌套的if结构
嵌套的if…else语句是合法的
语法:
if(布尔表达式 1){
///如果布尔表达式1的值为true执行代码
if(布尔表达式2){
///如果布尔表达式2的值为true执行代码
}
}
switch多选择结构
- 多选择结构还有另一种实现方式就是switch case语句
- switch case语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支
switch(expression){
case value :
//语句
bresk;//可选
case value :
//语句
bresk;//可选
……;
default://可选
//语句
}
- switch语句中的变量类型可以是:
- byte,short,int或者char
- 从Java SE 7开始,支持String类型了
- 同时case标签必须为字符串常量或者字面量
public class SwitchDemo01 {
public static void main(String[] args) {
//case穿透(break)//switch匹配一个具体的值
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;//可选
default:
System.out.println("未知等级");
}
}
}
public class SwitchDemo02 {
public static void main(String[] args) {
String str = "良好";
//JDK7的新特性,表达式结果可以是字符串
//字符的本质还是数字
//反编译 java ----class(字节码文件)----反编译(IDEA)
//idea反编译:找到class文件放到IDEA中
switch (str){
case "优秀":
System.out.println("优秀");
break;//可选
case "良好":
System.out.println("良好");
break;//可选
default:
System.out.println("未知等级");
}
}
}
循环结构
-
while循环
- while是最基本的循环,它的结构为:
while(布尔表达式){
//循环内容
}- 只要布尔值为true,循环就会一直只想下去
- 我们大多数情况会让循环停下来的,我们需要一个让表达式失效的方式来结束循环
- 少部分情况需要循环一致执行下去,比如服务器的请求响应监听等
- 循环条件一直为true就会造成无限循环【死循环】,我们正常的业务编程中应该精良避免死循环,会影响程序性能或者造成程序卡死崩溃
public class WhileDemo01 {
public static void main(String[] args) {
//输出1-100
int i = 0;
while (i < 100){
i++;
System.out.println();
}
}
}
public class WhileDemo02 {
public static void main(String[] args) {
//死循环
while (true){
//等待客户端连接
//定时检查
//……
}
}
}
public class WhileDemo03 {
public static void main(String[] args) {
//计算1+2+3+...+100=?
int i = 0;
int sum = 100;
while (i <= 100){
sum = sum +i;
i++;
}
System.out.println(sum);
}
}
-
do…while循环
- 对于while语句而言,如果不满足条件,则不会静茹循环,但有时候我们需要即使不满足条件也需要执行一次
- do……while循环与while循环相似,不同的是,do……while循环至少会循环一次
do{
//循环语句
}while(布尔表达式);
- while 和do……while的区别
- while先判断后执行。do……while限制性后判断
- do……while总是保证循环体至少被执行一次,这是他们的主要差别
public class DoWhileDemo01 {
public static void main(String[] args) {
//计算1+2+3+...+100=?
int i = 0;
int sum = 100;
do{
sum = sum +i;
i++;
}while (i <= 100);
System.out.println(sum);
}
}
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);
}
}
-
For循环
-
虽然所有的循环都可以用while和do……while表示,但java提供了另一种语句——for循环,使一些循环结构变得简单。
-
for循环语句是支持迭代的一种通用结构,是最有效,最灵活的循环结构
-
for循环执行的次数在执行前就确定了。语法格式如下
for(初始化;布尔表达式;更新){
//代码语句
}
-
联系1:计算0-100之间的奇数和偶数之和
-
练习2:用while或for循环输出0-1000之间能被5整除的数,并且每行输出3个
-
练习3:打印99乘法表
public class ForDemo01 { public static void main(String[] args) { int a = 1;//初始化条件 while(a<=100){//条件判断 System.out.println(a);//循环体 a+=2;//迭代 } System.out.println("while循环结束!"); //初始化值 条件判断 迭代 for(int i = 1;i<=a;i++){ System.out.println(i); } System.out.println("for循环结束!"); } /* 关于for循环有一下几点说明: 最先执行初始化步骤,可以声明一种类型,但可初始化一个或者多个循环控制变量,也可以是空语句 然后,检测布尔表达式的值,如果为true,循环体被执行,如果为false,循环终止,开始执行循环后的语句。 再次检查布尔表达式,循环执行上面的过程。 */ //for(;;)死循环 }
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); } }
public class ForDemo03 { public static void main(String[] args) { //用while或for循环输出0-1000之间能被5整除的数,并且每行输出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(); System.out.print("\n"); } } //print输出完不换行 //println输出完换行 } }
public class ForDemo04 { public static void main(String[] args) { //打印99乘法表 //1.先打印第一列 //2.我们把固定的1再用一个循环包起来 //3.去掉重复项,i<j //4.调整样式 for (int j = 0; j < 9; j++) { for (int i = 0; i < j; i++) { System.out.print(j+"*"+i+"="+(j*i)+"\t"); } System.out.println(); } } }
-
-
增强for循环
- 在java5中引入了一种主要用于数组的增强型for循环
- java增强for循环语法格式如下:
for(声明语句 :表达式)
{
//代码句子
}
- 声明语句:声明新的局部变量,该变量类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
- 表达式:表达式是要访问的数组名,或者是返回为数组的方法
public class ForDemo05 { public static void main(String[] args) { int[] numbers = {10,20,30,40,50};//定义了一个数组 //遍历数组的元素 for(int x:numbers){ System.out.println(x); } } }
break&continue
-
break在任何循环语句的主题部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中的剩余语句。(break语句也在switch语句中使用)
-
continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定
public class BreakDemo { public static void main(String[] args) { int i = 0; while(i<100){ i++; System.out.println(i); if(i==30){ break; } } } }
public class ContinueDemo { 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); } } }
-
关于goto关键字
- goto关键字很早就在程序设计语言中出现。尽管goto仍是java的一个保留字,但并未在语言中得到真正的使用;java没有goto。然而,在break和continue这两个关键字身上,我们任然看出一些goto的影子---带标签的break和continue。
- “标签”是指后面跟一个冒号的标识符,例如:lable:
- 对java来说唯一用到标签的地方是在循环语句之前。而在循环之前设计标签的唯一理由是:我们希望在其中嵌套另一个循环,由于break和continue关键字通常只中断循环,但如随同标签使用,他们就会中断在存在标签的地方
public class LableDemo { 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.println(i+""); } } }
练习
打印三角形
public class TestDemo {
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();
}
}
}