栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java-3 Java流程控制

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java-3 Java流程控制

3 Java流程控制

学习来源: 【狂神说Java】Java零基础学习视频通俗易懂_哔哩哔哩_bilibili

用户交互Scanner

获取用户的输入

  • 基本语法
Scanner s = new Scanner(System.in);
  • 通过Scanner类的next()与next()方法获取输入的字符串,在读取前我们一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据
package com.tanswc.scanner;

import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        //创建一个扫描器对象,用于接收键盘数据
        Scanner scanner = new Scanner(System.in);//Alt+Enter自动补全
        System.out.println("使用next方式接收:");

        //判断用户有没有输入字符串
        if (scanner.hasNext()){
            //使用next的方式接收用户的输入
            String str = scanner.next();
            System.out.println("输入的内容为:"+str);
        }
        //凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
        scanner.close();

    }
}
package com.tanswc.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()){
            String str = scanner.nextLine();
            System.out.println("输出的内容是:"+str);
        }
        scanner.close();
    }
}
Scanner对象

next():

  1. 一定要读取到有效字符后才可以结束输入
  2. 对有效字符之前遇到的空白,next()方法会自动将其去掉
  3. 只有输入有效字符后将其后面输入的空白作为分隔符或者结束符
  4. next()不能得到带有空格的字符串

nextLine():

  1. 以Enter为结束符 也就是说nextLine()方法返回的是输入回车之前的所有字符
  2. 可以获得空白
package com.tanswc.scanner;

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请数据数据——使用nextline方式接收:");
        String str = scanner.nextLine();//这一句用来接收数据
        System.out.println("输出的内容是:"+str);
        scanner.close();
    }
}
package com.tanswc.scanner;

import java.util.Scanner;

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();
    }
}
package com.tanswc.scanner;

import java.util.Scanner;

public class Demo05 {
    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+1
            m = m + 1;//m++
            sum = sum + x;
            System.out.println("你输入了第"+m+"个数据,然后当前结果sum="+sum+"请输入下一个数据:");
        }
        System.out.println(m+"个数的和为"+sum);
        System.out.println(m+"个数的平均值是"+(sum / m));
        scanner.close();
    }
}
顺序结构

程序默认的结构,自上而下的执行

他是任何一个算法都离不开的一种基本算法结构

package com.tanswc.struct;

public class ShunXuDemo {
    public static void main(String[] args) {
        //顺序结构
        System.out.println("hello world1");
        System.out.println("hello world2");
        System.out.println("hello world3");
        System.out.println("hello world4");
        System.out.println("hello world5");
        System.out.println("hello world6");
        System.out.println("hello world7");
    }
}
选择结构
  • if
  • switch
  1. if (){

}

package com.tanswc.struct;

import java.util.Scanner;

public class XuanZeDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Plese input what you want:");
        String s = scanner.nextLine();
         //equals:判断字符串是否相等,和==有区别,少用==去判断字符串
        if (s.equals("Hello")){
            System.out.println(s);
        }
        System.out.println("End");
        scanner.close();
    }
}
  1. if (){

}else{

}

import java.util.Scanner;

public class IfDemo02 {
    public static void main(String[] args) {
        //考试成绩大于60分就是及格,否则就是不及格
        Scanner scanner = new Scanner(System.in);
        System.out.println("Plese input your score:");
        int i = scanner.nextInt();
        if (i>= 60){
            System.out.println("your score is ok");
        }else{
            System.out.println("your score is bad");
        }


        scanner.close();
    }
}
  1. if (){

}else if ({

}

或者if的嵌套

import java.util.Scanner;

public class IfDemo03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("input your score");
        int score = scanner.nextInt();
        if (score < 60){//不够严谨,应该写 if (score < 60 && score >= 0)
            System.out.println("you are too bad");
        }else if (score < 80) {//没有具体的范围,虽然也没报错,正确应该是 if (score < 80 && score >= 60)
            System.out.println("you are bad");
        }else if (score < 90){
            System.out.println("you are good");
        }else {//应该加上,[90,100]的区间,除此之外的应该是else,并且输出 成绩不合法,这样全部范围都在里面,才是完整的程序
            System.out.println("you are very good" );
        }
        scanner.close();
    }
}
//修正后
package com.tanswc.struct;

import java.util.Scanner;

public class FuxiIfDemo03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的成绩:");
        int score = scanner.nextInt();
        if (score == 100){
            System.out.println("恭喜你获得满分,你真牛逼!");
        } else if (score >=85 && score <= 100) {
            System.out.println("真优秀!请继续保持");
        }else if (score >=70 && score <= 85){
            System.out.println("你的成绩还行,要好好学习,变得更优秀哦!");
        } else if (score >=60 && score <= 70) {
            System.out.println("你的成绩不太行,要好好努力啊");
        } else if (score >=0 && score <= 60) {
            System.out.println("你的成绩也太tm垃圾了,你整天都在干什么!!!");
        } else {
            System.out.println("你输入的是个什么东西,能不能长长眼好好看清输入的是啥,赶紧重新输入!");
        }
        scanner.close();
    }
}
  1. switch (){

case ():

break;

}

case穿透

public class SwitchDemo01 {
    public static void main(String[] args) {
        char grade = 'F';
        switch (grade) {
            case 'A':
                System.out.println("niu");
                break;
            case 'B':
                System.out.println("yiban");
                break;
            case 'C':
                System.out.println("couhe");
                break;
            case 'D':
                System.out.println("cha");
                break;
            default:
                System.out.println("reinput");
        }
    }
}
package com.tanswc.struct;

