学习java第一天
学习使用Eclipse的第一天,先在网上学习如何安装软件和配置环境;
今天的学习情况如下
创建好类和包如图所示:
今天的编写的代码如下:
运行结果如图
未完待续......
学习java第二天
今天学习java中的简单运算,包括+ - * / %
代码如下:
package helloworld;
public class Test {
public static void main(String args[]) {
int tFirstInt,tSecondInt,tResultInt;
double tFirstDouble,tSecondDouble,tResultDouble;
tFirstInt=15;
tSecondInt=4;
tFirstDouble=1.2;
tSecondDouble=3.5;
// +
tResultInt=tFirstInt+tSecondInt;
tResultDouble=tFirstDouble+tSecondDouble;
System.out.println(""+tFirstInt+"+"+tSecondInt+"="+tResultInt);
System.out.println(""+tFirstDouble+"+"+tSecondDouble+"="+tResultDouble);
// -
tResultInt=tFirstInt-tSecondInt;
tResultDouble=tFirstDouble-tSecondDouble;
System.out.println(""+tFirstInt+"-"+tSecondInt+"="+tResultInt);
System.out.println(""+tFirstDouble+"-"+tSecondDouble+"="+tResultDouble);
// *
tResultInt=tFirstInt*tSecondInt;
tResultDouble=tFirstDouble*tSecondDouble;
System.out.println(""+tFirstInt+"*"+tSecondInt+"="+tResultInt);
System.out.println(""+tFirstDouble+"*"+tSecondDouble+"="+tResultDouble);
// /
tResultInt=tFirstInt/tSecondInt;
tResultDouble=tFirstDouble/tSecondDouble;
System.out.println(""+tFirstInt+"/"+tSecondInt+"="+tResultInt);
System.out.println(""+tFirstDouble+"/"+tSecondDouble+"="+tResultDouble);
// %
tResultInt=tFirstInt%tSecondInt;
tResultDouble=tFirstDouble%tSecondDouble;
System.out.println(""+tFirstInt+"%"+tSecondInt+"="+tResultInt);
System.out.println(""+tFirstDouble+"%"+tSecondDouble+"="+tResultDouble);
}
}
运行结果如下
15+4=19
1.2+3.5=4.7
15-4=11
1.2-3.5=-2.3
15*4=60
1.2*3.5=4.2
15/4=3
1.2/3.5=0.34285714285714286
15%4=3
心得
println要输出所打的字符(如+),需要使用“”将起括起来,没有“”的 + 在println里面起的是连接作用。
学习Java的第三天
今天学习if条件句的使用,代码尝试如下:
public class Ifdemo {
public static void main(String args[]) {
int temp1,temp2;
//try a positive value
temp1=5;
if (temp1>=0)
{
temp2=temp1;
}
else
{
temp2=-temp1;
}
System.out.println("The absolute value of " + temp1 + " is " + temp2);
// try a negative value
temp1=-5;
if (temp1>=0)
{
temp2=temp1;
}
else
{
temp2=-temp1;
}
System.out.println("The absolute value of " + temp1 + " is " + temp2);
//函数调用
temp1=6;
System.out.println("The absolute value of " + temp1 + " is " + abs(temp1));
temp2=-8;
System.out.println("The absolute value of " + temp2 + " is " + abs(temp2));
}
//函数主体
public static int abs(int paraValue) {
if (paraValue >= 0) {
return paraValue;
} else {
return -paraValue;
}
}
}
输出结果为
The absolute value of 5 is 5
The absolute value of -5 is 5
The absolute value of 6 is 6
The absolute value of -8 is 8
心得
学习了简单的 if 语句使用,同时学习了编写简单的函数(求绝对值)及其调用
学习Java第四天
目标:
学习f嵌套 布尔类型
应用:判断年份是否维闰年
原理:
判断某一年是否为闰年,给定一个年份,判断是否是闰年。条件为:
代码如下:
package helloworld;
public class IfDemo1 {
public static void main(String args[]) {
//test 1
int tempY1=2021;
System.out.print(""+tempY1+"is");
if (!isLeapYear(tempY1))
{
System.out.print("NOT");
}
System.out.print(" "+"a leap year\n");
tempY1=2000;
System.out.print(""+tempY1+"is");
if (!isLeapYear(tempY1))
{
System.out.print("NOT");
}
System.out.print(" "+"a leap year\n");
tempY1=2100;
System.out.print(""+tempY1+"is");
if (!isLeapYear(tempY1))
{
System.out.print("NOT");
}
System.out.print(" "+"a leap year\n");
tempY1=2004;
System.out.print(""+tempY1+"is");
if (!isLeapYear(tempY1))
{
System.out.print("NOT");
}
System.out.print(" "+"a leap year\n");
}
//函数
public static boolean isLeapYear(int year) {
if ((year%4==0) && (year%100!=0)) {
return true;
}
else if(year%400==0){
return true;
}
else
{
return false;
}
}
}
结果如下
2021isNOT a leap year
2000is a leap year
2100isNOT a leap year
2004is a leap year
心得
今天发现了void类型的数据无法拥有返回值
学习Java第五天
目标:
Switch, case, break, default 的用法.
实验:
判断分数的等级,给定等级如下:
10-9分为A;8分为B;7分为C;6分为D;0-5分为F;错误监测为E。
注:尽量将函数写在main的外面
代码如下:
public class SwitchDemo {
public static void main(String args[]) {
scoreToLevelTest();
}
public static char scoreToLevel(int score) {
char resultLevel='E';
int tempLevel=score/10;
switch (tempLevel) {
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;
}
public static void scoreToLevelTest() {
int Score=100;
System.out.println("Score "+ Score +" to level is " +scoreToLevel(Score));
Score=91;
System.out.println("Score "+ Score +" to level is " +scoreToLevel(Score));
Score=82;
System.out.println("Score "+ Score +" to level is " +scoreToLevel(Score));
Score=75;
System.out.println("Score "+ Score +" to level is " +scoreToLevel(Score));
Score=66;
System.out.println("Score "+ Score +" to level is " +scoreToLevel(Score));
Score=52;
System.out.println("Score "+ Score +" to level is " +scoreToLevel(Score));
Score=8;
System.out.println("Score "+ Score +" to level is " +scoreToLevel(Score));
Score=120;
System.out.println("Score "+ Score +" to level is " +scoreToLevel(Score));
}
}
运行结果如下
Score 100 to level is A
Score 91 to level is A
Score 82 to level is B
Score 75 to level is C
Score 66 to level is D
Score 52 to level is F
Score 8 to level is F
Score 120 to level is E
心得
1.switch与if的不同之处在于,该switch 只能测试是否相等,而if可以计算任何类型的布尔表达式。也就是说,switch只查找表达式的值与其case常量之一之间的匹配。
2.同一个switch中的任何两个情况常数都不能有相同的值。当然,switch语句和封闭的外部switch 可以具有相同的大小写常量。
3.switch语句通常比一组嵌套的ifs更有效。
学习Java第6天
目标:
for 循环的简单使用
实验:
分别以1和2为步长,求1到10的和
代码如下:
public class ForDemo {
public static void main(String args[])
{
forStatementTest();
}//main
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));
}
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 add to 10 is: 55
1 add to 0 is: 0
1 add to 10 with step length 1 is: 55
1 add to 10 with step length 2 is: 25
学习Java第七天
目标:
for 循环的嵌套,矩阵的加法
实验:
1.创建一个矩阵,求其所以元素的和;
2.创建两个矩阵,将其各元素分别相加。
代码如下:
import java.util.Arrays;
public class MatrixAddition {
public static void main(String args[]) {
matrixElementSumTest();
matrixAdditionTest();
}//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++) {
resultSum+=paraMatrix[i][j];
}//j
}//i
return resultSum;
}//matrixElementSum
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;
}//j
}//i
System.out.println("The matrix is: \r\n" + Arrays.deepToString(tempMatrix));
System.out.println("The matrix element sum is: " + matrixElementSum(tempMatrix) + "\r\n");
}//maritixElementSumTest
public static int[][] matrixAddition(int[][] para1,int[][] para2){
int[][] resultMartix=new int[para1.length][para1[0].length];
for(int i=0;i<para1.length;i++) {
for(int j=0;j<para1[0].length;j++) {
resultMartix[i][j]=para1[i][j]+para2[i][j];
}//j
}//i
return resultMartix;
}//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;
}//j
}//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));
}//matrixAddtionTest
}
结果如下:
The matrix is:
[[0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23]]
The matrix element sum is: 138
The matrix is:
[[0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23]]
The new matrix is:
[[0, 2, 4, 6], [20, 22, 24, 26], [40, 42, 44, 46]]
心得:
import java.util.Arrays; 这串代码的含义
Java:import java.util.Arrays(一)的使用_JunYig的博客-CSDN博客_import java.util.arrays;
Arrays.deepToString()主要用于数组中还有数组的情况,而Arrays.toString()则相反,对于Arrays.toString()而言,当数组中有数组时,不会打印出数组中的内容,只会以地址的形式打印出来。
学习Java第八天
目标:
矩阵乘法;
实验:
生成两个矩阵,判断两个矩阵能否相乘,若能,算出相乘后的矩阵
代码如下:
import java.util.Arrays;
public class MatrixMultiplication {
public static void main(String args[]) {
matrixMultiplicationTest();
}//main
public static void matrixMultiplicationTest() {
int[][] tempMatrix1=new int[2][3];
for (int i=0;i<tempMatrix1.length;i++) {
for(int j=0;j<tempMatrix1[0].length;j++) {
tempMatrix1[i][j]=i+j;
}//j
}//i
System.out.println("The first matrix is: \r\n" + Arrays.deepToString(tempMatrix1));
int[][] tempMatrix2=new int[3][2];
for (int i=0;i<tempMatrix2.length;i++) {
for(int j=0;j<tempMatrix2[0].length;j++) {
tempMatrix2[i][j]=i*10+j;
}//j
}//i
System.out.println("The second matrix is: \r\n" + Arrays.deepToString(tempMatrix2));
int[][] tempMatrix3 = multiplication(tempMatrix1, tempMatrix2);
System.out.println("The third matrix is: \r\n" + Arrays.deepToString(tempMatrix3));
System.out.println("Trying to multiply the first matrix with itself.\r\n");
tempMatrix3 = multiplication(tempMatrix1, tempMatrix1);
System.out.println("The result matrix is: \r\n" + Arrays.deepToString(tempMatrix3));
}//matrixMultiplicationTest
public static int[][] multiplication(int[][] paraMatrix1, int[][] paraMatrix2) {
int m=paraMatrix1.length;
int n=paraMatrix1[0].length;
int p=paraMatrix2[0].length;
//确认矩阵能否相乘
if(m!=p) {
System.out.println("The two matrices cannot be multiplied.");
return null;
}//if
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]=paraMatrix1[i][k]*paraMatrix2[k][j];
}//k
}//j
}//i
return resultMatrix;
}//multiplication
}
运行结果:
The first matrix is:
[[0, 1, 2], [1, 2, 3]]
The second matrix is:
[[0, 1], [10, 11], [20, 21]]
The third matrix is:
[[40, 42], [60, 63]]
Trying to multiply the first matrix with itself.
The two matrices cannot be multiplied.
The result matrix is:
null
心得:
注意循环次数,不要超过数组的索引上限
学习Java第九天
目标:
练习使用while语句
代码如下:
public class WhileStatement {
public static void main(String args[]) {
whileStatementTest();
}//main
public static void whileStatementTest() {
int tempMax=100;
int tempValue=0;
int tempSum=0;
//approach 1
while(tempSum<=tempMax) {
tempValue++;
tempSum+=tempValue;
System.out.println("tempValue = " + tempValue + ", tempSum = " + tempSum);
}//while
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;
}//if
}//while
tempSum-=tempValue;
System.out.println("The sum not exceeding " + tempMax + " is: " + tempSum);
}//whileStatementTest
}
运行结果如下:
tempValue = 1, tempSum = 1
tempValue = 2, tempSum = 3
tempValue = 3, tempSum = 6
tempValue = 4, tempSum = 10
tempValue = 5, tempSum = 15
tempValue = 6, tempSum = 21
tempValue = 7, tempSum = 28
tempValue = 8, tempSum = 36
tempValue = 9, tempSum = 45
tempValue = 10, tempSum = 55
tempValue = 11, tempSum = 66
tempValue = 12, tempSum = 78
tempValue = 13, tempSum = 91
tempValue = 14, tempSum = 105
The sum not exceeding 100 is: 91
Alternative approach.
tempValue = 1, tempSum = 1
tempValue = 2, tempSum = 3
tempValue = 3, tempSum = 6
tempValue = 4, tempSum = 10
tempValue = 5, tempSum = 15
tempValue = 6, tempSum = 21
tempValue = 7, tempSum = 28
tempValue = 8, tempSum = 36
tempValue = 9, tempSum = 45
tempValue = 10, tempSum = 55
tempValue = 11, tempSum = 66
tempValue = 12, tempSum = 78
tempValue = 13, tempSum = 91
tempValue = 14, tempSum = 105
The sum not exceeding 100 is: 91
心得:
for循环(计数器循环):大多用于已知循环次数的场景。在一个典型的for循环中,递增变量一般在循环结束后不可用。
while循环:分为do-while和while,大多用于不定循环次数的场景。在与for语句等价的while语句中,递增变量在循环结束之后仍然可用
学习Java第十天
综合任务:
学生的成绩存放于一个矩阵,其中行表示学生,列表示科目。如:第 0 行表示第 0 个学生的数学、语文、英语成绩。要求:
- 进行学生成绩的随机生成, 区间为 [50, 100].
- 找出成绩最好、最差的同学。但有挂科的同学不参加评比
代码如下:
import java.util.Arrays;
import java.util.Random;
public class Task1 {
public static void main(String args[]) {
task1();
}//main
public static void task1() {
int n=10;
int m=3;
int lowerBound=50;
int upperBound=100;
int threshould=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);
}//j
}//i
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] < threshould) {
totalScores[i] = 0;
break;
}//if
totalScores[i] += data[i][j];
}//j
}//i
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
if (tempBestScore < totalScores[i]) {
tempBestScore = totalScores[i];
tempBestIndex = i;
}//if
if (tempWorstScore > totalScores[i]) {
tempWorstScore = totalScores[i];
tempWorstIndex = i;
}//if
}//i
//输出学生及其成绩
if (tempBestIndex == -1) {
System.out.println("Cannot find best student. All students have failed.");
}//if
else {
System.out.println("The best student is No." + tempBestIndex + " with scores: "
+ Arrays.toString(data[tempBestIndex]));
}//else
if (tempWorstIndex == -1) {
System.out.println("Cannot find worst student. All students have failed.");
}//if
else {
System.out.println("The worst student is No." + tempWorstIndex + " with scores: "
+ Arrays.toString(data[tempWorstIndex]));
}//else
}//task1
}
结果如下:
The data is:
[[94, 65, 88], [51, 92, 93], [50, 83, 89], [98, 64, 86], [70, 68, 93], [65, 50, 62], [81, 66, 76], [66, 70, 87], [60, 82, 97], [52, 53, 72]]
The total scores are:
[247, 0, 0, 248, 231, 0, 223, 223, 239, 0]
The best student is No.3 with scores: [98, 64, 86]
The worst student is No.6 with scores: [81, 66, 76]
心得:
已知循环次数时,使用 for 循环更好;
加深对break 和 continue 的理解。