java流程控制
流程控制
用户交互Scanner
可以获取用户输入
基本语法
Scanner s = new =Scanner(System.in);
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();
}
}
/*
hello world
输出
hello
*/
nextLine:
- 以Enter为结束符,也就是说nextLine()方法的是回车之前的所有字符。
- 可以获取空白
public class Demo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextLine方式接收:");
if (scanner.hasNextLine()){
//使用next方式接收
String str = scanner.nextLine();
System.out.println("输出内容为:"+str);
}
scanner.close();
}
}
/*
hello world
输出
hello world
public class Demo04 {
public static void main(String[] args) {
//new Scanner(System.in);回车
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("输入的不是小数数据:");
}
}
}
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);
System.out.println(m + "个数的平均值是"+(sum/m));
scanner.close();
}
}
顺序结构
java的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行
顺序结构是最简单的算法结构
语句与语句之间,框与框之间是按从上到下的顺序进行的,它是由若干个一次执行的处理步骤组成的,它是任何一个算法都离不开的一种基本算法结构
举列
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
*/
以顺序进行
选择结构
if单选择结构
我们很多时候需要去判断一个东西是否可行,然后我们才去执行,这样一个过程在程序中用if语句来表示
语法:
if(布尔表达式){
//如果布尔表达式为true将执行的语句
}
if双选择结构
if(布尔表达式){
//如果布尔表达式为true将执行的语句
}else{
//如果布尔表达式的值为false
}
public class IfDemo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt();
if (score > 60) {
System.out.println("及格");
} else {
System.out.println("不及格");
}
}
}
只会执行一个
if多选择结构
真实情况可能存在ABCD,存在多级判断。先执行第一个
语法:
if(布尔表达式1){
//如果布尔表达式1的值为true将执行代码
}else if(布尔表达式2){
//如果布尔表达式2的值为true将执行代码
}else if(布尔表达式3){
//如果布尔表达式3的值为true将执行代码
}else{
//如果以上布尔表达式都不为true执行代码
}
if语句至多有一个else语句,else 语句在所有的else if语句之后。
if 语句可以有若干个else if 语句,它们必须在else语句之前。
一旦其中一个else if 语句检测为true,其他的else if以及else语句都将跳过执行。
嵌套的if结构
使用嵌套的if...else语句是合法的。也就是说可以在另一个if或者else if语句中使用if或else if语句。
语法
if(布尔表达式1){
//如果布尔表达式1的值为true将执行代码
if(布尔表达式1){
//如果布尔表达式1的值为true将执行代码
}
}
switch多选择结构
多选择结构另一个实现方式就是switch case语句。
switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
switch(expression){
case value:
//语句
break;//可选
case value:
//语句
break;//可选
//你可以有任意数量的case语句
default ://可选
//语句
}
类型可以是:
byte,int,short,char
支持字符串
switch具有穿透性
所以break要加上
循环结构
while循环
是最基本的循环,结构为
while(布尔表达式){
//循环内容
}
只要布尔表达式的值为true,循环就会一直执行下去。
我们大多数情况是会让循环停止下来的,我们需要让一个表达式失效的方式来结束循环。
少部分情况需要循环一直执行,比如服务器的请求响应监听等。
循环条件一直未true就会造成无限循环(死循环),我们正常业务编程中应尽量避免死循环。会影响程序性能或者造成程序卡死崩溃!
思考:计算1+2+3+++100=?
public static void main(String[] args) {
int i = 0;
int sum = 0;
while (i<=100){//条件判断
sum = sum + i;//循环体
i++;
}
System.out.println(sum);
}
}
/*
5050
*/
do...while循环
对于while循环,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次。
do...while循环和while循环相似,不同的是,do...while循环至少会执行一次。
do{
//代码语句
}while(布尔表达式);
while和do..while的区别:
while先判断后执行,do...while是先执行后判断!
do...while总是保证循环体至少被执行一次!这是他们的主要差别
for循环
虽然所有循环结构都可以用while或do...while表示,但java提供了另一种语句-----for循环,使一些循环结构变得简单。
for循环语句是支持迭代的一种通用结构,是最有效,最灵活的循环结构。
for循环执行的次数是在实现前就确定的。语法格式如下:
for(初始化;布尔表达式;更新){
//代码语句
}
public static void main(String[] args) {
int a = 1;
for (int i=1;i<=100;i++){
System.out.println(i);
}
}
关于for循环:
最先执行初始化步骤。可以声明一种类型,但可以初始化一个或多个循环控制变量,也可以是空语句。
然后布尔表达式的值。如果是true,循环体被执行。如果为false,循环终止,开始循环后面的语句。
执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。
再次检测布尔表达式。循环执行上面的过程。
练习1:计算0到100之间的奇数和偶数的和
public static void main(String[] args) {
int oddSum = 0;
int eveSum = 0;
for (int i = 0; i <=100; i++) {
if (i%2!=0){//奇数
oddSum+=i;
}else{//偶数
eveSum+=i;
}
}
System.out.println("奇数的和:"+oddSum);
System.out.println("偶数的和:"+eveSum);
}
}
练习2:用while或for循环实处1-1000之间能被5整除的数,并输出3个
public static void main(String[] args) {
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.println("\n");
}
}
}
}
println
练习3:打印九九乘法表
public static void main(String[] args) {
//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循环
public static void main(String[] args) {
int[] numbers = {10,20,30,40,50};
for (int x:numbers){
System.out.println(x);
}
}
重点在于数组使用
for(声明语句 :表达式)
{
//代码句子
}
break & continue
break在任何循环语句的主体部分,均可用break控制循环的流程。break用于退出循环,不执行循环中剩余的语句。(可在switch中使用)
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);
}
}
break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。
continue 语句在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
练习
打印三角形
public static void main(String[] args) {
//打印三角形
for (int i = 0; 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();
}
}

浙公网安备 33010602011771号