跟着老师学Java Day0-Day4

0. 序言

本人有C语言基础,十一年前读书时学过Java,之后没碰过,所以Java不太会。。。

我会把我在学习过程中敲过的代码,遇到的疑惑和bug记录下来,交待自己,欢迎来踩。

Day1:环境安装

1.1安装需要的环境JDK和Eclipse。

JAVA是在JVM(JAVA虚拟机)上运行的,而通过JDK的编译可以使你编写的程序在JVM上运行(运行环境),而eclipse的出现则是让你编写程序更加的轻松方便(开发工具)。说道运行环境和开发工具的区别就是:程序通过开发工具开发出来,而通过运行环境来运行,相辅相成。

安装教程参考  Eclipse下载与安装_零碎de記憶的博客-CSDN博客_eclipse下载

1.2代码

直接通过命令行的方式运行,提示错误,删除package basic之后,运行成功。

package basic;

/**
 *This is the first code. Names and comments should follow my style strictly.
 *@author Fan Min minfanphd@163.com
 */
public class HelloWorld{
	
	public static void main(String[] args) {
		System.out.println("Hello, world!");
	}//Of main
}//Of class HelloWorld

1.3package

package(包),也就是文件夹,主要用于解决不同程序开发人员在定义类时所产生的命名冲突问题。package(文件夹)中,放置定义的类的文件,如:className1.java。程序员A定义的类放在package A_name 中,程序员B定义的类放在package B_name 中,当包A的类相当调用包B的类时,需要在程序中加一条语句,import B_name。注意:package和文件夹的名字应一致。

程序员Package(包)_百度百科解释的浅显易懂。

上面的bug解决方法如下,好吧~还是没有解决。。。

1.4eclipse入门资料

判断上面的bug应该是设置的问题,应该和环境配置、代码无关,所以边熟悉eclipse,边解决问题。最终问题解决!哈哈

6分钟学会JDK,Eclipse安装&配置_哔哩哔哩_bilibili

Day2:基本算术操作

包括:加减乘除、整除、取余、println中阶操作。

2.1代码   

package basic;

/**
 * This is the Second code. Names and comments should follow my style stricty.
 * @author Fan Min minfan@163.com
 */
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

2.2运行结果

 Day3: 基本if 语句

3.1 if then else

3.2 方法(函数)调用:增加代码的复用性

3.3 方法(函数)头部规范的注释,是后期生成文档的基础

3.1代码

package basic;

/**
 * The usage of the if statement.
 * 
 * @author Fan MIn minfanphd@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
		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));
		tempNumber2 = -8;
		System.out.println("The absolute value of " + tempNumber2 + " is " + abs(tempNumber2));
	}//Of main
	
	/**
	 * ****************
	 * The absolute value of 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 IfStatement 

3.2运行结果

 3.3 必要的空格和空行使代码美观并增强可读性

Day4: 闰年的计算

4.1 if 语句的嵌套

4.2 如何判断闰年

普通闰年:公历年份是4的倍数,且不是100的倍数的,为闰年(如2004年、2020年等就是闰年)。

世纪闰年:公历年份是整百数的,必须是400的倍数才是闰年(如1900年不是闰年,2000年是闰年)。

4.3 布尔类型

4.1代码

package basic;

/**
 * The complex usage of the if statement.
 * 
 * @author XuQiong
 */
public class LeapYear {
	
	/**
	 * *********
	 * The entrance of the program.
	 * 
	 * @param args Not used now.
	 * *********
	 */
	public static void main(String args[]) {
		//Test isLeapYear
		int tempYear = 2021;
		
		System.out.println("" + tempYear + " is ");
		if (!isLeapYear(tempYear)) {
			System.out.print("Not ");
		}// Of if
		System.out.println("a leap year.");
		
		tempYear = 2000;
		
		System.out.println("" + tempYear + " is ");
		if (!isLeapYear(tempYear)) {
			System.out.print("Not ");
		}// Of if
		System.out.println("a leap year.");
		
        tempYear = 2100;
		
		System.out.println("" + tempYear + " is ");
		if (!isLeapYear(tempYear)) {
			System.out.print("Not ");
		}// Of if
		System.out.println("a leap year.");
		
        tempYear = 2004;
		
		System.out.println("" + 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.println("" + tempYear + " is ");
		if (!isLeapYearV2(tempYear)) {
			System.out.print("Not ");
		}// Of if
		System.out.println("a leap year.");
		
		tempYear = 2000;
		
		System.out.println("" + tempYear + " is ");
		if (!isLeapYearV2(tempYear)) {
			System.out.print("Not ");
		}// Of if
		System.out.println("a leap year.");
		
        tempYear = 2100;
		
		System.out.println("" + tempYear + " is ");
		if (!isLeapYearV2(tempYear)) {
			System.out.print("Not ");
		}// Of if
		System.out.println("a leap year.");
		
        tempYear = 2004;
		
		System.out.println("" + 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 LeapYear

4.2运行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值