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

java流程控制

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

java流程控制

java流程控制

基本和C没啥差别

Scanner对象

获取用户的输入

Scanner S =new Scanner(System.in);
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();
    }
}

next():

  1. 以空白为结束符
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();
    }
}

nextLine():

  • 以回车结束
public class Demo04 {
    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++;
            sum = sum + x;
        }
        System.out.println("个数"+m);
        System.out.println("sum"+sum);

        scanner.close();
    }
}

scanner.hasNextDouble(),名副其实,同理有hasNextFloat()等。

顺序结构

都知道就不写了

选择分支结构

和C差不多

  • if单选择结构

  • if双选择结构

    public class IfDemo01 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    
            System.out.println("plz input");
            String s =scanner.nextLine();
            if(s.equals("How much?")){
                System.out.println("9.15$");
            }
            else{
                System.out.println("I don't know.");
            }
            scanner.close();
        }
    }
    
  • 嵌套结构

  • switch结构

    与c一样,但是jdk7支持了String

    public class SwitchDemo01 {
        public static void main(String[] args) {
           String level = "菜";
    
           switch (level){
               case "牛逼":
                   System.out.println("优秀");
                   break;
               case "菜":
                   System.out.println("良好");
                   break;
               case "傻逼":
                   System.out.println("及格");
                   break;
               default:
                   System.out.println("未知情况!");
           }
        }
    }
    

    反编译的操作:直接用idea打开class文件就可以完成反编译。

    反编译得到原因如下:

    • 因为运用了hashCode()

    • 每个对象都有自己的编码

package com.niuya.struct;
public class SwitchDemo01 {
    public SwitchDemo01() {
    }
    public static void main(String[] args) {
        String level = "菜";
        byte var3 = -1;
        switch(level.hashCode()) {
        case 33756:
            if (level.equals("菜")) {
                var3 = 1;
            }
            break;
        case 677601:
            if (level.equals("傻逼")) {
                var3 = 2;
            }
            break;
        case 944449:
            if (level.equals("牛逼")) {
                var3 = 0;
            }
        }
        switch(var3) {
        case 0:
            System.out.println("优秀");
            break;
        case 1:
            System.out.println("良好");
            break;
        case 2:
            System.out.println("及格");
            break;
        default:
            System.out.println("未知情况!");
        }
    }
}
循环结构

与C无差别。

  • while 循环

    满足while的条件就一直执行

  • do ……while循环

    先do,再判断while里的条件。

  • for循环(使用的多)

    与C语言无差别。

    public class WhileDemo01 {
        public static void main(String[] args) {
            int sum = 0;
            for (int i=1;i<=100;i++)
            {
                sum=sum+i;
            }
            System.out.println("1到100的和:"+sum);
        }
    }
    

    0到100的奇数偶数和

    public class ForDemo01 {
        public static void main(String[] args) {
            int sum_ji = 0;
            int sum_ou = 0;
            for (int i = 0; i <= 100; i++) {
                if(i%2==0){
                    sum_ou = i + sum_ou;
                }
                else{
                    sum_ji = i + sum_ji;
                }
            }
            System.out.println("0到100内的奇数和:"+sum_ji);
            System.out.println("0到100内的偶数和:"+sum_ou);
        }
    }
    

    打印0到1000能被5整除的并每行三个

    public class ForDemo02 {
        public static void main(String[] args) {
            System.out.println("能被5整除的数");
            for (int i = 0; i <=1000 ; i=i+15) {
                if(i%5==0){
                    System.out.println(i+"t"+"t"+(i+5)+"t"+(i+10));
                    //System.out.print();输出不换行
                }
            }
        }
    }
    

    九九乘法口诀表

    public class ForDemo03 {
        public static void main(String[] args) {
            for (int i = 1; i <=9; i++) {
                for (int j = 1; j <=i ; j++) {
                    System.out.print(i+"*"+j+"="+(i*j)+"t");
                }
                System.out.println();
            }
        }
    }
    
  • java5引入增强型的for循环(以上同C)

    用于遍历数组和集合

    public class ForDemo04 {
        public static void main(String[] args) {
            int[] numbers = {10,20,30,40,50};//定义了一个数组
    		//一般型
            for (int i = 0; i < 5 ; i++) {
                System.out.print(numbers[i]+"t");
            }
            System.out.println();
            System.out.println("=================");
            //增强型,遍历数组元素
            for (int x:numbers){
                System.out.print(x+"t");
            }
        }
    }
    
break 和 continue

和C一个用法

笑死了,类似辞职和请假,break直接跳出了循环,continue是跳过这次循环

java中类似goto的操作-----标签label

不建议使用,goto很危险

public class LabelDemo {
    public static void main(String[] args) {
        //打印101~150所有的质数
        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.print(i+"t");
        }
    }
}
作业打印三角形
public class TestDemo {
    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("x");
            }
            System.out.println();
        }
    }
}

把*改为空格,就可以看到三角形了。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/396938.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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