zoukankan      html  css  js  c++  java
  • Java学习--循环语句1

    1. break

    public class BreakDemo{
    // 完成一个四则运算的功能
    public static void main(String args[]){
    for(int i=0;i<10;i++){
    if(i==3){
    break ;         //跳出整个的循环
    }
    System.out.println("i = " + i) ;
    }
    }
    };

    运行结果:

    i = 0
    i = 1
    i = 2

    2. continue

    public class ContinueDemo{
    // 完成一个四则运算的功能
    public static void main(String args[]){
    for(int i=0;i<10;i++){
    if(i==3){
    continue ;
    }
    System.out.println("i = " + i) ;
    }
    }
    };

    运行结果:

    i = 0
    i = 1
    i = 2
    i = 4
    i = 5
    i = 6
    i = 7
    i = 8
    i = 9

    3.

    public class DoWhileDemo{
    // 完成一个四则运算的功能
    public static void main(String args[]){

    int x = 1;
    int sum = 0 ; // 保存累加的结果
    do{
    sum += x ; // 执行累加操作
    x++ ;
    }while(x<=5) ;
    System.out.println("1 --> 10 累加的结果为:" + sum) ;
    System.out.println("x的结果为:" + x ) ;

    }

    };

    运行结果:

    1 --> 10 累加的结果为:15
    x的结果为:6

    4.

    public class ForDemo{
    // 完成一个四则运算的功能
    public static void main(String args[]){
    int sum = 0 ; // 保存累加的结果
    for(int x=1;x<=10;x++){
    sum += x ;
    }
    System.out.println("1 --> 10 累加的结果为:" + sum) ;
    }
    };

    运行结果:

    1 --> 10 累加的结果为:55

    5.

    public class ForNestedDemo{
    // 完成一个四则运算的功能
    public static void main(String args[]){
    for(int i=1;i<=9;i++){ // 控制行
    for(int j=1;j<=i;j++){ // 控制列
    System.out.print(i+"*"+j+"="+(i*j)+" ") ;
    }
    System.out.println() ;
    }
    }
    };

    运行结果

    1*1=1
    2*1=2 2*2=4
    3*1=3 3*2=6 3*3=9
    4*1=4 4*2=8 4*3=12 4*4=16
    5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
    6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
    7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
    8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
    9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81

    6.

    public class IfDemo{
    public static void main(String args[]){
    int x = 3 ; // 定义整型变量3
    int y = 10 ; // 定义整型变量10
    System.out.println("===== 比较开始 =====") ;
    if(x>y){
    System.out.println("x比y大!");
    }
    if(x<y){
    System.out.println("x比y小!") ;
    }
    System.out.println("===== 比较完成 =======") ;
    }
    };

    运行结果:

    ===== 比较开始 =====
    x比y小!
    ===== 比较完成 =======

    7.

    public class IfElseDemo{
    public static void main(String args[]){
    int x = 3; // 定义整型变量x
    if(x%2==1){ // 判断于是是否为1
    System.out.println("x是奇数!") ; // 如果余数为1表示奇数
    }else{
    System.out.println("x是偶数!") ; // 如果余数为0表示是偶数
    }
    }
    };

    运行结果:

    x是奇数!

    8.

    public class MaxDemo{
    public static void main(String args[]){
    int max = 0 ; // 保存最大值
    int x = 3; // 定义整型变量x
    int y = 10 ;
    max = x>y?x:y ; // 通过三目运算符求出最大值
    System.out.println("最大值为:" + max) ;
    }
    };

    运行结果:

    最大值为:10

    9.

    public class MoreIfElseDemo{
    public static void main(String args[]){
    int x = 5; // 定义整型变量x
    if(x==1){
    System.out.println("x的值是1!") ;
    } else if(x==2){
    System.out.println("x的值是2!") ;
    }else if(x==3){
    System.out.println("x的值是3!") ;
    }else{
    System.out.println("x的值不是1、2、3中的一个!") ;
    }
    }
    };

    运行结果:

    x的值不是1、2、3中的一个!

    10.

    public class SwitchDemo01{
    // 完成一个四则运算的功能
    public static void main(String args[]){
    int x = 3 ;
    int y = 6 ;
    char oper = '-' ;
    switch(oper){
    case '+':{ // 执行加法操作
    System.out.println("x + y = " + (x + y )) ;
    break ;
    }
    case '-':{ // 执行减法操作
    System.out.println("x - y = " + (x - y )) ;
    break ;
    }
    case '*':{ // 执行乘法操作
    System.out.println("x * y = " + (x * y )) ;
    break ;
    }
    case '/':{ // 执行除法操作
    System.out.println("x / y = " + (x / y )) ;
    break ;
    }
    default:{
    System.out.println("未知的操作!") ;
    break ;
    }
    }
    }
    };

    运行结果

    x - y = -3

    11. 与10相比少了break

    public class SwitchDemo02{

    // 完成一个四则运算的功能
    public static void main(String args[]){
    int x = 3 ;
    int y = 6 ;
    char oper = '*' ;
    switch(oper){
    case '+':{ // 执行加法操作
    System.out.println("x + y = " + (x + y )) ;
    }
    case '-':{ // 执行减法操作
    System.out.println("x - y = " + (x - y )) ;
    }
    case '*':{ // 执行乘法操作
    System.out.println("x * y = " + (x * y )) ;
    }
    case '/':{ // 执行除法操作
    System.out.println("x / y = " + (x / y )) ;
    }
    default:{
    System.out.println("未知的操作!") ;
    }
    }
    }
    };

    运行结果:

    x * y = 18
    x / y = 0
    未知的操作!

  • 相关阅读:
    字体符号版面设计
    有人嘲笑我ps技术不够好@罗小白
    浅谈UI:
    色彩基础:
    常用的Mysql数据库操作语句大全
    汇编(坑逼之路)
    Linux学习笔记|扬帆
    坑爹的C++要课堂检测了 然而我什么都没学
    why I need a flow learn note.
    burpsuite
  • 原文地址:https://www.cnblogs.com/fenr9/p/5368142.html
Copyright © 2011-2022 走看看