Java控制结构

Posted Wecccccccc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java控制结构相关的知识,希望对你有一定的参考价值。

控制结构

  • 程序流程控制介绍

顺序控制

分支控制if-else

单分支

案例演示

01:

import java.util.Scanner;
public class IfWorkDemo
{
    public static void main(String[] args)
    {
        Scanner myScanner = new Scanner(System.in);
        System.out.println("input your age");
        int age = myScanner.nextInt();
        if (age > 18)
        {
            System.out.println("Your age beyond 18 ,you shoule  be responsible  to your action ");
        }
    }
}

流程图

双分支

案例演示

01:

import java.util.Scanner;
public class IfWorkDemo01
{
    public static void main(String [] args)
    {
        Scanner myScanner = new Scanner(System.in);
        int age = myScanner.nextInt();
        System.out.println("input your age");
        if (age > 18)
        {
            System.out.println("Your age beyond 18 ,you shoule  be responsible  to your action ")
        }
        else
        {
            System.out.println("You are not old no enough to let you off this time");
		}
	}
}

流程图

单分支和双分支练习题

01:

public class PracticeWorkDemo
{
    public static void main(String [] args)
    {
        int x = 7;
        int y = 4;
        if (x > 5)
        {
            if (y > 5)
            {
            System.out.println(x+y);
            }
        	System.out.println("okokok");
        }
        else
        {
            System.out.println("x is "+x);
        }
    }
}

OUTPUT:

okokok

02:

public class IfWorkDemo
{
    public static void main(String[] args)
    {
        double d1 = 34.5;
        double d2 = 2.6;
        if (d1 > 10.0 && d2 < 20.0)
        {
            System.out.println("两个数之和 = "+(d1+d2);
        }
    }
}

03:

public class SumWorkDemo
{
    public static void main(String[] args)
    {
        int num1 = 10;
        int num2 = 5;
        int sum = num1+num2;
        if (sum % 3==0 && sum % 5==0)
        {
            System.out.println("ok");
		}
        else
        {
            System.out.println("no");
		}
    }
}

04:

import java.util.Scanner;
public class YearWorkDemo
{
    public static void main(String[] args)
    {
        Scaneer myYear = new Scanner(System.in);
        int year = myYear.nextInt();
        if ((year %4==0 && year %100 !=0 )|| year %400 ==0)
        {
            System.out.println("ok");
		}
        else
        {
            System.out.println("no");
		}
    }
}

多分支

流程图

案例演示

import java.util.Scanner;
public class MaWorkDemo
{
    public static void main(String[] args)
    {
        Scanner myScanner = new Scanner(System.in);
        int score = myScanner.nextInt();
        if (score >= 1 && score <= 100)
       {   
        if (score == 100)
        {
            System.out.println("信用极好");
		}
        else if (score > 80 && score <= 99)
        {
            System.out.println("信用优秀");
		}
        else if (score >=60 && score <= 80)
        {
            System.out.println("信用一般");
        }
        else
        {
            System.out.println("信用不及格");
		}
       }
        else
        {
            System.out.println("Input is Error");
		}
    }
}

小练习

01:

boolean b = true;
if (b==false)
{
    System.out.println("a");
}
else if (b)
{
    System.out.println("b");
}
else if (!b)
{
    System.out.println("c");
}
else 
{
    System.out.println("d");
}

//output = b

02:

boolean b = true;
if (b=false)
{
    System.out.println("a");
}
else if (b)
{
    System.out.println("b");
}
else if (!b)
{
    System.out.println("c");
}
else 
{
    System.out.println("d");
}

//output = c

嵌套分支

应用案例

01:

import java.util.Scanner;
public class MatchWorkDemo
{
    public static void main(String [] args)
    {
        Scanner myScanner = new Scanner(System.in);
        System.out.println("Please input score");
        double score = myScanner.nextDouble();
        if (score > 8.0)
        {
            System.out.println("Please input gender");
             char gender = myScanner.next().charAt(0);
            if (gender == '男')
            {
                System.out.println("Enter the men's division");
            }
            else if (gender== '女')
            {
                System.out.println("Enter the women's division");
            }
            else 
            {
                System.out.println("Input Error");
            }
		}
        else
        {
            System.out.println("cup tie");
        }
    }
}

代码略。

switch分支结构

流程图

案例演示

01:

import java.util.Scanner;
public class SwitchWorkDemo
{
    public static void main(String[] args)
    {
        Scanner myScanner = new Scanner(System.in);
        System.out.println("Please input a-g");
        char c1 = myScanner.next().charAt(0);
        switch(c1)
        {
            case 'a':
                System.out.println("Today is monday");
                break;
            case 'b':
                System.out.println("Today is Tuesday");
                break;
            case 'c':
                System.out.println("Today is Wednesday");
                break;
                //......
                default :
                System.out.println("Input Error");
        }
    }
}

细节讨论

