您的位置 首页 java

Java 入门知识学习笔记二

第4章 流程控制

一、条件语句

1、if语句

if(布尔表达式){

语句序列

}

/*

布尔表达式如果为true则执行if代码块中的语句序列;

反之则不执行。

*/

2、if…else语句

if(布尔表达式){

语句序列

}

else{

语句序列

}

/*

布尔表达式如果为true则执行if代码块中的语句序列;

反之则执行else代码块中的语句序列。

如果一段代码中有多处if..else,则每一个else总是与它上面最近的一个if相匹配。

*/

/*

创建GetTerm类,在主方法中定义变量x,从控制台输入接受赋值,并通过if…else if多分支语句判断x的值决定输出结果。

*/

import java.util.Scanner;

public class GetTerm {

public static void main(String[] args){

Scanner scan=new Scanner(System.in);

System.out.println(“请输入一个整数:”);

int x=scan.nextInt();

if(x>1000){

System.out.println(“x的值大于1000”);

}else if(x>500){

System.out.println(“x的值大于500,但小于1000”);

}else if(x>100){

System.out.println(“x的值大于100,但小于500”);

}else {

System.out.println(“x的值小于100”);

}

scan.close();

}

}

3、 switch 多分支语句

switch(表达式){ //表达式的值必须是整型或者字符型

case 常量值1: //这里常量值必须与表达式的值对应,下同

语句序列1

break ; //跳出switch语句块,如没有则会继续执行下一句

case 常量值2:

语句序列2

break;

default: //当上述各case的常量值没有匹配时候则从此处执行

语句序列;

break;

}

import java.util.Scanner;
public class Switch_Example {
 public static void main(String[] args){
 Scanner scan=new Scanner(System.in);
 System.out.println("请输入你的姓名:");
 String name=scan.nextLine();
 System.out.println("请输入你的专业编号(0~9):");
 int majorLanguage=scan.nextInt();
 switch(majorLanguage){
 case 0:
 case 1:
 case 2:
 System.out.println("恭喜你,"+name+"你被分配到办公室");
 break;
 case 3:
 case 4:
 case 5:
 case 6:
 System.out.println("恭喜你,"+name+"你被分配到总工办");
 break;
 case 7:
 case 8:
 case 9:
 System.out.println("恭喜你,"+name+"你被分配到财务处");
 break;
 default:
 System.out.println("抱歉,你未被录用"); 
 }
 scan.close();
 }
} 

二、 循环语句

1、 while 语句

while( 条件表达式 ){ //条件表达式必须为布尔类型 语句序列; //条件表达式为true则执行该语句序列} //否则退出循环 

2、do…while语句

do {
 语句序列;
}while(条件表达式) /*与while语句的区别可以从顺序上理解出,即便条件表达式为 false ,也会执行一次do代码块里的指令*/ 

3、for语句

for(表达式1;表达式2;表达式3){
 语句序列;}/*表达式1:初始化工作
表达式2:为布尔类型,用于判断循环条件
表达式3:改变循环条件判断参数表达式2:为布尔类型,用于判断循环条件
表达式3:改变循环条件判断参数执行流程:先通过表达式1进行初始化设置工作,再判断表达式2,如果为true则执行语句序列,再通过表达式3改变循环条件判断参数;进入第二次循环时候先判断表达式2,如果为true则执行语句序列,再通过表达式3改变循环条件判断参数;直至某次判断表达式2为false则跳出循环。*/ 

//用for循环输出九九乘法表

public class MultiTable {

public static void main(String[] args){

for(int i=1;i<10;i++){

for(int j=1;j<i+1;j++){

System.out.println(j+”*”+i+”=”+j*i+” “);

}

System.out.println(); //换行

}

}

}

4、 foreach 语句

for(元素变量x:遍历对象obj){
 语句序列(一般会对x有操作)}/*foreach是for语句的特殊简化格式,从上述示例代码可以看出foreach并不是所用的关键字,而是习惯上对这种循环的称谓。*/ 

//使用foreach语句读取一位整型数组各元素

public class Repetition {

public static void main(String[] args){

int arr[]={1,3,4,7,8,10};

System.out.println(“一维数组中各元素分别为:”);

for(int x:arr){

System.out.println(x+”t”);

}

}

}

三、跳转语句

1、break语句

break只可以用在switchforwhiledo..while循环语句中,用于强行退出整个循环体。

2、continue语句

continue只能用在forwhiledo…while循环语句中,用于直接跳出当前循环进入下一次循环。

//计算1~100各偶数之和 2 public class ContinueDemo {

public static void main(String[] args){

for(int sum=0,i=0;i<101;i++){

if(i%2==0){

sum+=i;

}else {

continue;

}

if(i==100){

System.out.println(“0~100内偶数之和是”+sum);

}

}

}

}

public class CatchBird {

public static void main(String[] args){

String[] birdArray=new String[]{“麻雀”,”鸽子”,”百灵”,”布谷”,”老鹰”,”鹦鹉”,”老鹰”,”翠鸟”,”斑鸠”,”老鹰”,”黄鹂”};

int laoYingNumber=0;

for(String birdString:birdArray){

if(birdString.equals(“老鹰”)){

System.out.println(“一只老鹰已经被我抓起来了”);

laoYingNumber++;

continue;

}

System.out.println(“发现了一只”+birdString); //这里当然也可以用if-else

}

System.out.println(“总共抓住了”+laoYingNumber+”只老鹰”);

}

}

//计算1+1/2!+1/3!+...+1/20!的值 public class x1 { public static void main(String[] args){ double sum=0; long a=1L; for(long i=1L;i<21;i++){ for(long j=1L;j<=i;j++){ a=a*j; } sum+=1/(double)a; //强制浮点运算 a=(long)1; //重置为长整型数值12 System.out.println(sum); } } } 

第5章 数组

文章来源:智云一二三科技

文章标题:Java 入门知识学习笔记二

文章地址:https://www.zhihuclub.com/181192.shtml

关于作者: 智云科技

热门文章

评论已关闭

9条评论

  1. Roberge CJ, Gaudry M, de MГ©dicis R, Lussier A, Poubelle PE, Naccache PH Increased peak systolic velocity 10 cm sec is one of such parameters which has been advocated

  2. A study in healthy adult male volunteers n 7 that received clinically relevant doses for 9 days suggests that cefepime is not accumulated in the body 2, B and supplementary Fig

  3. For tamoxifen inducible mice, we injected adult mice 8 weeks old with 75 mg kg of tamoxifen i Although most of the research reviewed in this document was not in the setting of weight reduction interventions, given that 1 in 3 adults in the United States is obese, understanding how meal frequency and timing influence energy balance may be helpful clinically

  4. Genomic DNA was analyzed for V H to DJ H rearrangements as described in the Methods section

  5. The specific regulation of visceral fat lipolysis in OGT AKO mice can be at least partially attributed to the unique O GlcNAc signaling dynamics in visceral fat

  6. Monitor Closely 2 acemetacin decreases effects of labetalol by pharmacodynamic antagonism Warm After Eating Blood Pressure Lower

  7. Goldstein SR, Nachtigall M, Snyder JR, Nachtigall L 1990 Endometrial assessment by vaginal ultrasonography before endometrial sampling in patients with postmenopausal bleeding

网站地图