java方法+流程控制+循环

java 方法+流程控制+循环

1 运算符
1.1 概述
在这里插入图片描述

1.2 练习1:平年闰年
输入年号,判断是否是闰年。两个条件:

1、能被4整除,并且不能被100整除
2、或者能被400整除

public class Test1 {

       public static void main(String[] args) {

              System.out.println("年号:");

              int y = new Scanner(System.in).nextInt();

             

              String r="平年";

              if(y%4==0){

                     if(y%100!=0){

                            r="闰年";

                     }

              }

              if(y%400==0){

                     r="闰年";

              }


 
//或者
 

if((y%4==0&&y%100!=0)||y%400==0){

                     r="闰年";

              }

              System.out.println(y+"年是"+r);
       }

}

 

1.3 demo2:自增自减

int a = 1;

              System.out.println(a++);//先取值再自增

             

              int b=1;

              System.out.println(++b);//先自增再取值

             

              int c=1;

              int d = c++;

              int e = ++c;

              System.out.println(d);

              System.out.println(e);

1.4 练习3:求两个数里的大值

1.5 练习4:求三个数的最大值

package day0203_平年闰年;

import java.util.Scanner;

public class Test1_三个数的最大值 {

    public static void main(String[] args) {

             

              System.out.println("整数a:");

              int a = new Scanner(System.in).nextInt();

             

              System.out.println("整数b:");

              int b = new Scanner(System.in).nextInt();

 

              System.out.println("整数c:");

              int c = new Scanner(System.in).nextInt();
           
int max = a>b?a:b;
              max=max>c?max:c;

//或者

int max = a>b?(a>c?a:c):(b>c?b:c);

              System.out.println(max);

       }

}

2 分支结构1:if
2.1 概述
顺序结构的程序虽然能解决计算、输出等问题,但不能做判断再选择。对于要先做判断再选择的问题就要使用分支结构。

2.2 形式

单分支:

if(判断条件){

   代码。。。

}

多分支:

if(判断条件){

   代码1。。。

}else{

   代码2。。。

}

嵌套分支:

if(判断条件1){

   代码1。。。

}else if(条件2){

   代码2。。。

} else if(判断条件3){

   代码3。。。

}else{

   代码4。。。

}

2.3 练习1:商品打折
接收用户输入的原价。满1000打9折。满2000打8折。满5000打5折。

public class ttt {


       public static void main(String[] args) {

 

              System.out.println("输入总原价");

             

              double price = new Scanner(System.in).nextDouble();

              double now = f(price);

              System.out.println(now);

       }

      

       public static double f(double p){

             

              if(p>5000){

                     p=p*0.5;

              }else

              if(p>2000){

                     p=p*0.8;

              }else if(p>1000){

                     p=p*0.9;

              }

             

              return p;

       }

      

}

2.4 练习2:统计学员得分

90分以上 优秀

80~89 良好

70~79 中等

60~69 及格

60分以下 不及格

public class aa {

    public static void main(String[] args) {

       double score = new Scanner(System.in).nextDouble();

 

       if (score >= 100 || score <= 0) {

           System.out.println("请输入0~100以内的值");

       }

       if (score > 90 && score <= 100) {

           System.out.println("优秀");

       } else if (score >= 80 && score <= 90) {

           System.out.println("良好");

       } else if (score >= 70 && score <= 79) {

           System.out.println("中等");

       } else if (score >= 60 && score <= 69) {

           System.out.println("及格");

       }else if (score < 60) {

           System.out.println("不及格");

       }

 

    }

}

3 [了解]分支结构2:switch
3.1 概述
当一个case成立,从这个case向后穿透所有case,包括default,直到程序结束或者遇到break程序才结束。

3.2 形式
switch(expr1)中,expr1是一个整数表达式, 整数表达式可以是int基本类型或Integer包装类型,由于byte,short,char都可以隐含转换为int,所以也支持。

注意: jdk1.7以后新增 String

switch(变量或者表达式){

   case 1:

   case 2:

   case 3:

case 4:

   default:

}

3.3 练习1:数字匹配

public class Test1_数字匹配 {

 

       public static void main(String[] args) {

             

              int i=3;

              switch (i) {

              case 1:

                     System.out.println("1");

                     break;

              case 2:

                     System.out.println("2");

                     break;

              case 3:

                     System.out.println("3");

//                  break;

              default:

                     System.out.println("default");

                     break;

              }

             

       }

      

}

4 循环结构1:for
4.1 概述
循环结构是指在程序中需要反复执行某个功能而设置的一种程序结构。

它由循环体中的条件,判断继续执行某个功能还是退出循环。

根据判断条件,循环结构又可细分为先判断后执行的循环结构和先执行后判断的循环结构。

4.2 形式

for(开始条件;循环条件;更改条件){

   循环体代码…

}

4.3 练习1:打印0到10
在这里插入图片描述

public class ttttt {

       public static void main(String[] args) {

              f1();

       }

       public static void f1(){

              for (int i = 0; i <= 10; i++) {

                     System.out.print(i);

              }

       }

      

}

4.4 练习2:打印10到0

public class ttttt {

       public static void main(String[] args) {

              f2();

       }

       public static void f2(){

              for(int i=10;i>=0;i--){

                     System.out.print(i);

              }     

       }

}

4.5 练习3:打印8,88,888,8888

public class ttttt {

       public static void main(String[] args) {

              f();

       }

       public static void f(){

              for(int i=8;i<=10000;i=i*10+8){

                     System.out.println(i);

              }

       }

}

5 拓展
3.5.1 &和&&的区别
当一个&表达式在求值的时候,两个操作数都会被求值,&&更像 是一个操作符的快捷方式。当一个&&表达式求值的时候,先计算第一个操作数,如果它返回true才会计算第二个操作数。如果第一个操作数 取值为fale,第二个操作数就不会被求值

5.2 a=a+4和a+=4的区别

byte a = 1;

// a=(byte) (a+4);//右侧int,左侧byte,大转小,强转。

// a=(byte) (a+4);//右侧int,左侧byte,大转小,强转。

   a+=4;//会自动完成数据类型的转换 

   //注意:a=a+4和a+=4是有区别的!!

   System.out.println(a);
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值