java学习day1-day3

65 阅读2分钟

来源:我的博客

day1 环境搭建

1.1 开发环境

在搭建java环境时,从官网下载了java安装包安装成功后,一定要正确配置环境变量

1.2 package import 和 println

(1)package包:就好似我们日常生活中的”收纳盒“,不同的”收纳盒“装不同的”物品“,方便我们查找和定位。在大”收纳盒“中有包含各种小”收纳盒“,这也体现了包是以树形结构存储的。

package com //一个主包
package com.project //主包下建一个项目工程包
package com.project.util //一个工具包,util工具包在com目录下的project目录中

(2)import:导入包成员,在写一个java类时,我们需用到其他包中的成员,此时就需要通过import导入相应的包,则就好似c语言需要导入头文件,才能用一些库里的函数

import java.util.ArrayList;; //导入Java util包下的ArrayList 那则可以用这个类中一些方法,变量
import java.util.*; //导入util包下所有的东西,那我们使用的范围就比上面这个更多

(3)println:打印输出语句,且在输出后会自动换行,若不换行则是print

1.3 编写HelloWorld.java

main函数是程序入口

package basic;
public class HellowWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World");
    }
}

day2 基本算术操作

2.1 加、减、乘、除、整除、取余.

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);
    }
}

在这里插入图片描述

day3 基本if 语句

3.1 if条件分支语句

其中,if中的表达式应该为布尔表达式。这里会存在三种不同选择。(假设在if中会有数据的处理)

第一,只使用if语句,这相当于我只过滤我想要的数据;

第二:if...else 语句,(不入if就进else, 非真即假)

第三:if…else if…else 语句,这就是多条件分支判断。对不同条件进行判断

3.2 代码

package basic;

public class IfStatement {
    /**
     * The entrance of the program
     * @param args
     */
    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));
    }

    /**
     * @param paraValue The given value.
     * @return  The absolute value of the given parameter.
     */
    public static int abs(int paraValue) {
        if (paraValue >= 0) {
            return paraValue;
        } else {
            return -paraValue;
        }
    }
}

在这里插入图片描述