The first stage 05 process control

Java branch structure - if statement

(1) Basic if branch structure

The form of the basic if branch structure:

  if(conditional expression) {

    statement body 1;

}

Among them, the else statement body is optional. { } (curly braces) can be omitted

   example:

    if(score>60)System.out.println("及格");

    else System.out.println("Failed ");

(2) Nested if structure

  The form of the nested if structure is:

    if(conditional expression 1) statement body 1;

    else if(conditional expression 2) statement body 2;

    ........

    [else statement body n]

  

  example:

    public class StuAverage{

      public static void main(String[] args){

        System.out.println("Enter student average grade");

        Scanner input = new Scanner(System.in);

        int average = input.nextInt(); //Student average grade

        if(avaerage>100){ //Student average grade

          System.out.println("Incorrect input!");

        }else if(average>=85){ //100 >=average grade>=85

              System.out.println("优");

        }else if(average>=70){ //85 >=average grade>=70

             System.out.println("良");

          }else if(average>=60){ //70 >=average grade>=60       

          System.out.println("Pass");

        }else{ // average score < 50

          System.out.println("Failed");

        }

      }

    }

 

keyboard input:

  Scanner is a new class after SDK1.5, which can be used to create a keyboard input object, such as: Scanner reader = new Scanner(System.in);

  Then the reader object calls related methods (functions) to read various data types entered by the user on the command line. These methods will cause blockages when executing, waiting for the user to enter data on the command line and press Enter to confirm. Several commonly used methods are as follows:

      nextInt(): Wait for the user to input an integer data and press Enter, this method gets an int data.

      nextDouble(): Wait for the user to input a value and press Enter, this method gets a double type of data.

      nextFloat(): Wait for the user to input a value and press Enter, this method gets a float type of data.

      nextLine(): Wait for the user to input a text line and press Enter, this method gets a String type of data.

  example:

    Scanner sc = new Scanner(System.in);

    inttemp = sc.nextInt();

    String str = sc.nextLine();

 

Java branch structure - switch statement

The basic form of the switch selection structure:

  switch(expression){

    case value 1: {statement body 1}; [break;]

    case value 2: {statement body 2}; [break;]

    .......

    case value

}

  example:

    import java.util.Scanner;

    public class TestSwitch{

      public static void main(String[ ] args){

        Scanner input = new Scanner(System.in);

        System.out.println("请输入第一个操作数:");

        int data1 = input.nextInt();

        System.out.println("请输入第二个操作数:");

        int data2 = input.nextint();

        int result = 0;

        System.out.println("请输入一个操作符:     ");

        char opr = (char)Sytem.in.read( );

        switch(opr){

          case '+';

            result = data1 + data2;

            break;

          case '-';

            result = data1 - data2;

            break;

           case '*';

            result = data1 * data2;

            break;

          case '/':

            result = data1/data2;

            break;

      }

      System.out.println(data1+" "+opr+" " +data2+ "=" +result);

  }

}

 

 

Java循环结构-for语句

(1)for循环结构的一般格式:

    for(循环初始值;循环条件;循环增长量){

      循环体

}

  例:  

    用for循环完成1+2+3+.....+100的计算

    int sum = 0;

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

      sum = sum +i;

}

     System.out.println("1+2+3+...+100="+sum);

 

 Java循环结构-while语句与do...while语句

(1)while循环结构的表达式形式:

    while(循环结构){

      语句体

}

     例:

    用while循环完成10!的计算

    ..........

    int sum = 1;

    int i = 1;

    while(i <=10){

        sum = sum * i;

        i++;

    }

    System.out.println("10 !=" + sum);

    .........

 

(2)do......while循环结构的表达式形式:

    do{

      语句体;

    }while(表达式);

    例:

       用do.....while循环完成10!的计算

   ...........

    int sum = 1;

    int i = 1;

    do{

      sum = sum * i;

      i++;

    }while(i <= 10);

    System.out.println("10 != " + sum);

   .............

如何中断和继续语句的执行

break:中断语句执行

   break关键字在前面学习switch分支语句中已经使用过了,下面直接用代码说明

continue:继续语句执行

   continue语句表示跳出本循环,继续执行下一次循环,同样还是采用程序来讲解continue语句的知识

 

 

Scanner

我们要学的Scanner类是属于引用数据类型,我们先了解下引用数据类型。

1.引用数据类型的使用

 与定义基本数据类型变量不同,引用数据类型的变量定义及赋值有一个相对固定的步骤或格式。

 数据类型  变量名  =  new 数据类型();

每种引用数据类型都有其功能,我们可以调用该类型实例的功能。

 变量名.方法名();

 

Scanner类

 Scanner类是引用数据类型的一种,我们可以使用该类来完成用户键盘录入,获取到录入的数据。

 Scanner使用步骤:

    导包:import java.util.Scanner;

 创建对象实例:Scanner sc = new Scanner(System.in);

 调用方法:

          int  i = sc.nextInt(); 用来接收控制台录入的数字

                           String s = sc.next(); 用来接收控制台录入的字符串

 了解完Scanner类,我们编写代码来使用下它:ScannerDemo01.java

      import java.util.Scanner;

     public class ScannerDemo01 {

      public static void main(String[] args) {

        //创建Scanner引用类型的变量

        Scanner sc = new Scanner(System.in);

        //获取数字

        System.out.println("请输入一个数字");

        int n = sc.nextInt();

        System.out.println("n的值为" + n);

     //获取字符串

        System.out.println("请输入一个字符串");

        String str = sc.next();

        System.out.println("str的值为" + str);

    }

}

 

 

随机数类Random

我们来学习下,用来产生随机数的类Random,它也属于引用数据类型。

这个Random类,它可以产生多种数据类型的随机数,在这里我们主要介绍生成整数与小数的方式。

1.方法简介

public int nextInt(int maxValue)     产生[0,maxValue)范围的随机整数,包含0,不包含maxValue;

public double nextDouble()  产生[0,1)范围的随机小数,包含0.0,不包含1.0。

         引用数据类型的使用方式,在学习键盘录入Scanner时,我们已经学习过,在这里,再次回顾一下:

 Random使用方式:

  import导包:所属包java.util.Random  

  创建实例格式:Random 变量名 = new Random();

接下来,通过一段代码,一起学习下Random类的使用,RandomDemo.java

      import java.util.Random;

      public class RandomDemo {

        public static void main(String[] args) {

         // 创建Random类的实例

         Random r = new Random(); 

         // 得到0-100范围内的随机整数,将产生的随机整数赋值给i变量

         int i = r.nextInt(100); 

         //得到0.0-1.0范围内的随机小数,将产生的随机小数赋值给d变量

         double d = r.nextDouble(); 

         System.out.println(i); 

         System.out.println(d); 

    }

}

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326533062&siteId=291194637