Java(if、switch、while、for语句)

2021-01-12

1、if语句(判断语句)

  if(条件1){执行语句;}//条件一般用boolean表达式,条件为true才执行里面语句;
        if else(条件2){执行语句;}//上一个条件不满足才判断这个条件
              if else(条件n){执行语句;}
                    else{条件都不成立所执行的内容;}
  例如:
        public class Score {
            public static void main(String[] args) {
                System.out.print("请输入小明考试成绩0-100之间:");
                Scanner sc = new Scanner(System.in);
                int grade = sc.nextInt();
                if(grade <=100 && grade >=0 ) {
                    if (grade == 100) {
                        System.out.println("满分,奖励一台Iphone");
                                }
                    else if (grade >= 90 ) {
                        System.out.println("成绩优秀,奖励麦当劳一次");
                    }
                    else if (grade >= 80 ) {
                        System.out.println("成绩良好,奖励练习题一套");
                    }            
                    else if (grade >= 60 ) {
                        System.out.println("成绩危险,再补课一学期");
                    }
                    else {
                        System.out.println("不及格,回家藤条焖猪肉");
                    }
                }else  {
                    System.out.println("请输入合法成绩!");
                }
            }
            }
  if的嵌套语句:

equals关键字

  作用:对字符串的数据进行比对,结果返回的是布尔值(true or  false)  
  用法:变量名.equlas("数据");

equals和等于号== 的区别

  ==是对对象地址的比较,而equals是对对象内容的比较。对于基本数据类型,一般用==,而对于字符串的比较,一般用equals

选择语句: Switch...case...

  作用:可以选择相同数据值的下面的执行程序;
  语法结构:
        Switch(表达式/变量名){
              case值1:满足括号里面的值等于值1所执行的内容;break;
              case值n:满足括号里面的值等于值n所执行的内容;break;
              default:默认执行的内容;
              }
        注意事项:
            1.switch里面的表达式:byte,short,int,char,枚举,字符串
            2.case后面必须接常量,并且常量值不能重复
            3.break当满足条件之后,跳出整个switch语句,break由于不会再继续执行,所有不能再写其他的任何语句
            4.default默认值,如果上面所有的case都不满足条件,就会执行default里面的语句块

while循环

  作用:对程序在一定条件下进行循环执行
  语法结构:
        while(判断条件){如果条件为true,就执行循环语句;}//如果条件一直为True就会进入死循环
        例如:  //水仙花:个位数的三次方+十位数的三次方+百位数的三次方 = 这个数本身
              public static void main(String[] args) {
                    int i =100;
                while(i<1000) {
                    int a, b, c;
                    c = i % 10;
                    b = i / 10%10;
                    a = i/ 100;
                    if (a * a * a + b * b * b + c * c * c == i ){
                        System.out.println(i +"这个数是一个水仙花数!");
                    } else {
                        System.out.println(i+"这个数不是一位水仙花数");
                    }
                    i++;
                }
                }

do...while循环:至少循环一次

         do{循环语句;}while(判断语句);
  特点:先执行,再循环

Random类

  作用:随机生成数字类,在java中调用util的Random,使用时需要创建一个对象,通过对象调用方法,需要在括号里设置取值范围,值是从0开始,到括                        
       号里面的数字-1
  例如://随机生成10-20之间的两个数字,比较这两个数字是否相同
          int i=1;
          int a1;
          int a2;
          do {
              Random ra = new Random();
              //创建两个变量用来存储随机生成的数字
              a1 = ra.nextInt(11) + 10;
               a2 = ra.nextInt(11) + 10;
              System.out.println(a1+"-"+a2);
              if (a1 == a2) {
                  System.out.println("相同");
              } else {
                  System.out.println("不相同");
              }
              System.out.println("循环次数:"+i);
              i++;
          }while(a1 != a2);
        }

for循环

  语法结构:for(循环初始值;循环判断条件;循环变量变化){循环内容;}
   注:执行顺序:先执行初始值,再判断条件,再执行循环内容,最后执行循环变量变化;
       例如:  //1-100之间不能被3整除的数之和
            public static void main(String[] args) {
                int sum = 0;
                int count = 0;
                for (int i = 1; i <= 100; i++) {
                    if (i % 3 != 0) {
                        sum += i;
                    }
                    count++;
                }
                System.out.println(sum);
                System.out.println(count);
            }

break关键字接和continue关键字

  break关键字:直接退出循环体
  continue关键字:结束本次循环,进入下一次循环。
  例如:
         public static void main(String[] args) {
          int sum = 0;
          for (int i = 1; i <= 10; i++) {
              if(i%2 == 1) {
                  continue;
              }
              sum +=i;
          }
          System.out.println(sum);
      }
     }

经典嵌套循环

   例如1、//九九乘法表
            public static void main(String[] args) {
                for (int i = 1; i <10 ; i++) {
                    for (int j = 1; j <=i ; j++) {
                        System.out.print(j+"×"+i +"="+i*j);
                        System.out.print("  ");
                    }
                    System.out.println();
                }
            }

   例如2://随机定一个小于10位的数,判断它是几位数,例如999是三位数
            public static void main(String[] args) {
                Random ra = new Random();
                int x = ra.nextInt(1000000000);//小于10位数
                System.out.println(x);
                int count = 0;
                for (int i = 1; i < 10; i++) {
                    x = x / (10);
                    count++;
                    if (x == 0) {
                        break;
                    }
                }
                System.out.println("这是一个:" + count + "位数");
            }
            }

posted @ 2021-01-12 17:41  一名初学者  阅读(481)  评论(0)    收藏  举报