今天开始学java了,不管学多学少,学点是点,坚持90天,我相信自己!
package basic;
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello,world!");
}
}
第一天,装环境,helloworld,之前装了jdk和idea,直接偷懒
package basic;
import java.util.Arrays;
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);
//Mutiplication
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
第二天,加减乘除取余,注意整形输出字符串的格式
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;
}
System.out.println("The abslout value of " + tempNumber1 + " is " + tempNumber2);
//Try a negative value
tempNumber1 = -3;
if (tempNumber1 >= 0) {
tempNumber2 = tempNumber1;
} else {
tempNumber2 = -tempNumber1;
}
System.out.println("The abslout value of " + tempNumber1 + " is " + tempNumber2);
tempNumber1 = 6;
System.out.println("The abslout value of " + tempNumber1 + " is " + abs(tempNumber1));
tempNumber1 = -8;
System.out.println("The abslout value of " + tempNumber1 + " is " + abs(tempNumber1));
}
public static int abs(int paraValue) {
if (paraValue >= 0) {
return paraValue;
} else {
return -paraValue;
}
}
}
第三天,if语句格式
package basic;
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 ");
} // 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;
}
}//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;
}
}
}
第四天,计算闰年,与或的使用
package basic;
public class SwitchStatement {
public static void main(String[] args) {
scoreToLevelTest();
}
public static char scoreToLevel(int parascore) {
//E stands for error , and F stands for fail.
char resultLevel = 'E';
int tempDigitalLevel = parascore / 10;
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;
}
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 = 32;
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));
}
}
第五天,switch语句的使用,使用范围很小,但也要记住,不过idea有更简单的格式,数字可以直接用逗号连起来
package basic;
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;
tempN = 10;
System.out.println("1 add to " + tempN + " with step length " + tempStepLength + " is: "
+ addToNWithStpLength(tempN, tempStepLength));
tempStepLength = 2;
tempN = 10;
System.out.println("1 add to " + tempN + " with step length " + tempStepLength + " is: "
+ addToNWithStpLength(tempN, tempStepLength));
}
//加到N
public static int addToN(int paraN) {
int resultSum = 0;
for (int i = 1; i <= paraN; i++) {
resultSum += i;
}
return resultSum;
}
//加到N 有步长
public static int addToNWithStpLength(int paraN, int paraStepLength) {
int resultSum = 0;
for (int i = 1; i <= paraN; i += paraStepLength) {
resultSum += i;
}
return resultSum;
}
}
第六天,for循环,python是把一群东西中一个一个取出来操作,java不知道是不是一样的概念,好像不是,那就跟while一样是条件判断了。
package basic;
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;
tempN = 10;
System.out.println("1 add to " + tempN + " with step length " + tempStepLength + " is: "
+ addToNWithStpLength(tempN, tempStepLength));
tempStepLength = 2;
tempN = 10;
System.out.println("1 add to " + tempN + " with step length " + tempStepLength + " is: "
+ addToNWithStpLength(tempN, tempStepLength));
}
//加到N
public static int addToN(int paraN) {
int resultSum = 0;
for (int i = 1; i <= paraN; i++) {
resultSum += i;
}
return resultSum;
}
//加到N 有步长
public static int addToNWithStpLength(int paraN, int paraStepLength) {
int resultSum = 0;
for (int i = 1; i <= paraN; i += paraStepLength) {
resultSum += i;
}
return resultSum;
}
}
第七天,矩阵相加,一个矩阵所有元素的和,或者两个矩阵相加,就是两层for循环嘛。要import
java.util.Arrays
package basic;
import java.util.Arrays;
public class MatrixMutiplication {
public static void main(String[] args) {
matrixMutiplicationTest();
}
public static int[][] matrixMutiplication(int[][] paraFirstMatrix, int[][] paraSecondMatrix) {
int m = paraFirstMatrix.length;
int n = paraFirstMatrix[0].length;
int p = paraSecondMatrix[0].length;
//Step 1.Dimension check.
if (paraSecondMatrix.length != n) {
System.out.println("The two matrices cannot be multiplied.");
return null;
}
//Step 2. The loop.
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;
}
public static void matrixMutiplicationTest() {
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 = matrixMutiplication(tempFirstMatrix, tempSecondMatrix);
System.out.println("The third matrix is: \r\n" + Arrays.deepToString(tempThirdMatrix));
System.out.println("Trying to mutiply the first matrix with itself.\r\n");
tempThirdMatrix = matrixMutiplication(tempFirstMatrix, tempFirstMatrix);
System.out.println("The result matrix is: \r\n" + Arrays.deepToString(tempThirdMatrix));
}
}
第八天,矩阵相乘,有点复杂了,第一个问题是相乘的前提是前行乘后列,需要前行等于后列数,第二个问题是得到的结果是前列数乘后行数的一个矩阵。第三个问题是还要有个k遍历乘的时候的前行和后列,得到乘积的和,即为新矩阵的一个元素值。
package basic;
public class WhileStatement {
public static void main(String[] args) {
whileStatemenTest();
}
public static void whileStatemenTest() {
int tempMax = 100;
int tempValue = 0;
int tempSum = 0;
//Approach 1.
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);
//Approach 2.
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);
}
}
第九天,while语句,这个很简单,使用范围也不大,知道break和continue作用,跟c语言一样的。
package basic;
import java.util.Arrays;
import java.util.Random;
public class Task1 {
public static void main(String[] args) {
task1();
}
public static void task1() {
int n = 10;
int m = 3;
int lowerBound = 50;
int upperBound = 100;
int threhold = 60;
//Step 1. random numbers.
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);
}
}
System.out.println("The data is:\r\n" + Arrays.deepToString(data));
//Step 2. Compute the total score of each student.
int[] totalScores = new int[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (data[i][j] < threhold) {
totalScores[i] = 0;
break;
}
totalScores[i] += data[i][j];
}
}
System.out.println("The totall scores are:\r\n" + Arrays.toString(totalScores));
//Step 3. Find the best and worst student.
int tempBestIndex = -1;
int tempWorstIndex = -1;
int tempBestScore = 0;
int tempWorstScore = m * upperBound + 1;
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;
}
}
//Step 4. Output the student number and score.
if (tempBestIndex == -1) {
System.out.println("Cannot find best student.All student have failed.");
}else{
System.out.println("The best student is No."+ tempBestIndex +" with scores:"
+Arrays.toString(data[tempBestIndex]));
}
if (tempWorstIndex == -1) {
System.out.println("Cannot find worst student.All student have failed.");
}else{
System.out.println("The worst student is No."+ tempWorstIndex +" with scores:"
+Arrays.toString(data[tempWorstIndex]));
}
}
}
第十天,一个矩阵表示每个学生的成绩,计算每个学生总成绩,找出最好的最坏的,第一个问题是如何导入随机数,利用random函数创建一个random值,第二个问题要注意低于60的就不考虑了,for循环中加if判断一下,第三个问题,要注意考虑,所有人都不及格的情况,容易忘。