01.搭建环境
搭建java环境,成功后可以看到java版本信息,如图
在eclipse创建一个java项目:
02.基本算数操作
在println里可以使用字符串拼接输出,代码:
package xjx;
import java.util.Arrays;
public class My {
public static void main(String[] args) {
int a, b, ans;
double a1, b1, ans1;
a = 28;
b = 6;
a1 = 2.1;
b1 = 2.9;
//Addition
ans = a + b;
ans1 = a1 + b1;
System.out.println("" + a + " + " + b + " = " + ans);
System.out.println("" + a1 + " + " + b1 + " = " + ans1);
//Subtraction
ans = a - b;
ans1 = a1 - b1;
System.out.println("" + a + " - " + b + " = " + ans);
System.out.println("" + a1 + " - " + b1 + " = " + ans1);
//Multiplication
ans = a * b;
ans1 = a1 * b1;
System.out.println("" + a + " * " + b + " = " + ans);
System.out.println("" + a1 + " * " + b1 + " = " + ans1);
//Division
ans = a / b;
ans1 = a1 / b1;
System.out.println("" + a + " / " + b + " = " + ans);
System.out.println("" + a1 + " / " + b1 + " = " + ans1);
//Modulus
ans = a % b;
System.out.println("" + a + " % " + b + " = " + ans);
}
}
运行结果:
03.基本if语句
使用if else语句计算绝对值和函数的调用,每个函数头部规范注释,代码:
package xjx;
import java.util.Arrays;
public class My {
/**
*********************
* The entrance of the program.
*
* @param args Not used now.
*********************
*/
public static void main(String[] args) {
int num1, num2;
num1 = 6;
if (num1 >= 0) {
num2 = num1;
} else {
num2 = -num1;
}
System.out.println("The absolute value of " + num1 + " is " + num2);
num1 = -3;
if (num1 >= 0) {
num2 = num1;
} else {
num2 = -num1;
}
System.out.println("The absolute value of " + num1 + " is " + num2);
num1 = 6;
System.out.println("The absolute value of " + num1 + " is " + abs(num1));
num1 = -8;
System.out.println("The absolute value of " + num1 + " is " + abs(num1));
}
/**
*********************
* The absolute value of the given parameter.
*
* @param value The given value.
*********************
*/
public static int abs(int value) {
if (value >= 0) {
return value;
} else {
return -value;
}
}
}
运行结果:
04.闰年的计算
分别用两种判断方法:
第一种:
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;
}
}
运行结果:
05.基本switch语句
switch用来实现多分支判断,计算switch中的数值,在case中查找相应的数值,如果找到,就从这里开始执行程序代码,遇到break就退出switch。default 是当要判断的值与所有的case值都不匹配时,程序就从default开始执行。
用给分数分等级的例子,分数为90-100,为A,80-89为B,70-79为C,60-69为D,0-59为F,其他分数是E,代码:
package xjx;
public class My {
/**
*********************
* The entrance of the program.
*
* @param args Not used now.
*********************
*/
public static void main(String args[]) {
scoreToLevelTest();
}
/**
*********************
* Score to level.
*
* @param paraScore From 0 to 100.
* @return The level from A to F.
*********************
*/
public static char scoreToLevel(int paraScore) {
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';
}
return resultLevel;
}
/**
*********************
* Method unit test.
*********************
*/
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));
}
}
运行截图:
06.基本for语句
算法的时间复杂度一般可以通过循环语句判断,一般一秒内可以循环1e8次,for循环的表达式为:for(单次表达式;条件表达式;末尾循环体){中间循环体;}
代码:
package xjx;
public class My {
/**
*********************
* The entrance of the program.
*
* @param args Not used now.
*********************
*/
public static void main(String args[]) {
forStatementTest();
}
/**
*********************
* Method unit test.
*********************
*/
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: "
+ addToNWithStepLength(tempN, tempStepLength));
tempStepLength = 2;
System.out.println("1 add to " + tempN + " with step length " + tempStepLength + " is: "
+ addToNWithStepLength(tempN, tempStepLength));
}
/**
*********************
* Add from 1 to N.
*
* @param paraN The given upper bound.
* @return The sum.
*********************
*/
public static int addToN(int paraN) {
int resultSum = 0;
for (int i = 1; i <= paraN; i++) {
resultSum += i;
}
return resultSum;
}
/**
*********************
* Add from 1 to N with a step length.
*
* @param paraN The given upper bound.
* @param paraStepLength The given step length.
* @return The sum.
*********************
*/
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开始每隔一个数加到10的结果。
07.矩阵元素相加
矩阵是一个二维数组,通过双重for循环对矩阵赋值,i表示行,j表示列。
代码:
package xjx;
import java.util.Arrays;
public class My {
/**
*********************
* The entrance of the program.
*
* @param args Not used now.
*********************
*/
public static void main(String args[]) {
matrixElementSumTest();
}
/**
*********************
* Add two matrices. Attention: NO error check is provided at this moment.
*
* @param paraMatrix1 The first matrix matrix.
* @param paraMatrix2 The second matrix matrix. It should have the same size as
* the first one.
* @return The addition of these matrices.
*********************
*/
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;
}
/**
*********************
* Unit test for respective method.
*********************
*/
public static void matrixElementSumTest() {
int[][] tempMatrix = new int[3][4];
int resultSum = 0;
for (int i = 0; i < tempMatrix.length; i++) {
for (int j = 0; j < tempMatrix[0].length; j++) {
tempMatrix[i][j] = i * 10 + j;
resultSum += tempMatrix[i][j];
}
}
System.out.println("The matrix is: \r\n" + Arrays.deepToString(tempMatrix));
System.out.println("The matrix element sum is: " + resultSum + "\r\n");
int[][] tempNewMatrix = matrixAddition(tempMatrix, tempMatrix);
System.out.println("相加后:\r\nThe new matrix is: \r\n" + Arrays.deepToString(tempNewMatrix));
}
}
先对矩阵内的元素求了总和,然后再让矩阵自加,运行结果:
08.矩阵相乘
矩阵相乘需要用到三重循环,因为通常A矩阵为m行n列,B矩阵为n行s列的,所以相乘后的矩阵C=AB结果为m行s列的。第一重循环用来实现m行的,第二重循环用来实现s列的,第三重循环用于实现读取矩阵A的n列及矩阵B的n行。
在矩阵相乘前要先对两个矩阵是否能相乘进行错误判断,A矩阵的列数必须和B矩阵的行数相等,否则不能相乘,代码:
package xjx;
import java.util.Arrays;
public class My {
/**
*********************
* The entrance of the program.
*
* @param args Not used now.
*********************
*/
public static void main(String args[]) {
matrixMultiplicationTest();
}
/**
*********************
* Unit test for respective method.
*********************
*/
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("第一个矩阵: \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("第二个矩阵: \r\n" + Arrays.deepToString(tempSecondMatrix));
int[][] tempThirdMatrix = multiplication(tempFirstMatrix, tempSecondMatrix);
System.out.println("第一个矩阵和第二个矩阵相乘后:\r\nThe third matrix is: \r\n" + Arrays.deepToString(tempThirdMatrix));
System.out.println("让第一个矩阵自乘.\r\n");
tempThirdMatrix = multiplication(tempFirstMatrix, tempFirstMatrix);
System.out.println("相乘后的矩阵: \r\n" + Arrays.deepToString(tempThirdMatrix));
}
/**
*********************
* Matrix multiplication. The columns of the first matrix should be equal to the
* rows of the second one.
*
* @param paraFirstMatrix The first matrix.
* @param paraSecondMatrix The second matrix.
* @return The result matrix.
*********************
*/
public static int[][] multiplication(int[][] paraFirstMatrix, int[][] paraSecondMatrix) {
int m = paraFirstMatrix.length;
int n = paraFirstMatrix[0].length;
int p = paraSecondMatrix[0].length;
if (paraSecondMatrix.length != n) {
System.out.println("两个矩阵不能相乘");
return null;
}
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;
}
}
运行结果:
09.while语句
用两种while方法:
第一种,直接在while判断条件里判断sum是否小于等于max,当sum大于max时自动结束循环,代码:
while (tempSum <= tempMax) {
tempValue++;
tempSum += tempValue;
System.out.println("value = " + tempValue + ", tempSum = " + tempSum);
}
tempSum -= tempValue;
System.out.println("The sum not exceeding " + tempMax + " is: " + tempSum);
第二种,while里的判断条件为true,while会一直循环,在while循环体内加一个判断,当sum大于max时使用break跳出循环,代码:
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);
两种代码得到的结果是一样的:
10.综合任务1
题目:
学生的成绩存放于一个矩阵,其中行表示学生,列表示科目。如:第 0 行表示第 0 个学生的数学、语文、英语成绩。要求:
1、进行学生成绩的随机生成, 区间为 [50, 100].
2、找出成绩最好、最差的同学。但有挂科的同学不参加评比.
题解:
先用随机数生成一个十行三列的矩阵,表示十个学生的语数外成绩,随机数使用Random类,tempRandom.nextInt(50),表示在0-50范围内随机生成一个数字,然后生成的随机数加上50就可以表示区间[50,100]的分数了。
双重循环计算每个学生的总分,如果该学生有一科成绩低于60分,那么总分赋值为0,不参与评比。
for循环找出成绩最好的学生和最差的学生,当遇到分数为0的学生时,使用continue语句略过,继续循环。
代码:
package xjx;
import java.util.Arrays;
import java.util.Random;
public class My {
/**
*********************
* The entrance of the program.
*
* @param args
* Not used now.
*********************
*/
public static void main(String args[]) {
task1();
}
/**
*********************
* Method unit test.
*********************
*/
public static void task1() {
int n = 10;
int m = 3;
int lowerBound = 50;
int upperBound = 100;
int threshold = 60;
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));
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;
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;
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;
}
}
if (tempBestIndex == -1) {
System.out.println("Cannot find best student. All students have failed.");
} else {
tempBestIndex++;
System.out.println("The best student is No." + tempBestIndex + " with scores: "
+ Arrays.toString(data[tempBestIndex-1]));
}
if (tempWorstIndex == -1) {
System.out.println("Cannot find worst student. All students have failed.");
} else {
tempWorstIndex++;
System.out.println("The worst student is No." + tempWorstIndex + " with scores: "
+ Arrays.toString(data[tempWorstIndex-1]));
}
}
}
运行结果: