Java-01

来源:日撸 Java 三百行(01-10天,基本语法)_闵帆的博客-CSDN博客

1 输出Hello World

package 日撸Java300行;

/**
 * This is the first code.
 *@time 2022/03/31
 *@author Hui Xiao
 */
public class HelloWorld {
	public static void main(String[] args) {
		System.out.println("Hello world!");
	}// Of main
}// Of class HelloWorld

截图:

2 基本算术操作

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

2.2 熟悉println的中阶用法(字符串的加法)。

package 日撸Java300行;

/**
 * This is the second code.
 *@time 2022/3/31
 *@author Hui Xiao
 */
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;
		
		// Addiction
		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);
				
		// Mutiplication
		tempResultInt = tempFirstInt * tempSecondInt;
		tempResultDouble = tempFirstDouble * tempSecondDouble;
		
		System.out.println("" + tempFirstInt + " * " + tempSecondInt + " = " +tempResultInt);
		System.out.println("" + tempFirstDouble + " * " + tempSecondDouble + " = " +tempResultDouble);
		
		// Diviaion
		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 基本if语句

3.1 if then else

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

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

package 日撸Java300行;

/**
 *The usage of if statement.
 *@time 2022/3/31
 *@author Hui Xiao
 */

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

截图:

4 闰年的计算

4.1 if语句的嵌套。

4.2 闰年:年份能被4或者400整除但不能被100整除。

4.3 布尔类型。

package 日撸Java300行;

/**
 * This is the fourth code.
 * The complex usage of if statement.
 *@time 2022/3/31
 *@author Hui Xiao
 */
public class LeapYear {
	/**
	 **************
	 * The entrance of 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.println("NOT ");
		}// Of if
		System.out.println("a leap year.");
		
		tempYear = 2000;
		
		System.out.println("" + tempYear + " is ");
		if(!isLeapYear(tempYear)) {
			System.out.println("NOT ");
		}// Of if
		System.out.println("a leap year.");
		
		tempYear = 2100;
		
		System.out.println("" + tempYear + " is ");
		if(!isLeapYear(tempYear)) {
			System.out.println("NOT ");
		}// Of if
		System.out.println("a leap year.");
		
		tempYear = 2004;
		
		System.out.println("" + tempYear + " is ");
		if(!isLeapYear(tempYear)) {
			System.out.println("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.println("NOT ");
		}// Of if
		System.out.println("a leap year.");
		
		tempYear = 2000;
		
		System.out.println("" + tempYear + " is ");
		if(!isLeapYearV2(tempYear)) {
			System.out.println("NOT ");
		}// Of if
		System.out.println("a leap year.");
		
		tempYear = 2100;
		
		System.out.println("" + tempYear + " is ");
		if(!isLeapYearV2(tempYear)) {
			System.out.println("NOT ");
		}//Of if
		System.out.println("a leap year.");
		
		tempYear = 2004;
		
		System.out.println("" + tempYear + " is ");
		if(!isLeapYearV2(tempYear)) {
			System.out.println("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

截图:

5 基本switch语句

5.1 Switch,case,break,default的用法。

5.2 单元测试单独使用一个方法,main方法里面的代码越少越好。

package 日撸Java300行;


/**
 * This is the fifth code.
 *@time 2022/3/31
 *@author Hui Xiao
 */

public class SwitchStatement {
	/**
	 **************
	 *The entrance of program.
	 *
	 *@param args Not used now.
	 **************
	 */
	public static void main(String[] args) {
		scoreToLevelTest();
	}// Of main
	
	/**
	 ************
	 * Score to level
	 * @param paraScore From 0 to 100.
	 * @return The level from A to F.
	 ************
	 */
	public static char scoreToLevel(int paraScore) {
		// E stands for error, and F stands for fail.
		char resultLevel = 'E';
		
		// Divide by 10, the result ranges from 0 to 10
		int tempDigitalLevel = paraScore / 10;
		
		// The use of break is important
		switch (tempDigitalLevel) {
		case 10: 
		case 9:
			resultLevel = 'A';
			break;
		case 8:
			resultLevel = 'B';
			break;
		case 7:
			resultLevel = 'C';
			break;
		case 6:
			resultLevel = 'D';
			break;
		case 5:
		case 4:
		case 3:
		case 2:
		case 1:
		case 0:
			resultLevel = 'F';
			break;
		default:
			resultLevel = 'E';
		}// Of switch
		return resultLevel;
		
	}// Of scoreToLevel
	
	public static void scoreToLevelTest() {
		int tempScore = 100;
		System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));
		
		tempScore = 91;
		System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));
		
		tempScore = 82;
		System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));
		
		tempScore = 75;
		System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));
		
		tempScore = 66;
		System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));
		
		tempScore = 52;
		System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));
		
		tempScore = 8;
		System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));
		
		tempScore = 120;
		System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));
		
	}// Of scoreToLevelTest
	
}// Of class SwitchStatement

截图:

今日总结:之前是学过Java的,但是已经有点时间没碰过Java了,这次也顺便好好再复习一遍,并且跟着老师学习格式,让自己写代码的习惯练好。这次最主要的是将eclipse安装好,并且跟着做了五个练习,找到了一点点以前学Java的感觉,还得多练。

7-1 jmu-java-01入门-第一个pta上Java程序是一个关于学习Java编程的题目。在这个题目中,我们需要编写一个简单的Java程序。 这个程序要求实现一个功能,即能够输出"Hello Java!"这句话。实现这个功能很简单,只需要使用Java的输出语句System.out.println("Hello Java!");就可以了。 编写这个程序的步骤如下: 1. 打开编程软件,比如Eclipse或者IntelliJ IDEA等。创建一个Java项目。 2. 在项目的src目录下创建一个新的Java类。 3. 在这个Java类中,编写主函数public static void main(String[] args)。 4. 在主函数中,添加一行代码System.out.println("Hello Java!");,这行代码的作用是输出"Hello Java!"这句话。 5. 运行这个程序,可以看到控制台输出了"Hello Java!"这句话。 这个题目的目的是帮助我们熟悉Java的编程环境,并且学会使用基本的输出语句。虽然这个程序很简单,但是它对于初学者来说是一个很好的入门练习。 在学习编程的过程中,我们会遇到很多类似的题目,通过完成这些题目,我们可以不断熟悉各种编程语言的语法和特性,提高我们的编程能力。而且,通过编写简单的程序,我们还能够培养我们的逻辑思维能力和解决问题的能力。 总之,7-1 jmu-java-01入门-第一个pta上Java程序是一个帮助我们学习Java编程的题目,通过完成这个题目,我们可以学会使用Java的输出语句,熟悉Java的编程环境,提高我们的编程能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值