public class SwitchDemo02 {
    public static void main(String[] args) {
        String name = "王羲";
        //jdk7以后支持字符串的比较
        //反编译
        switch (name) {
            case "王羲之":
                System.out.println("王羲之");
                break;
            case "羲之":
                System.out.println("羲之");
                break;
            case "之":
                System.out.println("之");
                break;
            case "之羲":
                System.out.println("之羲");
                break;
            default:
                System.out.println("大哥");
        }
    }
}
循环结构
  • while 先判断后执行
  • do…while 先执行后判断,即使不满足循环,也至少执行一次
  • for循环
  1. while(){

}

public class WhileDemo01 {
    public static void main(String[] args) {
        //输出1~100
        int i = 1 ;
        while (i <= 100){
            System.out.println(i);
            i++;
        }
        System.out.println(i);
    }
}
public class WhileDemo03 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        while (i <= 100){
            sum =sum+i;
            i++;
        }
        System.out.println(sum);
    }
}

2.do{

}while();

public class DoWhileDemo01 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        do {
            sum = sum + i;
            i++;
        }while (i <= 101);
        System.out.println(sum);
    }
}
//测试do...whlie和while的区别
public class DoWhlieDemo02 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        while (i < 0){
            System.out.println(i);
            i++;
        }
        System.out.println("=======================================");
        do {
            System.out.println(i);
            i++;
        }while(i < 0);
        System.out.println(i);
    }
}
  1. for (初始值;布尔表达式;更新){

}

  • 最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。

快捷书写

fori

100.for

死循环:for( ; ; )

//分别计算1-100内奇数和偶数的和
public class ForDemo02 {
    public static void main(String[] args) {
        int jishuhe = 0;
        int oushuhe = 0;
        for (int i = 0;i <= 100;i++){
            if (i%2==0){
                oushuhe+=i;
            }else{
                jishuhe+=i;
            }
        }
        System.out.println(jishuhe);
        System.out.println(oushuhe);
    }
}
//1000以内输出能整除5的数字,并且每行打印3条
//方法1for
public class ForDemo03 {
    public static void main(String[] args){
        for(int i=0;i<=1000;i++){
            if (i%5 == 0){
                System.out.print(i+"t");//print输出不会换行,println输出会换行
            }
            if (i%(3*5)==0){//每行
                //System.out.println();
                System.out.print("n");
            }
        }
    }
}
//方法2while
public class ForDemo04 {
    public static void main(String[] args) {
        int i = 0;
        while (i <= 1000){
            if (i%5 == 0){
                System.out.print(i+" ");
            }
            if (i%(3*5) == 0) {
                System.out.println();
            }
            i++;
        }
    }
}
//九九乘法表
public class ForDemo05{
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1;i <= 9;i++){
            for (int j = 1;j <= 9;j++){
                sum=i*j;
                System.out.print(i+"*"+j+"="+sum+"t");
            }
            System.out.println();
        }
    }
}
  1. 增强型for循环 for-each

for(声明语句:表达式){

//代码句子

}

//遍历数组
package com.tanswc.struct;

public class ForDemo06 {
    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("=========================");
        //遍历数组的元素,把number赋值给int x,遍历
        //jdk5引入的
        for (int x:numbers){
            System.out.println(x);
        }
    }
}
break&continue
  • break 用于强制退出循环,只是退出循环,不执行循环中剩余的语句,但不会终止程序
  • continue 终止当次循环,即跳过循环中尚未执行的语句,接着进行下一次是否执行循环的判定
  • goto带标签,java没有goto,但是可以通过标签的形式寻找goto的影子
    • “标签”是指后面跟着一个冒号的标识符
    • 对于java来说唯一用到标签的地方就是循环语句之前。而在循环之前设置的唯一理由是:我们希望在其中嵌套另一个循环,由于break和continue关键字通常只中断当前循环,但若随同标签使用,他们就会中断到存在标签的地方
  • return:结束方法的运行
//break示例
public class BreakDemo{
    public static void main(String[] args){
        int i = 0;
        while (i < 100){
            i++;
            System.out.println(i);
            if (i == 30){
                break;
            }
        } 
    }
}
//continue示例
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.print(i+"t");
        }
    }
}
//goto
public class LabelDemo {
    public static void main(String[] args) {
        ddd:for (int i = 101;i < 150;i++){//ddd是标签,java没有goto,但是标签就是goto的影子
            for (int j = 2;j < i;j++){
                if (i%j == 0) {
                continue ddd;
                }
            }
            System.out.println(i);
        }

    }
}
练习
//打印一个三角形,主要是逻辑思维:可以先打印一部分出来,再打印另外一部分出来,最后打印全部的东西
public class LianxiDeno01 {
    public static void main (String[] args){

        for (int n = 1; n <= 10; n++) {//一个循环中可以包含多个循环
            for (int m = 10;m >= n;m--){
                System.out.print(" ");
            }
            for (int m = 1; m <= n; m++) {//不同的循环中可以用同一个变量,表示不同的含义
                System.out.print("*");
            }
            for (int m = 1;m < n;m++) {
                System.out.print("*");
            }
            System.out.println();
        }       
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/847049.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号