第2章 结构化程序设计

Java系列文章

第1章 绪论
第2章 结构化程序设计
第4章 数组、字符串、向量
第6章 异常处理、递归

1、Java标识符和关键字的概念。

标识符

标识符可以用来表识文件名变量名类名和类里的方法名

组成
java字母 + java数字
注意

1.首字符必须是Java字母
2.所有标识符是区分大小写的
3.除去关键字、false、true和null |

Java字母

A~Z, a~z,下划线“_ ”,美元符号“$”,等

Java数字

0~9,等

标识符

在这里插入图片描述

2、Java的数据类型包括哪些?每一种数据类型占用的位数是多少?定义方式是怎样?

每一种数据类型占用的位数(即内存空间)是固定的
不依赖于具体的计算机。

在这里插入图片描述

boolean
8位,truefalse

char
16位,0~65535

byte
8位,-128~+127

short
16位,-32768~+32767

int
32long
64float
32double
64

3、Java的算术运算符、关系运算符、布尔逻辑运算符、赋值运算符。

在这里插入图片描述
运算符与其他语言类似
我认为特殊的一点在于**,短路规则**
在这里插入图片描述

example:

public class logic {
	public static void main(String[] args) {
	 	int month = 8;
		int day =1;
		//条件或,采用短路规则,只判断第一个表达式
		if((month==8)||(++day<15))
			System.out.println("Month"+month+", day="+day);
		//逻辑或,不采用短路规则,两个表达式都计算了	
		if((month==8)|(++day<15))
			System.out.println("Month"+month+", day="+day);
输出结果

在这里插入图片描述

4、if、if /else、switch语句的用法。

大致分为两种

1.if…else

if(布尔表达式){
   //如果布尔表达式的值为true
}else{
   //如果布尔表达式的值为false
}
public class Test {
 
   public static void main(String args[]){
      int x = 30;
 
      if( x < 20 ){
         System.out.print("这是 if 语句");
      }else{
         System.out.print("这是 else 语句");
      }
   }
}

2.if…else if…else

if(布尔表达式 1){
   //如果布尔表达式 1的值为true执行代码
}else if(布尔表达式 2){
   //如果布尔表达式 2的值为true执行代码
}else if(布尔表达式 3){
   //如果布尔表达式 3的值为true执行代码
}else {
   //如果以上布尔表达式都不为true执行代码
}
public class Test {
   public static void main(String args[]){
      int x = 30;
 
      if( x == 10 ){
         System.out.print("Value of X is 10");
      }else if( x == 20 ){
         System.out.print("Value of X is 20");
      }else if( x == 30 ){
         System.out.print("Value of X is 30");
      }else{
         System.out.print("这是 else 语句");
      }
   }
}

5、for、while、do-while语句的用法。

for

for(初始化; 布尔表达式; 更新) {
    //代码语句
}
public class Main {
    public static void main(String[] args) {
        int[] intary = { 1,2,3,4};
        forDisplay(intary);
        foreachDisplay(intary);
    }
    public static void forDisplay(int[] a){  
        System.out.println("使用 for 循环数组");
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println();
    }
  }

while

while( 布尔表达式 ) {
  //循环内容
}
public class Test {
   public static void main(String[] args) {
      int x = 10;
      while( x < 20 ) {
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }
   }

do-while

do {
       //代码语句
}while(布尔表达式);
public class Test {
   public static void main(String[] args){
      int x = 10;
 
      do{
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }while( x < 20 );
   }
}

6、break语句、continue语句的用法。

break

public class Main {
    public static void main(String[] args) {
        int[] intary = { 99,12,22,34,45,67,5678,8990 };
        int no = 5678;
        int i = 0;
        boolean found = false;
        for ( ; i < intary.length; i++) {
            if (intary[i] == no) {
                found = true;
                break;
            }
        }
        if (found) {
            System.out.println(no + " 元素的索引位置在: " + i);
        } 
        else {
            System.out.println(no + " 元素不在数组中");
        }
    }
}

continue

public class Main {
    public static void main(String[] args) {
        StringBuffer searchstr = new StringBuffer("hello how are you. ");
        int length = searchstr.length();
        int count = 0;
        for (int i = 0; i < length; i++) {
            if (searchstr.charAt(i) != 'h')
            continue;
            count++;
            searchstr.setCharAt(i, 'h');
        }
        System.out.println("发现 " + count 
        + " 个 h 字符");
        System.out.println(searchstr);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值