Java常识 ------分支语句(for do---while while语句)

来源:互联网 发布:素颜美女知乎 编辑:程序博客网 时间:2024/05/21 10:31
//计算5的阶层
import java.util.Scanner;
class Dome_2 
{
public static void main(String[] args) 
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int i=1;
int sum=1;
while(i<=n){
sum*=i;
i++;
}
System.out.println(sum);
}

}

输出10-0

class Dome_3 
{
public static void main(String[] args) 
{
int x=10;
do{
System.out.println(x);
x--;
}while(x>=0);
}
}

输入一数  并计算阶层的和

import java.util.Scanner;
class Dome_4 
{
public static void main(String[] args) 
{
Scanner sc=new Scanner(System.in);
int yy=sc.nextInt();
int x=1;
int jc=1;
int sum=1;
do{
jc*=x;
sum+=jc;
x++;
}while(x<=yy);

System.out.println(sum);
}
}


计算1到100的和
class  Dome_While
{
public static void main(String[] args) 
{
int x=1,sum=0,sum1=0;
while(x<=100){
sum+=x;
    x++;
}
System.out.println(sum);
}
}

//100以内3的倍数不是5 的倍数
class Dome_6
{
public static void main(String[] args) 
{
int c=0;
for(int i=0;i<=100;i++){
if(i%3==0 && i%5!=0){
 c++;
 System.out.print(i+"\t");
 if(c%5==0){
 System.out.println();
 }
}
}
System.out.println("一共有"+c+"个");
}
}


一个图形

class  Dome_8
{
public static void main(String[] args) 
{
for(int i=1;i<=5;i++){
for(int j=5;j>=i;j--){
  System.out.print("==");
}
System.out.println();
}
}
}

一、分支结构
     1.if(){
        执行语句1; }
     2.if(){
     执行语句2;
        }elae{
     执行语句3;   }
     3.if(){
        执行语句4;
}else if(){
执行语句5;
}else if(){
执行语句6;
    }
switch    
    格式:
        SWitch(表达式){
 case 值1:
     语句体1;
     break;
  case 值2:
     语句体2;
     break;
          default://默认情况
      语句3;
 break;//停止循环
}
执行流程:先计算表达式的值,然后和case后面的常量比较是否相等。相等就执行这条语句。
switch(表达式)
  java1.6及之前 基本数据类型  byte  short  int  char  枚举
  java1.7之后  多了一个String
主:
  A: case 后面只能是常量,不能是变量,而且多个case后面的常量不能有重复。
  B:default:可以省略,一般不建议省略,他的作用是对输入不正常的时候给予提示,
String 类型作为switch的表达式
switch 语句default可以写在switch里面任意位置。先执行case  在执行default。
default在case前面没有break的时候  执行default之后 在向下执行  直到碰到}或break结束;






if --else  与switch --case  区别
       1.适用场景
要判断区间范围  用 if ---else
要判断定值  用switch---case
       2.语句的表达式值类型不同
          if(表达式)---->boolean true  false
 switch(表达式)--->byte  short  int  char  枚举  String








循环结构:
    重复的行为反复出现 就可以使用循环
    1、while循环
    2、do-while循环
    3、for循环
    
 1.while循环
     格式:while(判断条件语句){
            循环体语句
             }
      例:输出1到5
         int x=1;
          while(x<5){
 System.out.println(x);
 x++;
 }
2、do--while循环
      格式:
         do{
  循环体语句;
  控制条件语句;  
}while(判断条件语句);




先执行一次  在判断,判断结果为TRUE  在返回循环执行
如果判断结果为false,不执行。


for 循环
    格式:for(初始化语句;判断条件语句;控制条件语句){    
        循环体;
    }
    例:
       for(int i=0;i<10;i++){
       sum+=i;
       }
   i++   是在内存中先申请一片区域 先保存一下i+1  然后赋值给i;
   ++i   是直接给i赋值,效率高一点


   注意:1、判断条件语句  无论简单还是复杂,,结果必须是boolean.
         2、
写法:表达式1/2/3都可以省略(死循环)
      表达式1可以声明在循环外、表达式2没写就是死循环、表达式3可以声明在循环内部


三个循环的异同点:(for,while ,do--while) 
    1、do-while循环至少执行一次循环体
    2、for循环和while,初始化变量的作用域不同
         for循环只在循环体内可以使用
while在方法体中可以继续使用。


循环的嵌套(for 多次循环)
   循环内还有循环。
   格式:for(int i=1;i<9;i++){
   for(int j=1;j<i;j++){
   }
   }


   规律:尖朝下:可以改变初始值,让初始值随着外层循环变化。
         尖朝上:可以改变循环条件,让条件随着外层循环变化。






阅读全文
1 0
原创粉丝点击