程序语句的三种结构
1.顺序结构
2.选择结构
3.循环结构
3.1顺序结构
3.2选择结构
举例:验证选择结构
[java] view
plaincopyprint?
- 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语句
[java] view
plaincopyprint?
- 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是偶数");
- }
- }
- }
三目运算符
[java] view
plaincopyprint?
- 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);
- }
- }
使用三目运算符
[java] view
plaincopyprint?
- 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 语句
[java] view
plaincopyprint?
- 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语句
[java] view
plaincopyprint?
- 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循环
[java] view
plaincopyprint?
- 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循环
[java] view
plaincopyprint?
- 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循环
[java] view
plaincopyprint?
- 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层即可,不要太多,否则太占内存)
乘法表格
[java] view
plaincopyprint?
- 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循环语句====以后的操作都不在执行了
[java] view
plaincopyprint?
- 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语句
只中断一次循环的执行
[java] view
plaincopyprint?
- 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);
- }
- }
- }
时间: 2024-10-23 11:14:08