java中语句结构总结
来源:互联网 发布:金马网络服装批发城 编辑:程序博客网 时间:2024/06/04 19:41
程序语句的三种结构
1.顺序结构
2.选择结构
3.循环结构
举例:验证选择结构
if语句
if...else语句
三目运算符
使用三目运算符
if ... else if ...else 语句
switch语句
while循环
do while循环
for循环
循环嵌套操作(最好2~3层即可,不要太多,否则太占内存)
乘法表格
使用阶梯形状变化
public class ForNesteDemo{
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();
}
}
}
break语句
可以中断for循环语句====以后的操作都不在执行了
continue语句
只中断一次循环的执行
1.顺序结构
2.选择结构
3.循环结构
3.1顺序结构
3.2选择结构
举例:验证选择结构
- public class IfDemo{
- public static void main(String args[]){
- int x = 3; //定义变量
- int y = 10; //定义变量
- System.out.println("===比较开始==");
- if(x>y){
- System.out.println("x比y大");
- }
- if(x<y){
- System.out.println("x比y小!");
- }
- }
- }
if语句
if...else语句
- public class IfElseDemo{
- public static void main(String args[]){
- int x = 3;
- if(x%2==1){
- System.out.println("x是奇数");
- }else{
- System.out.println("x是偶数");
- }
- }
- }
三目运算符
- public class MaxDemo{
- public static void main(String args[]){
- int max = 0;
- int x = 3;
- int y = 10;
- if(x<y){
- max = y;
- }else
- {
- max = x;
- }
- System.out.println("最大值为:" + max);
- }
- }
使用三目运算符
- public class MaxDemo{
- public static void main(String args[]){
- int max = 0;
- int x = 3;
- int y = 10;
- max = x>y ? x : y; //通过三目运算符
- System.out.println("最大值为:" + max);
- }
- }
if ... else if ...else 语句
- public class MoreIfElseDemo{
- public static void main(String args[]){
- int x = 3;
- if(x==1){
- System.out.println("x的值是1");
- }else if(x==2){
- System.out.println("x的值是3");
- }else if(x==3){
- System.out.println("x的值是1");
- }else{
- System.out.println("x不是1,2,3中的任何一个");
- }
- }
- }
switch语句
- 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;
- }
- }
- }
- }
3.3循环结构
while循环
- public class WhileDemo{
- public static void mian(String args[]){
- int x = 1;
- int sum = 0;
- while(x<=10){
- sum += x;
- x++; //修改循环条件
- }
- System.out.println("1 --> 10 的累加效果为:" + sum);
- }
- }
do while循环
- public class DoWhileDemo{
- public static void main(String args[]){
- int x = 1;
- int sum = 0;
- do{
- sum += x;
- x++;
- }while(x<=10);
- System.out.println("1 --> 10 的累加效果为:" + sum);
- }
- }
for循环
- public class ForDemo{
- public static void main(String args[]){
- int sum = 0;
- for(int x=1;x<=10;x++){
- sum += x;
- }
- System.out.println("x --> 10 的累加效果为:" + sum);
- }
- }
循环嵌套操作(最好2~3层即可,不要太多,否则太占内存)
乘法表格
- public class ForNesteDemo{
- public static void main(String args[]){
- for(int i=1;i<=9;i++){ //控制行
- for(int j=1;j<=9;j++){ //控制列
- System.out.print(i+"*"+j+"="+(i*j)+"\t");
- }
- System.out.println();
- }
- }
- }
使用阶梯形状变化
public class ForNesteDemo{
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();
}
}
}
3.4终端语句
break语句
可以中断for循环语句====以后的操作都不在执行了
- public class BreakDemo{
- public static void main(String args[]){
- for(int i=1;i<10;i++){
- if(i==3){
- break;
- }
- System.out.println("i = " + i);
- }
- }
- }
continue语句
只中断一次循环的执行
- public class ContinueDemo{
- public static void main(String args[]){
- for(int i=1;i<10;i++){
- if(i==3){
- continue;
- }
- System.out.println("i = " + i);
- }
- }
- }
0 0
- java中语句结构总结
- java 中 return 语句总结
- Java总结第二章(Java基本语句结构)
- linux bash中for条件语句结构总结
- 总结Java中return语句的用法
- 总结Java中return语句的用法
- 总结Java中return语句的用法
- java 中return语句的用法总结
- Java中switch case 语句总结
- Java结构语句
- Java基础和语句结构
- java语句的执行结构
- Java语句的执行结构
- Java分支结构-if语句
- Java分支结构switch语句
- 1.4-Java-选择结构语句
- Java选择结构if语句
- java基础语句总结
- iOS CADisplayLink 使用WSYFPSLabel监测程序FPS
- 零售户决策树分类
- UVA - 524 Prime Ring Problem
- 安卓给app系统签名
- ICPCCamp 2016 Day 1 - B All Pair Shortest Path (bitset)
- java中语句结构总结
- webapi返回泛型给easyui
- ThinkPHP 3.1.2 视图-1
- Objective - C CALayer的简介
- CDHtmlDialog的基本使用
- Git 换行符自动转换
- C语言基础函数 判断是否闰年
- Cracking the code interview总结
- VC屏幕截图并保存为bmp、jpg、png等格式文件