Java学习(day4-day7)
学习是件循序渐进的过程。
一、进行闰年的计算(闰年的数学计算过程)
作为使用Java编程实现的第一个功能,直观的感受过编程过程中细节的重要性以及命名的艺术。
什么是闰年?
普通闰年:能被4整除但不能被100整除的年份为普通闰年。(如2004年就是闰年,1999年不是闰年);世纪闰年:能被400整除的为世纪闰年。(如2000年是世纪闰年,1900年不是世纪闰年)。
代码编写:
public class LeapYear {
public static void main(String args[]) {
int tempYear=2021;
System.out.print(""+tempYear+" is");
if(!isLeapYear(tempYear)) {
System.out.print(" NOT");
}
System.out.println(" a leap year");
tempYear=2000;
System.out.print(""+tempYear+" is");
if(!isLeapYear(tempYear)) {
System.out.print(" NOT");
}
System.out.println(" a leap year");
tempYear=2100;
System.out.print(""+tempYear+" is");
if(!isLeapYear(tempYear)) {
System.out.print(" NOT");
}
System.out.println(" a leap year");
tempYear=2050;
System.out.print(""+tempYear+" is");
if(!isLeapYear(tempYear)) {
System.out.print(" NOT");
}
System.out.println(" a leap year");
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=2050;
System.out.print("" + tempYear + " is ");
if(! isLeapYearV2(tempYear)){
System.out.print("NOT ");
}//Of if
System.out.println("a leap year.");
}
public static boolean isLeapYear(int paraYear) {
if((paraYear%4==0)&&(paraYear%100!=0)||(paraYear%400==0)) {
return true;
}else {
return false;
}
}
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;
}
}
}
遇到问题:输出换行问题,print输出不能换行,println代表输出后换行。
逻辑关系(LeapYear)能被100整除的数字,一定能被4整除,为什么还要加被4整除。
运行结果:
二、基本Switch语句学习
switch语句中,每个case语句最有会有break,表示这条case执行到最后。若没有break,程序会一直执行下去直到遇到break为止。
关于switch的介绍可以参考博客:switch介绍
程序编写:
package basic;
public class SwitchStatement {
public static void main(String args[]) {
scoreToLevelTest();
}// Of main
public static char scoreToLevel(int paraScore) {
//paraScore为虚假宣传,真正运行时带入main触发的tempScore计算
char resultLevel = 'E';
//char 用于表示一个字符
//switch语句可以拥有多个 case语句,每个 case后面跟一个要比较的值和冒号
//case表示分支,当传入值等于case的值时,就执行case后语句
int tempDigitalLevel = paraScore / 10;
switch (tempDigitalLevel) {
case 10:
case 9:
resultLevel = 'A';
break;//遇到 break语句时,switch语句终止,程序跳转至后面语句运行
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
运行结果:
遇见问题:分级设置为只能对100分以内进行分级,测试时为什么输入120。
三、基本for语句学习
循环语句是程序的核心语句,也往往是决定程序复杂度的核心,也是学习和掌握的一个重点。
程序编写:
package basic;
public class ForStatement {
public static void main(String args[]) {
forStatementTest();
}// Of main
public static void forStatementTest() {
int tempN = 10;
System.out.println("1 add to " + tempN + " is: " + addToN(tempN));
//1连加至10
tempN = 0;
System.out.println("1 add to " + tempN + " is: " + addToN(tempN));
int tempStepLength = 1;
tempN = 10;
System.out.println("1 add to " + tempN + " with step length " + tempStepLength + " is: "
+ addToNWithStepLength(tempN, tempStepLength));
tempStepLength = 2;
//间隔一个数进行连加
System.out.println("1 add to " + tempN + " with step length " + tempStepLength + " is: "
+ addToNWithStepLength(tempN, tempStepLength));
}// Of forStatementTest
public static int addToN(int paraN) {
int resultSum = 0;
for (int i = 1; i <= paraN; i++) {
resultSum += i;
} // Of for i
return resultSum;
}// Of addToN
public static int addToNWithStepLength(int paraN, int paraStepLength) {
int resultSum = 0;
for (int i = 1; i <= paraN; i += paraStepLength) {
resultSum += i;
} // Of for i
return resultSum;
}// Of addToNWithStepLength
}// Of class ForStatement
运行结果:
问题:为了保证主函数的简洁性,将所有变量赋值丢到分函数中,会不会降低函数的可读性
四、矩阵元素相加
矩阵是数学运算的必不可少的工具,学会矩阵运算可以很大程度上缩减程序的复杂度。
在学习Java中的矩阵运算时,要学会Java中跟矩阵相关的一些相关基础知识,例如矩阵的定义,矩阵如何输出等
程序编写:
package basic;
import java.util.Arrays;
public class MatrixAddition {
public static void main(String args[]) {
matrixElementSumTest();
matrixAdditionTest();
}// Of main
public static int matrixElementSum(int[][] paraMatrix) {
int resultSum = 0;
for (int i = 0; i < paraMatrix.length; i++) {
for (int j = 0; j < paraMatrix[0].length; j++) {
//paraMatrix[0]代表列数
resultSum += paraMatrix[i][j];
} // Of for j
} // Of for i
return resultSum;
}// Of matrixElementSum
public static void matrixElementSumTest() {
int[][] tempMatrix = new int[3][4];//创建3*4的二维矩阵
for (int i = 0; i < tempMatrix.length; i++) {
for (int j = 0; j < tempMatrix[0].length; j++) //tempMatrix[0].length表示列长
{
tempMatrix[i][j] = i * 10 + j;//每个元素增大i*10
} // Of for j
} // Of for i
System.out.println("The matrix is: \r\n" + Arrays.deepToString(tempMatrix));//Arrays.deepToString是输出矩阵用的专门格式吗
System.out.println("The matrix element sum is: " + matrixElementSum(tempMatrix) + "\r\n");
}// Of matrixElementSumTest
public static int[][] matrixAddition(int[][] paraMatrix1, int[][] paraMatrix2) {
int[][] resultMatrix = new int[paraMatrix1.length][paraMatrix1[0].length];
for (int i = 0; i < paraMatrix1.length; i++) {
for (int j = 0; j < paraMatrix1[0].length; j++) {
resultMatrix[i][j] = paraMatrix1[i][j] + paraMatrix2[i][j];
} // Of for j
} // Of for i
return resultMatrix;
}// Of matrixAddition
public static void matrixAdditionTest() {
int[][] tempMatrix = new int[3][4];
for (int i = 0; i < tempMatrix.length; i++) {
for (int j = 0; j < tempMatrix[0].length; j++) {
tempMatrix[i][j] = i * 10 + j;
} // Of for j
} // Of for i
System.out.println("The matrix is: \r\n" + Arrays.deepToString(tempMatrix));
int[][] tempNewMatrix = matrixAddition(tempMatrix, tempMatrix);
System.out.println("The new matrix is: \r\n" + Arrays.deepToString(tempNewMatrix));
}// Of matrixAdditionTest
}// Of class MatrixAddition
运行结果:
遇到问题:矩阵的输出格式。学习到矩阵的运算时明确感受到程序框架设置的重要性,首先要建立良好的逻辑思维能力,明确不同函数实现的不同功能,尽可能使程序简介。
如果需要学习Java中的其他运算可以参考:Java的矩阵运算。
未完待续
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。