java的流程控制
目录
-
用户交互Scanner
-
顺序结构
-
选择结构
-
循环结构
-
break & continue
-
练习
一、Scanner对象
之前我们学的基本语法中我们并没有实现程序和人的交互,但是Java给我们提供了这样一个工具类,我们可以获取用户的输入。java.util.Scanner是java5的新特征,我们可以通过Scanner类来获取用户的输入。
基本语法:
Scanner s = new Scanner(System.in);
通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前我们一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据
//从键盘接收数据 Scanner san = new Scanner(System.in); System.out.println("使用nextLine方式接收"); //判断是否还有输入 if (san.hasNextLine() == true){ String str = san.nextLine(); System.out.println("输出类容为:" + str); } san.close();//用完关闭
//创建一个扫描对象,用于接受键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收:");
//判断用户有没有输入字符串
if (scanner.hasNext() == true){
//使用next方式接受
String str = scanner.next(); //程序会等待用户输入完毕
System.out.println("输入的类容为:" + str);
}
//凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
scanner.close(); //关闭scanner
next():
-
一定要读取到有效字符后才可以结束输入。
-
对输入有效字符之前遇到的空白,next()方法会自动将其去掉。
-
只有输入有效字符后才将后面输入的空白作为分隔符或者结束符。
-
next()不能得到带有空格的字符串。
nextLine():
-
以Enter为结束符,也就是说nextTine()方法返回的是输入回车之前的所有字符。
-
可以获得空白。
{
Scanner scanner = new Scanner(System.in);
//键盘接收数据
int i = 0;
float f= 0.0f;
System.out.println("请输入整数:");
//if...else 如果...那么
if (scanner.hasNextInt() == true){
i = scanner.nextInt();
System.out.println(i);
}else{
System.out.println("输入的不是整数!");
}
if (scanner.hasNextFloat() == true){
f = scanner.nextFloat();
System.out.println(f);
}else{
System.out.println("输入的不是整数!");
}
scanner.close();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//和
double sum = 0f;
//计算输入了多少个数字
int m = 0;
System.out.println("请输入数据:");
//通过循环判断是否还有输入,并在里面对每一次进行求和和统计
while (scanner.hasNextDouble()){
double x = scanner.nextDouble();
//输入完一个数 用 m自己 +1
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 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 static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入内容:"); String str = scanner.nextLine(); //equals:判断字符串是否相等 if (str.equals("Hello")){ System.out.println(str); } System.out.println("end"); scanner.close();
-
if双选择结构
那现在有个需求,公司要收购一个软件,成功了,给人支付1001万元,失败了,自己找人开发。
这样的需求用一个if就搞不定了,我们需要有两个判断,需要一个双选择结构,所以就有了if-else结构
语法: if(){ //如果布尔值表达式值为true }else{ //如果布尔值表达式为false }
public static void main(String[] args) { //考试分数大于60分就是及格,小于就是不及格 Scanner scanner = new Scanner(System.in); System.out.println("请输入分数:"); int score = scanner.nextInt(); //score:成绩 if (score >= 60){ System.out.println("及格"); }else{ System.out.println("不及格"); } scanner.close(); }
-
if多选择结构
我们发现刚才的代码不符合实际情况,真实的情况还可能存在ABCD选项等,存在区间多级判断。比如90-100就是A,80-90就是B,80一下就是C等等,再生活中我们很多时候的选择也不仅仅只有两个,所以我们需要一个多选择结构来处理这类问题!
if(布尔值表达式1){ //如果布尔值表达式1的值为true执行代码 }else if(布尔值表达式2){ //如果布尔值表达式2的值为true执行代码 }else if(布尔值表达式3){ //如果布尔值表达式3的值为true执行代码 }else{ //如果布尔值表达式都不为true执行代码 }
public static void main(String[] args) { //获取输入值Scanner Scanner scanner = new Scanner(System.in); /* if语句至少有1个else语句,else语句在所有else if 语句之后。 if语句可以有若干个else if语句,它们必须在 else 之前。 一旦其中一个 else if 语句检测为 true ,其他的 else if 以及 else 语句都将跳过执行。 */ System.out.println("请输入成绩:"); int score = scanner.nextInt(); //如果成绩是100分 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或者 else if 语句中使用 if 或者 else if 语句。你可以像 if 语句嵌套 else if … else。
语法: if(布尔值表达式1){ //如果布尔值表达式的值为true执行代码 if(布尔值表达式2){ //如果布尔值表达式的值为true执行代码 } }
-
switch多选择结构
多选择结构还有一个实现方式就是switch case语句。
switch case 语句判断一个变量与一系列中某个值是否相等,每个值称为一个分支。
语句: switch(expression){ case value: //语句 break;//可选 case calue: //语句 break;//可选 //你可以有任意数量的case语句 default://可选 //语句 }//每个case后面都要跟上break结束,不然语句会穿透依次执行。
switch语句中的变量类型可以是:
-
byte 、 short 、 int或者char类型。
-
switch 支持字符串String类型。
-
同时case标签必须为字符串常量或者字面量。
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 static void main(String[] args) {
String name = "大哥";
switch(name){
case "龙华望":
System.out.println("龙华望");
break;
case "小龙":
System.out.println("小龙");
break;
default:
System.out.println("输入错误!");
}
}
四、循环结构
while 循环
while是最基本的循环,它的结构为:
while(布尔表达式){
//循环内容
}
1.只要布尔表达式为true,循环就会一直执行下去。
2.我们大多数情况会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环。
3.少部分情况需要循环一直执行,比如服务器的请求响应监听等。
4.循环条件一直为true就会造成无限循环【死循环】,我们正常的业务编程中应该尽量避免死循环。会影响程序性能或造成程序卡死崩溃!
//输出 1-100
int i = 0 ;
while(i<100){
i++;
System.out.println(i);
}
}
public static void main(String[] args) {
//计算1+2+3+...+100=?
int i = 0;
int sum = 0;
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 先判断后执行。dowhile是先执行后判断!
//Do...while总是保证循环体会被至少执行一次!这是他们的主要差别。
public static void main(String[] args) {
int i = 0;
int sum = 0;
do {
sum = sum + i;
i++;
}while(i <= 100);
System.out.println(sum);
}
}
public static void main(String[] args) {
int i = 0;
while(i<0){
System.out.println(i);
i++;
}
do {
System.out.println(i);
i++;
}while(i<0);
}
for 循环
//虽然所有循环都可以用while或者都do while来表示,但Java提供了另一种语句---for循环,使一些循环结构变得更加简单。
//for循环语句是支持迭代的一种通用结构,是最有效,最灵活的循环结构。
//for循环执行的次数是在执行前就确定的。语法格式如下
for(初始化;布尔表达式;更新){
//代码语句
}
{
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 <= 100; i++) {
System.out.println(i);
}
System.out.println("for循环结束!!!");
}
}
/*
关于for循环有以下几点说明:
最先执行初始化步骤,但可初始化一个或多个循环控制变量,可以是空语句。
然后,检测布尔表达式的值。如果为true,循环被执行。如果为false,循环终止,开始执行循环体后面的语句。
执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。
再次检测布尔表达式。循环执行上面的过程。
*/
//计算1-100之间所有奇数跟偶数的和
int j = 0;
int o = 0;
for (int i = 0; i < 100; i++) {
if (i%2!=0){
j = j+i;
}else{
o = o+i;
}
}
System.out.println("奇数和为:" + j);
System.out.println("偶数和为:" + o);
}
public static void main(String[] args) {
//用while或者for循环输出1-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();
}
}
//println 输出完会换行
//print 输出完不会换行
}
public static void main(String[] args) {
//打印出一个九九乘法表
/*
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
*/
//1.我们先打印第一列
//2.我们在吧固定的1用一个循环包起来
//3.去掉重复项,i <= j;
//4.调整样式
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();
}
}
增强for循环
在数组我们会重点使用!
它是从Java5开始,引入了一种主要用于数组或集合的增强型for循环。
Java增强for循环语句格式如下
for(声明语句 : 表达式){
//代码句子
}
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
public static void main(String[] args) {
int[] sz = {10,20,30,40};
for (int i = 0; i < 4;i++){
System.out.println(sz[i]);
}
System.out.println("========================================");
//遍历数组里的元素
for(int x:sz){
System.out.println(x);
}
}
五、break & continue
break:
break语句在任何循环语句的主体部分,均可以用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)
public static void main(String[] args) {
int i = 0;
while(i<100){
i++;
System.out.println(i);
if (i == 30){
break;
}
}
}
continue:
continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
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);
}
}
//练习:
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();
}