java基础入门微专业

java基础入门微专业第 1 章用程序来做计算 1 1 第一个 Java 程序 Mac version Preference gt General gt Keys gt Search Content Assist for binding to the short key you want 1 2 用变量做计算 1 3 表达式 浮点数 优先级和类型转换 rounding happens to

大家好,我是讯享网,很高兴认识大家。



第1章--用程序来做计算

1.1 第一个Java程序

Mac version: Preference -> General -> Keys -> Search "Content Assist" for binding to the short-key you want.

1.2 用变量做计算

1.3 表达式(浮点数,优先级和类型转换)

rounding happens to floating number: (e.g. shown below)

  System.out.println(1.2-1.1);   output: 0.099987
讯享网

第2章--判断

2.1 作比较

comparison between int and double:

讯享网  int a = 5;   double b = 5.0;   System.out.println(a==b);   output: true

comparison between double and double:

  int a = 5;   double b = 5.0;   System.out.println(a==b);   output: true   int a = 1.0;   double b = 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1;   System.out.println(b == c);   System.out.println(b+"; "+c);   output: false   output: 1.0; 0.99999

solution: Math.abs(a-b) < 1e-6;

2.2 判断语句

2.3 多路分支

第3章--循环

3.1 循环(while和do-while循环)

do-while syntax: (cannot image that I even do not remember this)

讯享网  do {    // body   } while (condition);

3.2 for循环

3.3 循环控制(含复合赋值、逻辑类型)

using break with a label:similar to "goto" in c language

 blahblahblah; LABEL: for(int i = 0; i < 10; i++) { blahblahblah if (...) { break LABEL; // break the loop labelled "LABEL" } }

3.4 循环应用

tricks for integer processing:

得到个位数:%10

去掉最低位:/10

Formatted Print:

System.out.printf("%.2f", x);

第4章--数组

4.1 数组的创建和使用

syntax of defining and creating an array: // fine, haven't write Java for rly a long time

type[] name = new type[size]; // size could be a variable, but it must be provided

e.g: int[] i = new int[array_size];

with initialisation:

type[] name = new type[size];

e.g: int[] i = {1,2,3};

4.2 数组变量和运算

int[] a = new int[size];

initialise array a

int[] b = a; // b just points to the block data which a points to (same block of data)

a == b ? true

but what if a and b points to different blocks of memory?

int[] a = {1,2};

int[] b = {1,2};

a == b ? false

if we wanna compare the content of two arrays: solution--traversing and comparing each pair of element

if we wanna copy an array: solution--traversing and copying each element one by one

syntax for "for-each" loop to traverse an array:

for (type element: array) java基础入门微专业 {}

4.3 二维数组

syntax for creating, initialising, and modifying a two-dimensional array:

int[][] a = new int[size][size];

int[][] a = { {1,2,3,4}, {1,2} };

a[1][2] = 5;

第5章--函数

5.1 函数的定义和调用

函数有多个出口(多个return statements)--> bad design

5.2 函数的参数与本地变量

Java在调用函数时,永远只能传值给函数(?)

life-cycle of a local variable: inside the block

  e.g.

    {      int i;     }     i = 0; // ERROR

(END)

小讯
上一篇 2024-12-28 14:35
下一篇 2024-12-25 21:30

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/6411.html