  • 表达式数据类型,应和case后的常量类型一致,或者是可以自动转成相互比较的类型,比如输入的是字符,而常量是int

01:

public class SwitchWorkDemo
{
    public static void main(String[] args)
    {
        char c = 'a';
        switch(c)
        {
            case 'a':
                System.out.println("ok1");
                break;
                //correct char -> int 
            case 20:
                System.out.println("ok2");
                break;
                //error String 无法转成 char
            case 'hello':
                System.out.println("ok3");
                break;
            default:
                System.out.println("ok4");
        }
    }
}

02:

public class SwitchWorkDemo
{
    public static void main(String[] args)
    {
        String  c = "a";
        switch(c)
        {
            case "b":
                System.out.println("ok1");
                break;
            case "20":
                System.out.println("ok2");
                break;
                //error String 无法转成 int
            case 20:
                System.out.println("ok3");
                break;
            default:
                System.out.println("ok4");
        }
    }
}
  • switch(表达式)中表达式的返回值必须是(byte,short,int,char,enum[枚举],String)

01:

public class SwitchWorkDemo
{
    public static void main(String[] args)
    {
        double c = 1.1;
        switch(c)
        {
                //error switch(表达式)中表达式的返回值必须是(byte,short,int,char,enum[枚举],String)
            case 1.1:
                System.out.println("ok1");
                break;
            default:
                System.out.println("ok2");
                
        }
    }
}
  • case子句中的值必须是常量(1,‘a’)或者是常量表达式,而不能是变量
 class SwitchWork
{
    public static void main(String[] args)
    {
        char c = 'a';
        char c2 = 'c';
        switch(c)
        {
                //error case子句中的值必须是常量(1,'a')或者是常量表达式,而不能是变量
            case c2:
                System.out.println("ok");
                break;
            default:
                System.out.println("ok2");
                
        }
    }
}
  • default是可选的,就是说,可以没有default。如果没有default子句,有没有匹配任何常量,则没有输出

  • break语句用来在执行完一个case分支后使程序跳出switch语句块,如果没有写break,程序会顺序执行到switch结尾

public class SwitchDemo {
    public static void main(String[] args)
    {
      char c = 'b';
      char c2 = 'c';
      switch(c)
      {
          case 'b'://没有break,穿透了
              System.out.println("ok1");
          case 'c':
              System.out.println("ok2");
          default:
              System.out.println("ok3");
      }
    }

}

结果如下:

小练习

01:

import java.util.Scanner;
public class ConvertWork
{
    public static void main(String[] args)
    {
        Scanner myScanner = new Scanner(System.in);
        System.out.println("Please input a-e");
        char c1 = myScanner.next().charAt(0);
        switch(c1)
        {
            case 'a':
                System.out.println("A");
                break;
            case 'b':
                System.out.println("B");
                break;
            case 'c':
                System.out.println("C");
                break;
            case 'd':
                System.out.println("D");
                break;
            case 'e':
                System.out.println("E");
                break;
            default :
                VSCode自定义代码片段5——HTML元素结构

Android Java:在 onCreateView() 中返回空视图的片段

java 登录过程 - android片段,异步任务登录,Asp.net控制器,存储库

你可能不知道的JavaScript代码片段和技巧(下)

你可能不知道的JavaScript代码片段和技巧(上)

Java控制结构