闵老板帖子:  日撸 Java 三百行(01-10天,基本语法)


0. 前言

为了提升编程能力, 重拾"java"技术, 在此开贴学习.

1. 第一个程序

搭建好所需要的编程环境, new java project, new package, and new class, then start to code our first program.

package basic;public class hello {/*** ********************** The entrance of the program.** @param args Not used now.**********************/public static void main(String[] args) {System.out.println("hello java!");}// Of main
}// Of class hello

2. if...else 条件语句

语法如下:

if(布尔表达式)
{//如果布尔表达式为true将执行的语句
}

2.1 例: 比较7与输入数字的大小

package basic;import java.util.Scanner;public class test_if {/*** ********************** The entrance of the program.** @param args Not used now.**********************/public static void main(String[] args) {int input_number;System.out.println("please input your number:");Scanner scan = new Scanner(System.in);input_number = scan.nextInt();if (input_number > 7) {System.out.println("input_number is bigger than 7");} else {System.out.println("input_number is not bigger than 7");}// Of if}// Of main
}// Of class test_if

输出:

please input your number:
5
input_number is not bigger than 7

3. switch case 语句

语法如下:

switch(expression){case value ://语句break; //可选case value ://语句break; //可选//你可以有任意数量的case语句default : //可选//语句
}

3.1 例: 创建一个可以加减乘除的计算器

package basic;import java.util.Scanner;public class operation {/*** ********************** The entrance of the program.** @param args Not used now.**********************/public static void main(String args[]) {double firstnumber, secondnumber, result;char operation;System.out.println("please input your firstnumber :");Scanner scan = new Scanner(System.in);firstnumber = scan.nextDouble();System.out.println("please input your secondnumber :");Scanner scan1 = new Scanner(System.in);secondnumber = scan1.nextDouble();System.out.println("please choose select the operation:(+ or - or * or /)");Scanner scan2 = new Scanner(System.in);operation = scan.next().charAt(0);switch (operation) {// Additioncase '+': {result = firstnumber + secondnumber;System.out.println("firstnumber + secondnumber = " + result);break;}// Subtractioncase '-': {result = firstnumber - secondnumber;System.out.println("firstnumber - secondnumber = " + result);break;}// Multiplicationcase '*': {result = firstnumber * secondnumber;System.out.println("firstnumber * secondnumuber = " + result);break;}// Divisioncase '/': {result = firstnumber / secondnumber;System.out.println("firstnumber / secondnumuber = " + result);break;}default:System.out.println("default");}// Of switch}// Of main
}// Of class operation

输出:

please input your firstnumber :
5
please input your secondnumber :
2
please choose select the operation:(+ or - or * or /)
/
firstnumber / secondnumber = 2.5

4. 循环结构

java中主要有三种循环结构:

while( 布尔表达式 ) {//循环内容
}
do {//代码语句
}while(布尔表达式);
for(初始化; 布尔表达式; 更新) {//代码语句
}

4.1 例: 矩阵加法

package basic;import java.util.Arrays;public class matrix_add {/*** ********************** Create a matrix of size 3 * 4.* * @param None.* @return A matrix of size 3 * 4.**********************/public static int[][] create() {int[][] matrix = new int[3][4];for (int i = 0; i < matrix.length; i++) {for (int j = 0; j < matrix[0].length; j++) {matrix[i][j] = i * 5 + j;} // Of for j} // Of for ireturn matrix;}// Of create/*** ********************** Add two matrices.** @param matrix1 The first matrix.* @param matrix2 The second matrix. And it must have the same size as the first*                one's.* @return The Addition of matrices.**********************/public static int[][] add(int[][] matrix1, int[][] matrix2) {int[][] addmatrix = new int[3][4];for (int i = 0; i < matrix1.length; i++) {for (int j = 0; j < matrix1[0].length; j++) {addmatrix[i][j] = matrix1[i][j] + matrix2[i][j];} // Of for j} // Of for ireturn addmatrix;}// Of add/*** ********************** The entrance of the program.** @param args Not used now.**********************/public static void main(String[] args) {int[][] matrix1 = new int[3][4];int[][] matrix2 = new int[3][4];int[][] addmatrix = new int[3][4];matrix1 = create();matrix2 = create();System.out.println("The two matrices are: \n" + Arrays.deepToString(matrix1));addmatrix = add(matrix1, matrix2);System.out.println("After adding, the new matrix is: \n" + Arrays.deepToString(addmatrix));}// Of main
}// Of class matrix_add

输出:

The matrix is:
[[0, 1, 2, 3], [5, 6, 7, 8], [10, 11, 12, 13]]
After adding, the new matrix is:
[[0, 2, 4, 6], [10, 12, 14, 16], [20, 22, 24, 26]]

4.2 例:矩阵乘法

package basic;import java.util.Arrays;public class matrix_multiply {/*** ********************** Create a matrix of size row*column.** @param paraRow    The row of the matrix.* @param paraColumn The column of the matrix.* @return A matrix of size paraRow * paraColumn.**********************/public static int[][] create(int paraRow, int paraColumn) {int[][] matrix = new int[paraRow][paraColumn];for (int i = 0; i < matrix.length; i++) {for (int j = 0; j < matrix[0].length; j++) {matrix[i][j] = i * 5 + j;} // Of for j} // Of for ireturn matrix;}// Of create/*** ********************** Matrix multiplication. Must satisfy the matrix multiplication.** @param matrix1 The first matrix.* @param matrix2 The second matrix.* @return The result matrix.**********************/public static int[][] multiply(int[][] matrix1, int[][] matrix2) {int row1 = matrix1.length;int row2 = matrix2.length;int column1 = matrix1[0].length;int column2 = matrix2[0].length;int[][] matrix = new int[row1][column2];if (row2 != column1) {System.out.println("The two matrices do not satisfy the matrix multiplication.");return null;} // Of iffor (int i = 0; i < row1; i++) {for (int j = 0; j < column2; j++) {for (int k = 0; k < row2; k++) {matrix[i][j] += matrix1[i][k] * matrix2[k][j];} // Of for k} // Of for j} // Of for ireturn matrix;}// Of multiply/*** ********************** The entrance of the program.** @param args Not used now.**********************/public static void main(String[] args) {int[][] matrix1, matrix2, matrix;matrix1 = create(3, 4);matrix2 = create(4, 3);System.out.println("The first matrix is :\n" + Arrays.deepToString(matrix1));System.out.println("The first matrix is :\n" + Arrays.deepToString(matrix2));matrix = multiply(matrix1, matrix2);System.out.println("After multiplying, matrix is :" + Arrays.deepToString(matrix));}// Of main
}// Of class matrix_multiply

输出:

The first matrix is :
[[0, 1, 2, 3], [5, 6, 7, 8], [10, 11, 12, 13]]
The second matrix is :
[[0, 1, 2], [5, 6, 7], [10, 11, 12], [15, 16, 17]]
After multiplying, matrix is :[[70, 76, 82], [220, 246, 272], [370, 416, 462]]

Java学习(01-10天, 基本语法)相关推荐

  1. JAVA学习脚印10:解惑java 中UTF-16与char

    JAVA学习脚印10:解惑java 中UTF-16与char java中的char.utf-16编码.代码点.代码单元等概念,做一个了解还是有必要的. 1.基本概念 1) Java的字符类型和字符串类 ...

  2. java学习-01 java历程以及java学习环境配置

    01 Java的历程 前言: ​ 最近在学习Java,之前学习过挺久的Python,现在想来,对很多基础地方都不是很清晰,原因就是没有养成良好的笔记记录习惯,因此,这次学习Java准备从基础开始记录笔 ...

  3. Java学习lesson 10

    API(应用程序编程接口) *  public final Class getClass();//返回Object的运行类(java的反射机制学) *  Class类中有一个类 *public Str ...

  4. JAVA学习 11.10

    Java第一天 1.1 Java发展史 1995年,SUN虽然推出了Java . 詹姆斯 高瑟林[java之父]带领自己的团队研发了java编程语言. 后来有一个公司就整了一个开发软件 Eclipse ...

  5. Java学习_Day 10(学习内容:尚硅谷集合JAVA零基础P523-P533)

    P523 集合-使用Iterator遍历Collection package com.collection;import org.junit.Test;import java.util.ArrayLi ...

  6. Java学习笔记10

    当程序创建对象.数组等引用类型实体时,系统都会在堆内存中为之分配一块内存区,对象就保存在这块内存区, 当我们创建的对象不再被引用时,所在的内存就变成了垃圾,最后等待垃圾回收机制进行回收,Java的垃圾 ...

  7. java学习(10):数据类型

    对于java的数据类型,既熟悉又陌生,于是整理了这篇文档. 最近的面试让我开始注意细节,细节真的很重要. 首先,我们知道在JAVA中一共有八种基本数据类型,他们分别是 byte.short.int.l ...

  8. Java学习笔记10(面向对象三:接口)

    接口: 暂时可以理解为是一种特殊的抽象类 接口是功能的集合,可以看作是一种数据类型,是比抽象类更抽象的"类" 接口只描述所应该具备的方法,并没有具体实现,具体实现由接口的实现类(相 ...

  9. Java学习笔记10(零压力理解继承多态权限修饰符)

    文章目录 继承 方法的重写(override) 四种访问权限修饰符: 关键字super 类对象的实例化的底层原理 多态 instanceof操作符 object类 继承 继承是Java最重要的,类之间 ...

  10. 【Java学习】10入门篇之综合实战(对象、IO流、方法等)

    问题描述:3v3游戏,每次开始游戏前先检查玩家列表,有过列表为空则向里面添加游戏玩家.游戏结束后,如果是新添加的游戏玩家,则写入到玩家列表当中,如果不是,结束操作. 创建游戏玩家对象: public ...

最新文章

  1. 关于Android学习
  2. 虚拟机里安装Linux系统出现乱码
  3. 转帖一篇:截取密码(VC++)学习消息VC++的好处
  4. Linux Kernel中spinlock的设计与实现
  5. vue-cli css文件图片路径写法
  6. 【quartz】执行一次功能
  7. 博客园 页面定制CSS代码
  8. [svc]cfssl模拟https站点-探究浏览器如何校验证书
  9. 一步步编写操作系统 10 cpu的实模式
  10. 5G牌照提前发放 将对整个产业界带来哪些影响?
  11. SQL STUFF用法很有趣的语法
  12. JSP内置对象之九——config
  13. 雷达的工作原理示意图_雷达的荧光屏真的相当于蝙蝠的耳朵吗
  14. Mac配置adb笔记,彻底解决zsh: command not found: adb问题
  15. 身份证读取设备开发解决方案:3、单片机读取身份证信息的demo
  16. ADADELTA: AN ADAPTIVE LEARNING RATE METHOD
  17. PS安装失败解决方法
  18. html css做椭圆,简单实例:用css3画椭圆
  19. BZOJ4378[POI2015]Logistyka——树状数组
  20. MPEG音频文件格式(包括MP3文件格式)详解

热门文章

  1. 【技术专题研究】OSPF的LSA类型
  2. 【TDA4系列】向 PSDKRA 添加新的图像传感器
  3. 凸优化第五章对偶 作业题
  4. Eventbus收录
  5. for/map循环里面进行异步操作async/await后返回数据,for里不能直接return执行方法函数...
  6. ES6 Generator 函数
  7. Android开发 - 掌握ConstraintLayout(一)传统布局的问题 1
  8. [另类应用]让SQL Profiler拦不到你的SQL
  9. itext poi 学习之旅 (3)读取数据库信息并由excel展现出来
  10. 建立域信任关系后,查找位置中看不到另一个域的信息