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)+"\t") ;
}
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
未知的操作!

Java学习--循环语句1的更多相关文章

  1. Java学习--循环语句

    1. break public class BreakDemo{ // 完成一个四则运算的功能 public static void main(String args[]){ for(int i=0; ...

  2. oracle学习--循环语句

    oracle学习--循环语句  loop循环: create or replace procedure pro_test_loop is i number; begin i:=0; loop i:=i ...

  3. Java笔记——循环语句

    Java笔记--循环语句     1. while语句 规律: 1. 首先计算表达式的值. 2. 若表达式为真,则执行循环语法,直至表达式为假,循环结束.   while(表达式) 语句; 例如: i ...

  4. 《Java基础——循环语句》

    Java基础--循环语句       1. while语句: 规则: 1. 首先计算表达式的值. 2. 若表达式为真,则执行循环语法,直至表达式为假,循环结束.   格式: while(表达式) 语句 ...

  5. java学习之语句结构

    在java语言当中存在4中语句结构,分别是: 1.顺序结构 2.判断结构 3.选择结构 4.循环结构 一.顺序结构: 所谓的顺序结构,也就是当不指定其他三种语句结构的情况下,语句是从上往下依次执行的, ...

  6. Java 【循环语句】

    一.java循环语句分支 二.for循环 在java中for循环和C的循环用法一样 public class demo{ public static void main(String[] args){ ...

  7. java while循环语句

    //循环语句 //符合条件,循环继续执行,否则循环退出. //特点: //先判断,后执行 public class Test16{ public static void main(String arg ...

  8. c#基础;初步学习循环语句

    循环语句就是 在满足循环条件的情况下会有顺序的执行循环体 循环语句:for   :    while    :     foreach:三种. 循环语句 必须具备四要素:初始条件.循环条件.循环体.状 ...

  9. Java的循环语句

    一.while 循环 while(循环条件){ 循环操作语句 } * 循环3要素: 变量的初值.变量的判断.变量的更新 * 缺少循环变量的更新,循环将一直进行下去 public class Whlie ...

随机推荐

  1. CButtonST|CUniButton等按钮类的使用

    CButtonST CButtonST类的使用参考链接:http://www.cnblogs.com/lidabo/archive/2012/12/17/2821122.html CCeButtonS ...

  2. codeforces 数字区分 搜索

    Jokewithpermutation Input file: joke.inOutput file: joke.outJoey had saved a permutation of integers f ...

  3. 语法分析器初步学习——LISP语法分析

    语法分析器初步学习——LISP语法分析 本文参考自vczh的<如何手写语法分析器>. LISP的表达式是按照前缀的形式写的,比如(1+2)*(3+4)在LISP中会写成(*(+ 1 2)( ...

  4. python 打开文件对话框 filedialog tkinter GUI 编程

    - -读取文件的gui编程 # _*_ coding:utf-8 _*_ import tkinter from tkinter import filedialog def openfiles2(): ...

  5. EXISTS 和 IN 的区别

    exists子句的用法 select * from 表1 where exists (select * from 表2 where 表1.列名=表2.列名); exists子句返回的结果并不是从数据库 ...

  6. note:debugging requires the debug connect session system privilege

    note:debugging requires the debug connect session system privilege 解决方案: 原因是用户权限不够,使用以下命令授予权限: GRANT ...

  7. SpringMVC学习笔记:表单提交 参数的接收

    SpringMVC可以接收原生form表单和json格式数据 有一个名为Book的model,其中的属性如下: 字符串类型的name,数字类型的price,数组类型的cover,集合类型的author ...

  8. C# 遇到 which has a higher version than referenced assembly

    当C#遇到这种提示: which has a higher version than referenced assembly, 说明有两个或多个工程引用的dll的版本有出现不一样, 如: A工程引用l ...

  9. 2017/2/16:自己ajax+json习惯性写法 代码拼接的写法 +json用post提交乱码的原因

    1.先导入jquery的包 2.ajax的写法跟注意点 返回一个list的写法 代码拼接写法: html层: 2.script处 4:在你前面传递参数的时候没有遇到乱码问题的情况下,你使用json并且 ...

  10. 【C#】解析C#程序集的加载和反射

    目录结构: contents structure [+] 程序集 程序集的加载 发现程序集中的类型 反射对类型成员的常规操作 发现类型的成员 创建类型的实例 绑定句柄减少进程的内存消耗 解析自定义特性 ...