栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

跟着老师学Java

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

跟着老师学Java

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;


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;


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;


public class IfStatement {
	
	
	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
	
	
	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;


public class LeapYear {
	
	
	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
	
	
	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
	
	
	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运行结果

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/970056.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号