0.前言
本人在本科期间简单的接触过Java,但Java知识十分薄弱,为了更好的掌握Java相关知识,规范代码格式,之后的90天将根据闵帆老师的日撸 Java 三百行来学习Java,以打好基础,此博客用于记录学习的过程以及其中遇到的问题和对应的改善方法,以供日后参考。
1.Day01: 环境搭建及基础语句
1.1 Eclipse和jdk下载与安装与环境检测
具体步骤参考: https://blog.csdn.net/qq_39135287/article/details/82108080
检测环境是否配置成功:打开命令行窗口输入java -version,出现以下窗口即配置成功
1.2 基础语句
package:
一个为了方便组织管理的java文件目录结构,其可避免不同的java文件之间发生命名的冲突
注意package 要与所建的包名(即文件夹名)一致. 在使用package时, 如果java文件中使用了package,该java文件则必须放在命名与package名称相同的目录下. 不同package中的类的名字可以相同, 但在使用时要带上package的名称用于区分
import:
由于很多的package的名称非常长,编程中要使用一个类时,需要将多个包名.类名完全写出,会使得代码变得非常的冗长,为了提高简洁度,引入了import
import就是在java文件开头说明会用到那些类别,在代码中就可以只用类名来指定某个类
比如:(示例来源: java中import作用详解)
引入import前
java.io.InputStream is = java.lang.System.in;
java.io.InputStreamReader isr= new java.io.InputStreamReader(is);
java.io.BufferedReader br = new java.io.BufferedReader(isr);
引入import后
在文件头表明会使用到的类
import java.lang.System;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
在程序中就可以写为
InputStream = System.in;
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
println:
java中输出方式一般包含print、println、printf三种,它们都是java.long包里的System类中的方法
print:输出自动将括号中的内容换成字符串输出,如果括号中是一个对象,则会自动调用toString()方法,该方法输出不会换行
println:println的用法与作用和print基本相同,但println会在输出结果后面加上换行符
printf:沿用了其在C语言的部分输出方式,支持格式化输出,一般的格式有
%d 十进制整数
%f 十进制浮点数
%o 八进制数
%u 无符号的十进制数
%x 十六进制数
%c 单个字符
%s 字符串
System.out.print();//输出但不换行
System.out.println(); //输出后换行
System.out.printf(); //格式同c语言
1.3 编写HelloWorld.java
package basic;
public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello, world!");
}//Of main
}//Of class HelloWorld
输出:
2.Day02: 基础的加减乘除取余运算与println的中阶用法
package basic;
public class BasicOperations {
public static void main(String args[]) {
int tempFirstInt, tempSecondInt, tempResultInt;
double tempFirstDouble, tempSecondDouble, tempResultDouble;
tempFirstInt = 15;
tempSecondInt = 4;
tempFirstDouble = 1.2;
tempSecondDouble = 3.5;
// Addition
tempResultInt = tempFirstInt + tempSecondInt;
tempResultDouble = tempFirstDouble + tempSecondDouble;
System.out.println("" + tempFirstInt + " + " + tempSecondInt + " = " + tempResultInt);
System.out.println("" + tempFirstDouble + " + " + tempSecondDouble + " = " + tempResultDouble);
// Subtraction
tempResultInt = tempFirstInt - tempSecondInt;
tempResultDouble = tempFirstDouble - tempSecondDouble;
System.out.println("" + tempFirstInt + " - " + tempSecondInt + " = " + tempResultInt);
System.out.println("" + tempFirstDouble + " - " + tempSecondDouble + " = " + tempResultDouble);
// Multiplication
tempResultInt = tempFirstInt * tempSecondInt;
tempResultDouble = tempFirstDouble * tempSecondDouble;
System.out.println("" + tempFirstInt + " * " + tempSecondInt + " = " + tempResultInt);
System.out.println("" + tempFirstDouble + " * " + tempSecondDouble + " = " + tempResultDouble);
// Division
tempResultInt = tempFirstInt / tempSecondInt;
tempResultDouble = tempFirstDouble / tempSecondDouble;
System.out.println("" + tempFirstInt + " / " + tempSecondInt + " = " + tempResultInt);
System.out.println("" + tempFirstDouble + " / " + tempSecondDouble + " = " + tempResultDouble);
// Modulus
tempResultInt = tempFirstInt % tempSecondInt;
System.out.println("" + tempFirstInt + " % " + tempSecondInt + " = " + tempResultInt);
}// Of main
}// Of class BasicOperations
3.Day03: 基本if语句
3.1 if then else
package basic;
/**
* The usage of the if statement.
*
* @author Yunhua Hu yunhuahu0528@163.com.
*/
public class IfStatement {
/**
*********************
* The entrance of the program.
*
* @param args Not used now.
*********************
*/
public static void main(String args[]) {
int tempNumber1, tempNumber2;
// Try a positive value
tempNumber1 = 5;
if (tempNumber1 >= 0) {
tempNumber2 = tempNumber1;
} else {
tempNumber2 = -tempNumber1;
} // Of if
System.out.println("The absolute value of " + tempNumber1 + " is " + tempNumber2);
// Try a negative value
// Lines 27 through 33 are the same as Lines 15 through 19
tempNumber1 = -3;
if (tempNumber1 >= 0) {
tempNumber2 = tempNumber1;
} else {
tempNumber2 = -tempNumber1;
} // Of if
System.out.println("The absolute value of " + tempNumber1 + " is " + tempNumber2);
// Now we use a method/function for this purpose.
tempNumber1 = 6;
System.out.println("The absolute value of " + tempNumber1 + " is " + abs(tempNumber1));
tempNumber1 = -8;
System.out.println("The absolute value of " + tempNumber1 + " is " + abs(tempNumber1));
}// Of main
/**
*********************
* The absolute value of the given parameter.
*
* @param paraValue The given value.
*********************
*/
public static int abs(int paraValue) {
if (paraValue >= 0) {
return paraValue;
} else {
return -paraValue;
} // Of if
}// Of abs
}// Of class IfSatement
注意:
else前有空格
可通过sourse-format(Ctrl+shift+F)作格式化整理
3.2 注释问题
创建注释模板,具体设置参照:基本if语句与函数调用
4.总结
这三天了解了基本的Java语法和一些简单的函数,格式也逐渐熟悉。 还需提升代码编写的准确性,得多敲敲。