日撸 Java 三百行 (Day01 Java基础)

序言:我本科期间学习过Java程序与设计这门课程,由于时间久远,有些地方已经遗忘。现跟着闵帆老师重拾Java基础,同时学习老师的代码书写格式。

案例一

输出 Hello word

package com.day01;

/**
 * This is the first code. 
 * @author Haitao [email protected]
 */
public class Day01 {

    public static void main(String args[]) {
        System.out.println("Hello, world!");
    }//Of main
}//Of class Day01

运行效果

分析:

  • package是Java中一个重要概念:包。package的作用是为了防止不同java文件之间发生命名冲突,不同package中的类可以重名,只需提前声明该类属于哪个package就行。
  • 一个Java文件只能有一个public类,其类名要与文件名相同(如下图),这样虚拟机在找某个类的时候就直接找与这个类名字相同的文件即可。    
  • main方法是程序的入口,被static修饰是一个静态方法,只能调用同样被static修饰的,若非static修饰则不能直接调用,必须使用new关键字创建对象来调用。main方法无返回值,前面要加void。

 

 

  • 输出语句System.out.println(),输出格式为换行输出
  • 输出语句System.out.print(),输出格式为不换行输出


案例二

基本算数操作

package com.day01;

/**
 * This is the second code.
 * @author Haitao [email protected]
 */

public class Day01 {

    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 Day01

运行结果

日撸 Java 三百行 (Day01 Java基础)_第1张图片

 分析

  • 基本的运算加、减、乘、除,需要注意的是除法中%是取模运算,/是取整运算
  • 用不同类型的数据做基本运算操作,结果会自动转化成操作集里的最高精度类型,

   日撸 Java 三百行 (Day01 Java基础)_第2张图片

      若用Int加上Double(如上图),则结果会自动转化为精度较高的Double。



案例三

基本if语句

                                                   日撸 Java 三百行 (Day01 Java基础)_第3张图片       

if ...else... 语句

                   日撸 Java 三百行 (Day01 Java基础)_第4张图片

if...else if....else 语句

                        日撸 Java 三百行 (Day01 Java基础)_第5张图片

 案列代码

package com.day01;

/**
 * The usage of the if statement.
 *
 * @author Haitao [email protected]
 */

public class Day01 {

    /**
     *********************
     * 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 Day01

 运行结果

日撸 Java 三百行 (Day01 Java基础)_第6张图片

 应用

闰年的计算

分析

  1. 闰年判断方法:年份能被4整除且不能被100整除;年份能被400整除(满足其一即可)
  2. isLeapYear()方法涉及逻辑运算符,返回值为布尔类型
  3. isLeapYearV2()方法涉及if语句的嵌套,返回值为布尔类型
package com.day01;

/**
 * The complex usage of the if statement.
 *
 * @author Haitao [email protected]
 */

public class Day01 {

    /**
     *********************
     * The entrance of the program.
     *
     * @param args Not used now.
     *********************
     */
    public static void main(String args[]) {
        // Test isLeapYear
        int tempYear = 2021;

        System.out.print("" + tempYear + " is ");
        if (!isLeapYear(tempYear)) {
            System.out.print("NOT ");
        } // Of if
        System.out.println("a leap year.");

        tempYear = 2000;

        System.out.print("" + tempYear + " is ");
        if (!isLeapYear(tempYear)) {
            System.out.print("NOT ");
        } // Of if
        System.out.println("a leap year.");

        tempYear = 2100;

        System.out.print("" + tempYear + " is ");
        if (!isLeapYear(tempYear)) {
            System.out.print("NOT ");
        } // Of if
        System.out.println("a leap year.");

        tempYear = 2004;

        System.out.print("" + tempYear + " is ");
        if (!isLeapYear(tempYear)) {
            System.out.print("NOT ");
        } // Of if
        System.out.println("a leap year.");

        // Test isLeapYearV2
        System.out.println("Now use the second version.");
        tempYear = 2021;

        System.out.print("" + tempYear + " is ");
        if (!isLeapYearV2(tempYear)) {
            System.out.print("NOT ");
        } // Of if
        System.out.println("a leap year.");

        tempYear = 2000;

        System.out.print("" + tempYear + " is ");
        if (!isLeapYearV2(tempYear)) {
            System.out.print("NOT ");
        } // Of if
        System.out.println("a leap year.");

        tempYear = 2100;

        System.out.print("" + tempYear + " is ");
        if (!isLeapYearV2(tempYear)) {
            System.out.print("NOT ");
        } // Of if
        System.out.println("a leap year.");

        tempYear = 2004;

        System.out.print("" + tempYear + " is ");
        if (!isLeapYearV2(tempYear)) {
            System.out.print("NOT ");
        } // Of if
        System.out.println("a leap year.");
    }// Of main

    /**
     *********************
     * Is the given year leap?
     *
     * @param paraYear The given year.
     *********************
     */
    public static boolean isLeapYear(int paraYear) {
        if ((paraYear % 4 == 0) && (paraYear % 100 != 0) || (paraYear % 400 == 0)) {
            return true;
        } else {
            return false;
        } // Of if
    }// Of isLeapYear

    /**
     *********************
     * Is the given year leap? Replace the complex condition with a number of if.
     *
     * @param paraYear The given year.
     *********************
     */
    public static boolean isLeapYearV2(int paraYear) {
        if (paraYear % 4 != 0) {
            return false;
        } else if (paraYear % 400 == 0) {
            return true;
        } else if (paraYear % 100 == 0) {
            return false;
        } else {
            return true;
        } // Of if
    }// Of isLeapYearV2

}// Of class Day01


运行结果

日撸 Java 三百行 (Day01 Java基础)_第7张图片



你可能感兴趣的:(java)