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

Java中的逻辑控制与方法(上)

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

Java中的逻辑控制与方法(上)

hi,小魏来更新了,今天为大家讲解一些新的东西,还接着上期唠,送点花花给大家

一 逻辑控制 a.顺序结构 b.选择结构 c.循环结构与Switch  case语句 d.猜数字游戏 二 方法的使用 1.了解方法的使用

在讲解知识点的过程中会用代码实例来强化理解

一 逻辑控制

a.顺序结构

这个很简单,就是代码从上到下依次执行,没有啥要说的

b.选择结构

选择结构包括呢,有if(){},if(){} else(),还有if(){}else if(){}else{}

选择结构得跟布尔表达式,不然编译器就会报错

选择结构只能实现一个

话不多说,上代码

public class TestDemo {
    public static void main(String[] args) {
        int x=10;
        int y=20;
        if(x==20){
            if (y==20){
                System.out.println("you are a pig");
            }
            else{
                System.out.println("you are not a pig");
            }
        }


    }
}
public class TestDemo {
    public static void main(String[] args) {
        int x=10;
        int y=20;
        if(x==20)
            if (y==20)
                System.out.println("you are a pig");

            else
                System.out.println("you are not a pig");




    }
}

大家看这两个代码,唯一的区别就是没有括号,看看俩段代码的结果是一样的吗?

答案是一样的,但是肯定会有做错的,因为这个括号很重要,不管你会不会判断,在初学阶段要加上括号

答案就是啥也不打印 ,第一个if中判断结果为false,所以啥也不执行

判断平年闰年的代码

在写这个代码之前,要知道什么是闰年,什么是平年

普通闰年:公历年份是4的倍数,且不是一百的倍数的,为闰年

世纪闰年:公历是100的倍数,也是400的倍数,为闰年

public class TestDemo {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //处理数据的多组输入
        while (scan.hasNext()) {
            int year = scan.nextInt();
            if (year % 100 == 0) {
                if (year % 400 == 0) {
                    System.out.println(year + "是闰年");
                } else {
                    System.out.println(year + "不是闰年");
                }
            }
                else{
                    if (year % 4 == 0) {
                        System.out.println(year + "是闰年");
                    } else {
                        System.out.println(year + "不是闰年");
                    }
                }
            }
        }
    
    

三  循环结构与switch    case 语句

a.   switch    case 语句

不能做switch的参数的类型有哪些

long float double boolean

public static void main3(String[] args) {
        String  a = "hello";//字符串是可以的  以后学习的枚举也是可以的
        //int a = 10;
        switch (a) {
            case "hello":
                System.out.println("hello");
                break;
            case "hello2":
                System.out.println("2");
                break;
            default:
                System.out.println("输入错误!");
                break;
        }
    }

要注意,Switch   case中case的类型要与Switch的类型相对应

b.循环结构

循环结构有while,do{}while();   for循环

下面,我来一个一个讲解

while结构

while(),括号中也是跟着布尔表达式

public class TestDemo {
    
    public static void main4(String[] args) {
        int sum = 0;//保存1-100的和
        int sumEve = 0;//保存1-100偶数的和
        int sumOdd = 0;//保存1-100奇数的和

        int i = 1;
        while (i <= 100) {
            sum += i;
            i++;
        }
        i = 2;//重新要赋值  上面已经加到了101了
        while (i <= 100) {
            sumEve += i;
            i+=2;//步进
        }
        i = 1;//重新要赋值
        while (i <= 100) {
            sumOdd += i;
            i+=2;//步进  不仅仅是i++;
        }
        System.out.println(sum);
        System.out.println(sumEve);
        System.out.println(sumOdd);

    }
  
 
public static void main5(String[] args) {
        int i = 1;
        int ret = 1;
        while (i <= 5) {
            ret = ret * i;
            i++;
        }
        System.out.println(ret);
    }
    public static void main7(String[] args) {
        int j = 1;
        int sum = 0;
        while (j <= 5) {
            int i = 1;
            int ret = 1;
            while (i <= j) {
                ret = ret * i;
                i++;
            }
            sum += ret;
            j++;
        }
        System.out.println(sum);
    }
 public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入你的姓名");
        String str=scanner.nextLine();//读入一个字符串
        System.out.println(str);


        System.out.println("请输入你的年龄");
        int age=scanner.nextInt();//读入一个整数
        System.out.println(age);


        System.out.println("请输入你的工资");
        float money=scanner.nextFloat();//读入一个浮点数
        System.out.println(money);


    }
 
    public static void main7(String[] args) {
        System.out.println("输出且换行!");
        System.out.print("输出且不换行!");
        System.out.printf("%sn","真的没换行吗?");
    }
public class TestDemo {
    public static void main9(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random(5220);//这里的seed和bound是系统自动生成的。
        int randNum = random.nextInt(101);//[0,101)==>[0,100]
        System.out.println("随机数是" + randNum);//生成的随机数是不一样的,是根据时间戳来生成的

        //int randNum = random.nextInt(100)+1;//[1,101)  数学的思维:50-100
        // int rand = (int) Math.random();
        //int randNum = random.nextInt();
        while (true) {
            System.out.println("请输入你要猜的数字:");
            int num = scanner.nextInt();
            if (num < randNum) {
                System.out.println("猜小了!");
            } else if (num == randNum) {
                System.out.println("猜对了!");
                break;
            } else {
                System.out.println("猜大了!");
            }
        }

    }

b.  do  while循环结构一般不咋用,就不做详细描述了

c.for  循环 

与C语言中的for循环并没有很大差别

四     方法的使用

方法就是函数


public class TestDemo {
    public static int add(int a, int b) {
        System.out.println("正在调用add()");
        return a+b;
    }


    public static void main10(String[] args) {
        int a=10;
        int b=20;
        int ret=add(a,b);
        System.out.println(ret);
    }
public class TestDemo {
    public static int fac(int n) {
        int ret = 1;
        for (int i = 1; i <= n; i++) {//这段代码是实现某个数的阶乘
            ret *= i;
        }
        return ret;
    }

    
    public static int facSum(int k) {
        int sum = 0;
        for (int i = 1; i <= k; i++) {//这段代码是实现几个数的阶乘和的代码
            sum += fac(i);
        }
        return sum;
    }

    public static void main(String[] args) {//调用
        System.out.println(facSum(5));//5就是有几个数的和
    }

小魏亲笔,求点关注!谢谢 

以上就是今天的分享,我们下期再见!886

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

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

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