1-10天 基本语法
原文:日撸代码300行(01-10天,基本语法)_minfanphd的博客-CSDN博客
1.环境搭建
我还是用IDEA顺手点。万事开头难,然后更难 。
package day10;
/**
* Day 1 : Hello World
* @author pzf
*/
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}// Of main
}// Of class HelloWorld
1.Java驼峰变量,算是清楚
2.新学到的代码规范,在代码块后加上//of xxx的注释,增加易读性。
2.基本算数
package day10;
/**
* Day2: 基础算数操作
* @author pzf
*/
public class Calculate {
public static void main(String[] args) {
int tempInt1 = 4,tempInt2 = 5;
double tempDouble1 = 6.5, tempDouble2 = 3.7;
//Addition
System.out.println(tempInt1 + " + " + tempInt2 + " = " + (tempInt1 + tempInt2));
System.out.println(tempDouble1 + " + " + tempDouble2 + " = " + (tempDouble1 + tempDouble2));
//Subtraction
System.out.println(tempInt1 + " - " + tempInt2 + " = " + (tempInt1 - tempInt2));
System.out.println(tempDouble1 + " - " + tempDouble2 + " = " + (tempDouble1 - tempDouble2));
//Multiplication
System.out.println(tempInt1 + " * " + tempInt2 + " = " + (tempInt1 * tempInt2));
System.out.println(tempDouble1 + " * " + tempDouble2 + " = " + (tempDouble1 * tempDouble2));
//Division
System.out.println(tempInt1 + " / " + tempInt2 + " = " + (tempInt1 / tempInt2));
System.out.println(tempDouble1 + " / " + tempDouble2 + " = " + (tempDouble1 / tempDouble2));
//Modulus
System.out.println(tempInt1 + " % " + tempInt2 + " = " + (tempInt1 % tempInt2));
}// Of main
}// Of class Calculate
3.基本if 语句
package day10;
/**
* Day3: 体会if语句与函数调用
*
* @author pzf
*/
public class IfStatement {
public static void main(String[] args) {
int tempNumber1 = 4, tempNumber2 = -3;
// without function
if (tempNumber1 >= 0) {
System.out.println("abs(" + tempNumber1 + ") = " + tempNumber1);
} else {
System.out.println("abs(" + tempNumber1 + ") = " + -tempNumber1);
}// Of if
//function
System.out.println("abs(" + tempNumber1 + ") = " + getAbs(tempNumber1));
System.out.println("abs(" + tempNumber2 + ") = " + getAbs(tempNumber2));
}// Of main
/**
* Get absolute value of the given parameter
* @param num The given value
* @return The absolute value of num
*/
public static int getAbs(int num){
if(num>=0) return num;
else return -num;
}// Of getAbs
}// Of class IfStatement
函数注释最难写
4.闰年的计算
package day10;
/**
* Day: 4 闰年计算,被4整除且不被100整除,或被400整除的年份
*
* @author pzf
*/
public class LeapYear {
public static void main(String[] args) {
// 打印0-2021所有闰年
for (int i = 0; i <= 2021; i++) {
if((i % 4 == 0 && i % 100 !=0) || i % 400 == 0){
System.out.println(i);
}// Of if
}// Of for
}// Of main
}// Of class LeapYear
5.基本switch 语句
package day10;
/**
* Day5: Switch
*
* @author pzf
*/
public class SwitchStatement {
public static void main(String[] args) {
int scoreArr[] = {3,5,9,7,1,0,-1};
for(int a:scoreArr){
System.out.println("point:" + a +", rank : " + getLevel(a));
}// Of for
}// Of main
/**
* Get the rank of score,valuable:A B C D, E means Error
* @param grade input grade
* @return The rank of grade
*/
public static char getLevel(int grade) {
char res;
switch (grade) {
case (1):
case (2):
case (3):
res = 'D';
break;
case (4):
case (5):
case (6):
res = 'C';
break;
case (7):
case (8):
res = 'B';
break;
case (9):
case (10):
res = 'A';
break;
default:
res = 'E';
}// Of switch
return res;
}// Of getLevel
}
运行结果:
switch感觉用得较少,只要不忘记break就好。
6.基本for 语句
package day10;
/**
* Day 6:for
* 第一种for循环,普通形式,给数组赋值;
* 第二种,简写形式,用不上数组下标的时候可以用,打印偶数
*
* @author pzf
*/
public class ForStatement {
public static void main(String[] args) {
int[] numArr = new int[20];
//1. for(int i;i<10;i++)
for (int i = 0; i < 20; i++) {
numArr[i] = i * i;
}
//2. for(int a: arrName)
for (int tempNum : numArr){
if(tempNum % 2 == 0){
System.out.println(tempNum);
}// Of if
}// Of for
}// Of main
}// Of class ForStatement
7.矩阵相加
package day10;
import java.util.Arrays;
/**
* Day: 7 矩阵相加
*
* @author pzf
*/
public class MatrixAddition {
public static void main(String[] args) {
int[][] tempMatrix = new int[3][4];
// 初始化数组
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
tempMatrix[i][j] = i * 3 + j * 4;
}
}
// 打印结果
System.out.println(Arrays.deepToString(matrixAdd(tempMatrix, tempMatrix)));
}
/**
* 矩阵相加
* @param paraMatrix1 矩阵1
* @param paraMatrix2 矩阵2
* @return 相加后矩阵
*/
public static int[][] matrixAdd(int[][] paraMatrix1, int[][] paraMatrix2) {
int len1 = paraMatrix1.length,len2 = paraMatrix1[0].length;
int[][] resMatrix = new int[len1][len2];
for (int i = 0; i < len1; i++) {
for (int j = 0; j < len2; j++) {
resMatrix[i][j] = paraMatrix1[i][j] + paraMatrix2[i][j];
}// Of for j
}// Of for i
return resMatrix;
}// Of MatrixAdd
}
8.矩阵相乘
package day10;
import tool.getMatrix;
import java.util.Arrays;
/**
* Day8: 矩阵相乘
*
* @author pzf
*/
public class MatrixMultiplication {
public static void main(String[] args) {
// 初始化矩阵
int[][] tempMatrix = getMatrix.getMatrix(4, 4);
// 打印相乘结果
System.out.println(Arrays.deepToString(multiplication(tempMatrix, tempMatrix)));
}// Of main
/**
* 矩阵相乘
* @param paraMatrix1 矩阵1
* @param paraMatrix2 矩阵2
* @return 相乘后矩阵
*/
public static int[][] multiplication(int[][] paraMatrix1, int[][] paraMatrix2) {
// 检查合法性
int row1 = paraMatrix1.length,row2 = paraMatrix2.length;
int col1 = paraMatrix1[0].length,col2 = paraMatrix2[0].length;
if(col1 != row2){
System.out.println("Error Input");
return null;
}// Of if
//乘
int resMatrix[][] = new int[row1][col2];
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
for (int k = 0; k < col1; k++) {
resMatrix[i][j] += paraMatrix1[i][k] * paraMatrix2[k][j];
}// Of for k
}// Of for j
}// Of for i
return resMatrix;
}//Of multiplication
}//Of class MatrixMultiplication
这里用了个工具类:
package tool;
/**
* 工具类
*
* @author pzf
*/
public class getMatrix {
/**
* 返回给定行列的数组
* @param row 行
* @param column 列
* @return 数组
*/
public static int[][] getMatrix(int row,int column){
int[][] tempMatrix = new int[row][column];
// 初始化数组
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
tempMatrix[i][j] = i * row + j * column;
}
}
return tempMatrix;
}
}
9.while 语句
跳过了…没啥好写的
package day10;
/**
* Day9: while
*
* @author pzf
*/
public class WhileStatement {
public static void main(String[] args) {
int count = 0;
while (count < 10) {
System.out.println(count++);
count += count;
}// Of while
}// Of main
}// Of class WhileStatement
10. 综合1
学生的成绩存放于一个矩阵,其中行表示学生,列表示科目。如:第 0 行表示第 0 个学生的数学、语文、英语成绩。要求:
-
进行学生成绩的随机生成, 区间为 [50, 100].
-
找出成绩最好、最差的同学。但有挂科的同学不参加评比.
package day10;
import java.util.Arrays;
import java.util.Random;
/**
* Task 1
*
* @author pzf
*/
public class Task1 {
/**
* 生成随机矩阵
*
* @param row 自定义行数 (科目数)
* @param col 自定义列数 (学生数)
* @return 学生矩阵
*/
public static int[][] getRandomMatrix(int row,int col){
int[][] resMatrix = new int[row][col];
Random random = new Random();
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
resMatrix[i][j] = random.nextInt(51) + 50;
}// Of for j
}// Of for i
return resMatrix;
}// Of getRandomMatrix
/**
* 找到最好、最差的学生
*
* @param matrix 学生矩阵
*/
public static void findBestWorst(int[][] matrix){
boolean flag = false; // 标志位,如果全员挂科就为0,防止报错
int row = matrix.length,col = matrix[0].length;
int[] tempSumScore = new int[row];
// 存无挂科的成绩
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if(matrix[i][j] < 60){ // 挂科总成绩当做-1
tempSumScore[i] = -1;
break;
}else{
tempSumScore[i] += matrix[i][j];
if(j==col-1){
flag = true; // 至少有一个人不挂科
}// Of if
}// Of if
}// Of for j
}// Of for i
// 比较
if(flag){
int max = 0,min = 100*row+1;
int maxPos = 0,minPos = 0;
for (int i = 0; i < row; i++) {
if(tempSumScore[i]!=-1 && tempSumScore[i]>max){
max = tempSumScore[i];
maxPos = i;
}// Of if
if(tempSumScore[i]!=-1 && tempSumScore[i]<min){
min = tempSumScore[i];
minPos = i;
}// Of if
}// Of for
System.out.println("The best student is NO."+maxPos+" with scores:"+Arrays.toString(matrix[maxPos])+" ,total score: "+getSum(matrix[maxPos]));
System.out.println("The worst student is NO."+minPos+" with scores:"+Arrays.toString(matrix[minPos])+" ,total score: "+getSum(matrix[minPos]));
}else{
System.out.println("全员挂科,找个厂牌吧");
}// Of if
}//Of findBestWorst
/**
* 求学生总成绩
* @param paraMatrix 学生所在的行数组
* @return int 学生成绩
*/
public static int getSum(int[] paraMatrix){
int res = 0;
for(int i:paraMatrix){
res+=i;
}// Of for
return res;
}// Of getSum
/**
* main
* @param args
*/
public static void main(String[] args) {
int[][] student = getRandomMatrix(5,5);
System.out.println(Arrays.deepToString(student)); // 测试用
findBestWorst(student);
}// Of main
}// Of class Task1
1.随机数的类,random.nextInt(51) : [0-51) ,So :+50 -> [50-101)
2.运行截图: