Day 1: HelloWorld
public class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello world");
}
}
Day 2: 基本运算操作
public class BasicOperations {
public static void main(String args[]) {
int tempFirstInt, tempSecondInt, tempResultInt;
double tempFirstDouble, tempSecondDouble, tempResultDouble;
tempFirstInt = 9;
tempSecondInt = 6;
tempFirstDouble = 1.2;
tempSecondDouble = 3.4;
//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
Day 3:if语句(绝对值)
public class IfStatement {
public static void main(String args[]) {
int tempNumber1, tempNumber2;
tempNumber1 = 5;
if (tempNumber1 >= 0) {
tempNumber2 = tempNumber1;
} else {
tempNumber2 = -tempNumber1;
}
System.out.println("The absolute value of " + tempNumber1 + " is " + tempNumber2);
tempNumber1 = -3;
if (tempNumber1 >= 0) {
tempNumber2 = tempNumber1;
} else {
tempNumber2 = -tempNumber1;
}
System.out.println("The absolute value of " + tempNumber1 + " is " + tempNumber2);
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));
}
小插曲:
在本次练习过程中,原先将类名命名为if,导致无法运行
、
调查资料后发现:不能使用java关键字来命名类!
java关键字如下(java中所有关键字必须小写):
Day 4: 闰年的计算
判断闰年的方法:
1.可以被400整除。
2.可以被4整除但不能被100整除。
第一种思路:
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;
}
}
有关布尔类型:
boolean 类型适于逻辑运算,一般用于程序流程控制。
boolean 类型数据只允许取值 true 或 false ,不可以 0 或非 0 的整数替代 true 和 false ,这点和C语言不同。
Day 5:switch语句(考试分数分级)
switch语句格式:
switch表示这是switch语句;
表达式的取值:byte,short,int,char;
case后面跟的是要和表达式进行比较的值
语句体部分可以是一条或多条语句;
break表示中断,结束的意思,可以结束switch语句;
default语句表示所有情况都不匹配的时候,就执行该处的内容,和if语句的else相似。
考试成绩分级:90-100为A,80-89为B,70-79为C,60-69为D,0-59为F,其它为E。
public class SwitchStatement {
public static void main(String args[]) {
scoreToLevelTest();
}
public static char scoreToLevel(int paraScore) {
char resultLevel = 'E';
int tempDigitalLevel = paraScore / 10;
switch (tempDigitalLevel) {
case 10:
case 9://90-100是A
resultLevel = 'A';
break;
case 8://80-89是B
resultLevel = 'B';
break;
case 7://70-79是C
resultLevel = 'C';
break;
case 6://60-69是D
resultLevel = 'D';
break;
case 5://0-59是F
case 4:
case 3:
case 2:
case 1:
case 0:
resultLevel = 'F';
break;
default://其他分数是E
resultLevel = 'E';
}
return resultLevel;
}
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));
}
}
Day 6:for语句(1-10相加)
public class ForStatement {
public static void main(String args[]) {
forStatementTest();
}
public static void forStatementTest() {
int tempN = 10;
System.out.println("1 add to " + tempN + " is: " + addToN(tempN));
tempN = 0;
System.out.println("1 add to " + tempN + " is: " + addToN(tempN));
int tempStepLength = 1;//步长为1
tempN = 10;
System.out.println("1 add to " + tempN + " with step length " + tempStepLength + " is: "
+ addToNWithStepLength(tempN, tempStepLength));
tempStepLength = 2;//步长为2
System.out.println("1 add to " + tempN + " with step length " + tempStepLength + " is: "
+ addToNWithStepLength(tempN, tempStepLength));
}
public static int addToN(int paraN) {
int resultSum = 0;
for (int i = 1; i <= paraN; i++) {
resultSum += i;
}
return resultSum;
}
public static int addToNWithStepLength(int paraN, int paraStepLength) {
int resultSum = 0;
for (int i = 1; i <= paraN; i += paraStepLength) {
resultSum += i;
}
return resultSum;
}
}
1到10相加:1+2+3+4+5+6+7+8+9+10=55;
1到0相加:0;
1到10步长为1相加:1+2+3+4+5+6+7+8+9+10=55;
1到10步长为2相加:1+3+5+7+9=25。
Day 7: 矩阵元素相加
import java.util.Arrays;//导入Arrays类
public class MatrixAddition {
public static void main(String args[]) {
matrixElementSumTest();
matrixAdditionTest();
}
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++) {
resultSum += paraMatrix[i][j];
} //i为行、j为列
}
return resultSum;
}//矩阵元素相加
public static void matrixElementSumTest() {
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;
}
}//得到3行4列矩阵
System.out.println("The matrix is: \r\n" + Arrays.deepToString(tempMatrix));// '\r' 回车,回到当前行的行首,而不会换到下一行,如果接着输出的话,本行以前的内容会被逐一覆盖;
System.out.println("The matrix element sum is: " + matrixElementSum(tempMatrix) + "\r\n");// '\n' 换行,换到当前位置的下一行,而不会回到行首;
}
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];
}
} //矩阵相加
return resultMatrix;
}
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;
}
}
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));
}
}
第一段是矩阵元素相加;
第二段是矩阵相加。
Day 8:矩阵相乘
import java.util.Arrays;
public class MatrixMultiplication {
public static void main(String args[]) {
matrixMultiplicationTest();
}
public static void matrixMultiplicationTest() {
int[][] tempFirstMatrix = new int[2][3];
for (int i = 0; i < tempFirstMatrix.length; i++) {
for (int j = 0; j < tempFirstMatrix[0].length; j++) {
tempFirstMatrix[i][j] = i + j;
}
}
System.out.println("The first matrix is: \r\n" + Arrays.deepToString(tempFirstMatrix));//第一个矩阵
int[][] tempSecondMatrix = new int[3][2];
for (int i = 0; i < tempSecondMatrix.length; i++) {
for (int j = 0; j < tempSecondMatrix[0].length; j++) {
tempSecondMatrix[i][j] = i * 10 + j;
}
}
System.out.println("The second matrix is: \r\n" + Arrays.deepToString(tempSecondMatrix));//第二个矩阵
int[][] tempThirdMatrix = multiplication(tempFirstMatrix, tempSecondMatrix);
System.out.println("The third matrix is: \r\n" + Arrays.deepToString(tempThirdMatrix));//前两个矩阵相乘得到第三个矩阵
System.out.println("Trying to multiply the first matrix with itself.\r\n");
tempThirdMatrix = multiplication(tempFirstMatrix, tempFirstMatrix);
System.out.println("The result matrix is: \r\n" + Arrays.deepToString(tempThirdMatrix));
}
public static int[][] multiplication(int[][] paraFirstMatrix, int[][] paraSecondMatrix) {
int m = paraFirstMatrix.length;
int n = paraFirstMatrix[0].length;
int p = paraSecondMatrix[0].length;
// Step 1. 保证第一个矩阵的列数与第二个矩阵的行数一致
if (paraSecondMatrix.length != n) {
System.out.println("The two matrices cannot be multiplied.");
return null;
}
// Step 2. 三重循环
int[][] resultMatrix = new int[m][p];
for (int i = 0; i < m; i++) {
for (int j = 0; j < p; j++) {
for (int k = 0; k < n; k++) {
resultMatrix[i][j] += paraFirstMatrix[i][k] * paraSecondMatrix[k][j];
}
}
}
return resultMatrix;
}
}
两矩阵相乘要保证第一的列和第二的行相等;
三重循环:
A矩阵为m行n列,B矩阵为n行s列;
矩阵C=AB结果为m行s列;
第一重循环用来实现m行;
第二重循环用来实现s列的;
第三重循环用于实现读取矩阵A的n列及矩阵B的n行。
Day9 :while语句
public class WhileStatement {
public static void main(String args[]) {
whileStatementTest();
}
public static void whileStatementTest() {
int tempMax = 100;
int tempValue = 0;
int tempSum = 0;
// 方法1;在while条件中判断
while (tempSum <= tempMax) {
tempValue++;
tempSum += tempValue;
System.out.println("tempValue = " + tempValue + ", tempSum = " + tempSum);
}
tempSum -= tempValue;
System.out.println("The sum not exceeding " + tempMax + " is: " + tempSum);
// 方法2:while判断条件为true,while循环内加入if语句判断
System.out.println("\r\nAlternative approach.");
tempValue = 0;
tempSum = 0;
while (true) {
tempValue++;
tempSum += tempValue;
System.out.println("tempValue = " + tempValue + ", tempSum = " + tempSum);
if (tempMax < tempSum) {
break;
}
}
tempSum -= tempValue;
System.out.println("The sum not exceeding " + tempMax + " is: " + tempSum);
}
}
两种方法:
1.在while条件中判断;
2.while判断条件为true,while循环内加入if语句判断;
两种方法结果相同。
Day10:综合任务1
任务:
学生的成绩存放于一个矩阵,其中行表示学生,列表示科目。如:第 0 行表示第 0 个学生的数学、语文、英语成绩。要求:
1.进行学生成绩的随机生成, 区间为 [50, 100].
2.找出成绩最好、最差的同学。但有挂科的同学不参加评比。
import java.util.Arrays;//导入Arrays类
import java.util.Random;//导入Randam类
public class Task1 {
public static void main(String args[]) {
task1();
}
public static void task1() {
// 第一步:生成n个学生和m个课程的数据
int n = 10;// 学生数
int m = 3;// 科目数
int lowerBound = 50;// 下限
int upperBound = 100; // 上限
int threshold = 60;// 及格分数
// 利用random为每位学生生成成绩
Random tempRandom = new Random();
int[][] data = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
data[i][j] = lowerBound + tempRandom.nextInt(upperBound - lowerBound);//50+(利用random类生成0~50的随机数)=(50~100的随机数)
}
}
System.out.println("The data is:\r\n" + Arrays.deepToString(data));
// 第二步:计算每个学生的总分
int[] totalScores = new int[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (data[i][j] < threshold) {
totalScores[i] = 0;//如果有一门科目不及格则总分记为0
break;
}
totalScores[i] += data[i][j];
}
}
System.out.println("The total scores are:\r\n" + Arrays.toString(totalScores));
// 第三步:找到最好和最坏的学生
int tempBestIndex = -1;
int tempWorstIndex = -1;
int tempBestScore = 0;//最高得分与0比较,从低到高找到最大值
int tempWorstScore = m * upperBound + 1;//最低得分与301比较,从高到底找到最小值
for (int i = 0; i < n; i++) {
if (totalScores[i] == 0) {
continue;//跳过不及格的学生
}
if (tempBestScore < totalScores[i]) {
tempBestScore = totalScores[i];
tempBestIndex = i;
}
if (tempWorstScore > totalScores[i]) {
tempWorstScore = totalScores[i];
tempWorstIndex = i;
}
}
// 第四步:输出最好、最坏学生和分数
if (tempBestIndex == -1) {
System.out.println("Cannot find best student. All students have failed.");
} else {
System.out.println("The best student is No." + tempBestIndex + " with scores: "
+ Arrays.toString(data[tempBestIndex-1]));//初始值减1则从No.1开始排列
}
if (tempWorstIndex == -1) {
System.out.println("Cannot find worst student. All students have failed.");
} else {
System.out.println("The worst student is No." + tempWorstIndex + " with scores: "
+ Arrays.toString(data[tempWorstIndex-1]));
}
}
}