Java流程控制

文章详细介绍了Java中Scanner类的使用,包括基础的用户交互、进阶的数字处理,以及顺序、选择(if单、双、多选择结构和switch)和循环(while、do...while、for)结构的运用。示例代码展示了如何读取用户输入、条件判断和循环控制在实际编程中的应用。
摘要由CSDN通过智能技术生成

1.用户交互Scanner

package com.song.scanner;
​
import java.util.Scanner;
​
public class Demo1 {
    public static void main(String[] args){
    //创建一个扫描器对象,用于接收键盘数据,输入hello world
    Scanner scanner = new Scanner(System.in);
    System.out.println("使用next的接收方式:");
    //判断用户有无字符串输入
        if (scanner.hasNext()){
            //使用next方式接收
            String str =scanner.next();
            System.out.println("输出的内容为:"+str);  //hello
        }
       //凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
        scanner.close();
    }
}
package com.song.scanner;
​
import java.util.Scanner;
​
public class Demo2 {
    public static void main(String[] args){
        //从键盘接收数据,输入hello world
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用nextLine的接收方式:");
       //判断是否还有输入
        if (scanner.hasNextLine()){
            String str =scanner.nextLine();
            System.out.println("输出的内容为:"+str);
            //输出hello world
        }
        scanner.close();
​
    }
}

2.Scanner进阶使用

package com.song.scanner;
​
import java.util.Scanner;
​
public class Demo4 {
    public static void main(String[] args){
        //输入多个数字,并求和以及平均数,每输入一个数字用回车确认,输入非数字来结束输入并执行结果
        Scanner scanner=new Scanner(System.in);
        //求和
        double sum=0;
        //计算输入个数
        int m=0;
        System.out.println("请输入数据:");
        //通过循环判断是否还有输入,并在里面对每一次进行求和统计
        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();
    }
}

3.顺序结构

语句与语句之间,框与框之间按从上到下的顺序进行。

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");
    }
}

4.选择结构

if单选择结构

public class IfDemo1 {
    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双选择结构

public class IfDemo2 {
    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("不及格");
        }
        scanner.close();
    }
}

if多选择结构

public class IfDemo3 {
    public static void main(String[] args){
        Scanner scanner =new Scanner(System.in);
        System.out.println("请输入成绩:");
        int score =scanner.nextInt();
        if (score<60&&score>=0){
            System.out.println("不及格");
        } else if (score<=70) {
            System.out.println("中等");
        } else if (score<=80) {
            System.out.println("良好");
        }else if(score<=100){
            System.out.println("优秀");
        }else{
            System.out.println("成绩不合法,满分为一百分!");
        }
        scanner.close();
    }
}

嵌套if结构

Switch多选择结构

public class SwitchDemo1 {
    public static void main(String[] args){
        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;
            default:
                System.out.println("未知等级");
                break;
​
        }
    }
}
public class SwitchDemo2 {
    public static void main(String[] args){
        String name ="艾瑞利亚";
        //表达式结果可以是字符串
        switch (name){
            case "艾瑞利亚":
                System.out.println("我是艾瑞利亚");
                break;
            case "亚索":
                System.out.println("我是亚索");
                break;
            default:
                System.out.println("出错了");
        }
    }
}

do...while循环

public class DowhileDemo2 {
    public static void main(String[] args){
        int a=0;
        while(a<0){
            a++;
            System.out.println(a);
        }
        System.out.println("==============");
        do {
            a++;
            System.out.println(a);
        }while(a<0);
    }
}
  1. 对于while语句而言,如果不满足条件,则不能进入循环;

  2. do...while循环和while循环相似,不同的是,do...while循环至少会执行一次;

  3. while先判断后执行,do...while循环先执行后判断

  4. do...while循环总是保证循环体被至少执行一次!

public class DowhileDemo2 {
    public static void main(String[] args){
        int a=0;
        while(a<0){
            a++;
            System.out.println(a);
        }
        System.out.println("==============");
        do {
            a++;
            System.out.println(a);
        }while(a<0);
    }
}

For循环

格式:for(初始值;条件判断;迭代){

循环体...........

}

public class ForDemo2 {
    public static void main(String[] args){
        //计算0到100之间奇数和偶数的和
        int oddsum=0; //奇数和
        int evensum=0; //偶数和
        for (int i = 0; i <= 100; i++) {
            if(i%2==0) {
                evensum = evensum + i;
            }else{
                oddsum+=i;
            }
        }
            System.out.println("偶数的和"+evensum);
            System.out.println("奇数的和"+oddsum);
    }
}
public class ForDemo3 {
    public static void main(String[] args){
        //for循环输出1--1000之间能被5整除的数,且每行输出三个
        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");
            }
        }
        //println输出会换行
        //print输出不会换行
    }
}
public class WhileDemo1 {
    public static void main(String[] args){
        //while循环输出1--1000之间能被5整除的数,且每行输出三个
        int i=0;
        while(i<=1000){
            i++;
            if (i%5==0){
                System.out.print(i+"\t");
            }
            if (i%(5*3)==0){
                System.out.println();
            }
​
        }
    }
}

打印九九乘法表

public class ForDemo4 {
    public static void main(String[] args){
        //打印九九乘法表,使用For循环的嵌套
        for (int j = 1; j <= 9; j++) {
            for (int i = 1; i <= j; i++) {
                System.out.print(i+"*"+j+"="+(j*i)+"\t");
            }
            System.out.println();
        }
    }
}

增强For循环

public class ForDemo5 {
    public static void main(String[] args){
        int[] numbers={10,20,30,40,50};
        for (int i = 0; i < 5; i++) {
            System.out.println(numbers[i]);
        }
        System.out.println("==================");
        //遍历数组中元素并输出
        for(int x:numbers){  
            System.out.println(x);
        }
    }
}

break、continue语句

break:结束循环体,继续执行下面的语句

continue:结束当次循环,继续执行下次循环(满足循环判定条件下)

public class BreakDemo1 {
    public static void main(String[] args) {
        int i=0;
        while (i<=100){
            i++;
            System.out.println(i);
            if (i==10){
                break;
            }
        }
        System.out.println("123");
    }
}
public class ContinueDemo1 {
    public static void main(String[] args) {
        int i=0;
        while(i<20) {
            i++;
            if (i % 10 == 0) {
                continue;
            }
            System.out.print(i+"\t");
        }
    }
}

练习:打印三角形

public class TextDemo {
    public static void main(String[] args) {
        //打印三角形
        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();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值