jsd2307-java笔记
jdk下载地址:
https://repo.huaweicloud.com/java/jdk/8u191-b12/jdk-8u191-windows-x64.exe
idea旗舰版下载地址:
https://download.jetbrains.com/idea/ideaIU-2021.1.3.exe
用于检测jdk环境是否有问题
java 检测java环境
javac 检测javac环境
java -version 查看jdk版本
jdk(java developers kit)
jre(java runtime environment)
public class HelloWorld {
//程序运行的入口 main方法
public static void main(String[] args) {
//控制台打印自定义的一句话 xly 2307!!!
System.out.println("xly 2307!!!");
}
}
(1)简单的JAVA项目-JAVA SE-基础项目
-项目-工程
–包
—类
----代码:方法、变量、代码块等
(2)企业级的开发项目-JAVA EE
①byte
存储范围:-128~127
②short
存储范围:-32768~32767
③int,默认值0
存储范围:-2147483648~2147483647
④long
如果是超出int范围的值,需要加后缀L(l)
package cn.tedu;
/**
* 测试整型
*/
public class DataType1 {
public static void main(String[] args) {
//定义一个变量
byte b1 = 15;
//输出变量b1的值
System.out.println(b1);
short s1 = 11;
System.out.println(s1);
int i1 = 15;
System.out.println(i1);
long l1 = 2147483648L;
System.out.println(l1);
}
}
①float
需要加后缀F(f)
②double,默认值0.0
package cn.tedu;
/**
* 测试浮点型
*/
public class DataType2 {
public static void main(String[] args) {
float f1 = 5.88F;
double d1 = 10.03;
}
}
char
存储范围:0-65535
package cn.tedu;
/**
* 测试字符型
*/
public class DataType3 {
public static void main(String[] args) {
char c1 = '达';
System.out.println(c1);
char c2 = 'a';
System.out.println(c2);
char c3 = 73;
System.out.println(c3);
}
}
boolean
package cn.tedu;
/**
* 测试布尔型
*/
public class DataType4 {
public static void main(String[] args) {
boolean b1 = true;
System.out.println(b1);
b1 = false;
System.out.println(false);
}
}
注:
(1)数据类型大小关系(从大到小):double > float > long > int > short > byte
(2)数据类型间的转换:
①隐式转换(自动转换):小给大
②显式转换(强制转换):大给小
格式:小类型的变量 = (小类型)大类型的值
package cn.tedu;
/**
* 基本数据类型大小和转换
* double > float > long > int > short > byte
*
* ①隐式转换(自动转换):小给大
* ②显式转换(强制转换):大给小
* 格式:小类型的变量 = (小类型)大类型的值
*/
public class DataType5 {
public static void main(String[] args) {
byte b1 = 10;
int i1 = 0;
i1 = b1;//小给大,自动转换
System.out.println(i1);
int i2 = 30;
byte b2 = 0;
b2= (byte)i2;//大给小,强制转换
System.out.println(b2);
}
}
+、-、*、/、%、++、–
%(取模):求余数
(1)大取小:求余数
(2)小取大:直接得到最小的那个数
注:
(1)加减乘除在浮点类型做运算时,可能会出现精度丢失(数据不准确)的问题
(2)+有两重含义:第一种是正常的运算,第二种是字符串拼接
package cn.tedu;
/**
* 算术运算符
*/
public class Operator1 {
//psvm
public static void main(String[] args) {
//正常的算术运算
int a = 10;
int b = 20;
//sout
System.out.println(a+b);
int c = 0;
c = a + b;
System.out.println(c);
//浮点类型精度丢失问题
double d1,d2,d3,d4;
d1 = 15.0;
d2 = 8.3;
d3 = d1 -d2;
System.out.println(d3);
//取模
int d,e,f,g;
d = 10;
e = 3;
f = d%e;
System.out.println("f = " + f);//1
int num1,num2,num3;
num1 = 2;
num2 = 5;
num3 = num1%num2;
System.out.println("num3 = " + num3);//2
//加号的第二重含义
double balance1,newBalance1;
balance1 = 0;
System.out.println("我的余额是:" + balance1 + "元");
newBalance1 = balance1 + 300 + 50;
System.out.println("我的余额是:" + newBalance1 + "元");
//整型做运算时,自动类型提升1
byte b1 = 10;
byte b2 = 20;
byte b3 = 0;
b3 = (byte)(b1 + b2);//30
System.out.println("b3 = " + b3);
//整型做运算时,自动类型提升1
short num4 = 13;
long num5 = 22;
int num6 = 0;
num6 = (int)(num4 + num5);
System.out.println("num6 = " + num6);
//整型和浮点类型混合做运算时,自动类型提升
int num7 = 21;
long num8 = 97;
float num9 = 3.88F;
double num10 = 5.18;
double num11;
num11 = num7+num8+num9+num10;
System.out.println("num11 = " + num11);
//不会四舍五入
double num12,num13;
num12 = 7.38;
num13 = 5.4;
int num14 = (int)(num12+num13);
System.out.println("num14 = " + num14);
}
}
public class Operator6 {
public static void main(String[] args) {
int num1 = 10;
int temp1 = ++num1;//先运算,再赋值,先num1自加1,再把num1自加1的值给到++num1
System.out.println(num1);//11
System.out.println(temp1);//11
int num2 = 5;
int temp2 =num2++;//先赋值,再运算,先num2的值给到num2++,再让num2自加1
System.out.println(num2);//6
System.out.println(temp2);//5
int num3 = 8;
int temp3 = --num3;//7
System.out.println(num3);//7
System.out.println(temp3);//7
int num4 = 2;
int temp4 = num4--;//2
System.out.println(num4);//1
System.out.println(temp4);//2
int num5,num6,num7,num8,num9,num10;
num5 = 7;
num6 = 10;
num7 = 3;
num8 = 6;
num9 = 15;
num10 = 4;
int temp5 = num5+num6++-num7+++num8-num9--+num10--;//9
System.out.println(temp5);//9
}
}
=、+=、-=、*=、/=、%=
package cn.tedu;
/**
* 赋值运算符
*/
public class Operator2 {
public static void main(String[] args) {
//+= -= *= /= %=
int num1,num2,num3,num4,num5,num6;
double num7,num8,num9,num10;
num1 = 13;
num2 = 15;
num3 = 7;
num4 = 9;
num5 = 21;
num6 = 53;
num7 = 3.0;
num8 = 2.6;
num9 = 10.38;
num10 = 11.31;
num1+=10;//<=> num1 = num1+10
System.out.println("num1 = " + num1);//23
num2-=5;//<=> num2 = num2 - 5;
System.out.println("num2 = " + num2);//10
num3*=7;//<=> num3 = num3 * 7;
System.out.println("num3 = " + num3);//49
num8/=2.0;//<=> num8 = num8 / 2.0;
System.out.println("num8 = " + num8);//1.3
num9%=num10;//<=> num9 = num9 % num10;
System.out.println("num9 = " + num9);//10.38
}
}
>、>=、<、<=、==、!=
package cn.tedu;
/**
* 比较运算符
*/
public class Operator3 {
public static void main(String[] args) {
boolean num1,num2;
int num3,num4;
short num5,num6;
num3 = 5;
num4 = 11;
num5 = 7;
num6 = 5;
num1 = num3 > num4;
System.out.println("num1 = " + num1);//false
num2 = num3 >= num6;
System.out.println("num2 = " + num2);//true
num1 = num4 == num5;
System.out.println("num1 = " + num1);//false
num2 = num3 != num5;
System.out.println("num2 = " + num2);//true
}
}
&&(与) 、||(或) 、!(非)
(1)&&:一假则假,全真才为真
(2)||:一真则真,全假才为假
(3)!:取反
package cn.tedu;
/**
* 逻辑运算符
*/
public class Operator4 {
public static void main(String[] args) {
//&&
double mathScore1,mathScore2,engScore1,engScore2;
mathScore1 = 58.0;
mathScore2 = 57.0;
engScore1 = 93.0;
engScore2 = 95.0;
boolean num1 = mathScore1>mathScore2 && engScore1 < engScore2;
System.out.println("num1 = " + num1);//true
//||
boolean num2 = mathScore1 > engScore1 || mathScore2 < engScore2;
System.out.println("num2 = " + num2);//true
//!
num1 = !num1;
System.out.println("num1 = " + num1);//false
}
}
a > b? a表达式 : b表达式
a>b为真,走a表达式,否则走b表达式
package cn.tedu;
/**
* 三元运算符
*/
public class Operator5 {
public static void main(String[] args) {
double mathScore1,mathScore2,sumScore1,sumScore2;
mathScore1 = 88.0;
mathScore2 = 73.0;
sumScore1 = 568.0;
sumScore2 = 429.0;
double num1 = mathScore1 > mathScore2 ? sumScore1 : sumScore2;
System.out.println("num1 = " + num1);
//找三个数的最大值
int num2,num3,num4,max;
num2 = 10;
num3 = 7;
num4 = 18;
max = num2 > num3 ? (num2>num4?num2 : num4) : (num3>num4? num3 : num4);
System.out.println("max = " + max);
}
}
package cn.tedu;
/**
* 二选一:if else
*/
public class Branch1 {
public static void main(String[] args) {
double sumScore = 650;
if(sumScore > 660) {
System.out.println("奖励一部iphone 14");
} else {
System.out.println("奖励一部vivo s7");
}
}
}
package cn.tedu;
/**
* 多选一:if elseif else
*/
public class Branch2 {
public static void main(String[] args) {
double score = 88;
if(score >= 90) {
System.out.println("优秀");
} else if(score >= 80) {
System.out.println("良");
} else if(score >= 70) {
System.out.println("中等");
} else if(score >= 60) {
System.out.println("及格");
} else {
System.out.println("不及格");
}
}
}
switch支持的类型:byte、short、int、char、String、Enum
①没有break,从匹配处一直穿透,穿透到结束
②有break,从匹配处开始,到有break的地方结束
package cn.tedu;
/**
* switch case
*/
public class Branch3 {
public static void main(String[] args) {
int num1 = 5;
switch (num1) {
case 1:
System.out.println("math");
break;
case 3:
System.out.println("english");
break;
case 5:
System.out.println("chinese");
case 7:
System.out.println("physics");
break;
case 9:
System.out.println("history");
break;
case 11:
System.out.println("politics");
break;
default:
System.out.println("economics");
}
}
}
作业:
一、交换两个变量的值
编写步骤:
1. 定义类Homework6
2. 定义 main方法
3. 定义两个整数变量a,b并赋值
4. 控制台输出变量a,b互换前的值
5. 定义一个第三方变量temp
6. 利用第三方变量temp使a,b的值互换
7. 控制台输出变量a,b互换后的值
二、
编写步骤:
1. 定义类 Homework7
2. 定义 main方法
3. 定义2个int类型变量x、y,x赋值为100,y赋值为200
4. 定义新变量add,保存变量x,y的和并打印到控制台
5. 定义新变量sub,保存变量x,y的差并打印到控制台
6. 定义新变量mul,保存变量x,y的积并打印到控制台
7. 定义新变量div,保存变量x,y的商并打印到控制台
三、
编写步骤:
1. 定义类 Homework8
2. 定义 main方法
3. 定义2个double类型变量x、y,x赋值为100.8,y赋值为20.6
4. 定义新变量add,保存变量x,y的和并打印到控制台
5. 定义新变量sub,保存变量x,y的差并打印到控制台
6. 定义新变量mul,保存变量x,y的积并打印到控制台
7. 定义新变量div,保存变量x,y的商并打印到控制台
四、 强制类型转换练习
(1)先声明两个byte类型的变量b1,b2,并分别赋值为10和20,求b1和b2变量的和,并将结果保存在byte类型的变量b3中,最后输出b3变量的值
(2)先声明两个short类型的变量s1,s2,并分别赋值为1000和2000,求s1和s2变量的和,并将结果保存在short类型的变量s3中,最后输出s3变量的值
(3)先声明1个char类型的变量c1赋值为'a',再声明一个int类型的变量num赋值为5,求c1和num变量的和,并将结果将结果保存在char类型的变量letter中,最后输出letter变量的值。
(4)先声明两个int类型的变量i1,i2,并分别赋值5和2,求i1和i2的商,并将结果保存在double类型的变量result中,最后输出result变量的值。如何得到结果2.5呢?
五、
1. 定义两个int类型变量a1和a2,分别赋值10,11,判断变量是否为偶数,拼接输出结果
2. 定义两个int类型变量a3和a4,分别赋值12,13,判断变量是否为奇数,拼接输出结果
作业-答案:
package cn.tedu;
/**
* 一、交换两个变量的值
* 编写步骤:
* 1. 定义类Homework6
* 2. 定义 main方法
* 3. 定义两个整数变量a,b并赋值
* 4. 控制台输出变量a,b互换前的值
* 5. 定义一个第三方变量temp
* 6. 利用第三方变量temp使a,b的值互换
* 7. 控制台输出变量a,b互换后的值
*/
public class Homework6 {
public static void main(String[] args) {
int a,b,temp;
a = 10;
b = 30;
System.out.println("交换前:a = " + a + ",b = " + b);
temp = a;
a = b;
b = temp;
System.out.println("交换后:a = " + a + ",b = " + b);
int c,d;
c = 10;
d = 30;
System.out.println("交换前:c = " + c + ",d = " + d);
c = c + d;//40
d = c - d;//10
c = c - d;//30
System.out.println("交换后:c = " + c + ",d = " + d);
}
}
public class Homework7 {
public static void main(String[] args) {
//ctrl + c 复制
//ctrl + v 粘贴
//ctrl + shift + F10 快速运行当前程序
int x,y,add,sub,mul,div;
x = 100;
y = 200;
add = x + y;
sub = x - y;
mul = x * y;
div = x / y;
System.out.println("add=" + add);
System.out.println("sub=" + sub);
System.out.println("mul=" + mul);
System.out.println("div=" + div);
}
}
public class Homework8 {
public static void main(String[] args) {
double x,y,add,sub,mul,div;
x = 100.8;
y = 20.6;
add = x + y;
sub = x - y;
mul = x * y;
div = x / y;
//soutv
System.out.println("add = " + add);
System.out.println("sub = " + sub);
System.out.println("mul = " + mul);
System.out.println("div = " + div);
}
}
public class Homework9 {
public static void main(String[] args) {
byte b1,b2,b3;
b1 = 10;
b2 = 20;
b3 = (byte)(b1+b2);
System.out.println("b3 = " + b3);
short s1,s2,s3;
s1 = 1000;
s2 = 2000;
s3 = (byte)(s1+s2);
System.out.println("s3 = " + s3);
char c1,letter;
c1 = 'a';
int num = 5;
letter = (char)(c1 + num);//f
System.out.println("letter = " + letter);
int i1,i2;
i1 = 5;
i2 = 2;
double result = (double)i1/i2;
System.out.println("result = " + result);
}
}
public class Homework10 {
public static void main(String[] args) {
int a1,a2,a3,a4;
a1 = 10;
a2 = 11;
a3 = 12;
a4 = 13;
if(a1%2==0) {
System.out.println("a1=" + a1 + ",它是偶数");
} else {
System.out.println("a1=" + a1 + ",它是奇数");
}
if(a2%2==0) {
System.out.println("a2=" + a2 + ",它是偶数");
} else {
System.out.println("a2=" + a2 + ",它是奇数");
}
if(a3%2==0) {
System.out.println("a3=" + a3 + ",它是偶数");
} else {
System.out.println("a3=" + a3 + ",它是奇数");
}
if(a4%2==0) {
System.out.println("a4=" + a4 + ",它是偶数");
} else {
System.out.println("a4=" + a4 + ",它是奇数");
}
}
}
(1)for
for(①初始化;②判断条件;④表达式) {
③循环体
}
(2)while
①初始化
while(②判断条件) {
③循环体
④表达式
}
(3)do while
①初始化
do {
②循环体
③表达式
} while(④判断条件);
package cn.tedu.day02;
/**
* 循环:从1打印到10
*/
public class Loop1 {
public static void main(String[] args) {
//ctrl + alt + L 代码格式规范化
//ctrl + A 全选
//ctrl + Z 撤销,返回
//for
for (int i = 1; i < 11; i++) {
System.out.println(i);
}
//while
int k = 1;
while (k < 11) {
System.out.println(k);
k++;
}
//do while
int x = 1;
do {
System.out.println(x);
x++;
} while (x < 11);
}
}
package cn.tedu.day02;
/**
* 循环:找出1-100里面所有的奇数和偶数,并输出
*/
public class Loop2 {
public static void main(String[] args) {
//for
for (int i = 1; i < 101; i++) {
if (i % 2 == 0) {
System.out.println(i + ",它是偶数");
} else {
System.out.println(i + ",它是奇数");
}
}
//while
System.out.println("---while---");
int i = 1;
while (i < 101) {
if (i % 2 == 0) {
System.out.println(i + ",它是偶数");
} else {
System.out.println(i + ",它是奇数");
}
i++;
}
//do while
System.out.println("---do while---");
i = 1;
do {
if (i % 2 == 0) {
System.out.println(i + ",它是偶数");
} else {
System.out.println(i + ",它是奇数");
}
i++;
} while (i < 101);
}
}
package cn.tedu.day02;
/**
* 循环:
* (1)找出1-100里面是3的倍数的数
* (2)找出1-100里面以3结尾的数
*/
public class Loop3 {
public static void main(String[] args) {
//for
for(int i = 1;i < 101;i++) {
if(i%3==0) {
System.out.println(i);
}
}
System.out.println("---while---");
//while
int x = 1;
while(x < 101) {
if(x%3==0) {
System.out.println(x);
}
x++;
}
System.out.println("---do while---");
//do while
x = 1;
do {
if(x%3==0) {
System.out.println(x);
}
x++;
} while(x < 101);
System.out.println();
System.out.println();
System.out.println();
System.out.println("找出1-100以3结尾的数");
//for
for (int i = 1; i < 101; i++) {
if(i%10==3) {
System.out.println(i);
}
}
//while
System.out.println("---while---");
x = 1;
while(x < 101) {
if(x%10==3) {
System.out.println(x);
}
x++;
}
//do while
System.out.println("---do while---");
x = 1;
do {
if(x%10==3) {
System.out.println(x);
}
x++;
} while(x < 101);
}
}
package cn.tedu.day02;
/**
* 死循环
*/
public class Loop4 {
public static void main(String[] args) {
//ctrl + / 单行注释,再按一次就是取消单行注释
//ctrl + shift + / 多行注释,再按一次就是取消多行注释
//for
/* for(;;) {
System.out.println(10);
}*/
/*for (int i = 1; i < 10; ) {
System.out.println(11);
}*/
//while
/*while(true) {
System.out.println(12);
}*/
/*int x = 1;
while (x > 0) {
System.out.println(13);
}*/
//do while
/*do {
System.out.println(14);
} while(true);*/
int y = 10;
do {
System.out.println(15);
} while(y > 5);
}
}
package cn.tedu.day02;
/**
* 嵌套循环-打印三角形
*/
public class Loop5 {
public static void main(String[] args) {
// System.out.println(); 换行输出
// System.out.print(); 不换行
/**
* *
* **
* ***
* ****
* *****
*/
for (int i = 1; i < 6; i++) {//控制行数
for (int j = 1; j < i + 1; j++) {//控制每一行星号的个数
System.out.print("*");
}
//换行操作
System.out.println();
}
System.out.println();
System.out.println();
/**
* *
* **
* ***
* ****
* *****
*/
int x = 1;
while (x < 6) {//控制行数
int z = 5;
while (z > x) {//控制每一行空格的数量
System.out.print(" ");
z--;
}
int y = 1;
while (y < x + 1) {//控制每一行星号的个数
System.out.print("*");
y++;
}
//换行操作
System.out.println();
x++;
}
System.out.println();
System.out.println();
/**
* *****
* ****
* ***
* **
* *
*/
int z = 1;
do {//外循环控制行数
int k = 6;
do {//内循环控制每一行星号的个数
System.out.print("*");
k--;
} while (k > z);
//换行操作
System.out.println();
z++;
} while (z < 6);
System.out.println();
System.out.println();
/**
* *****
* ****
* ***
* **
* *
*/
int k = 1;
do {//外循环控制行数
int g = 1;
do {//内循环打印空格
if(g!=1) {
System.out.print(" ");
}
g++;
} while(g < k + 1);
int f = 6;
do {//内循环打印星号
System.out.print("*");
f--;
} while(f>k);
//换行操作
System.out.println();
k++;
} while(k < 6);
}
}
day02作业:
一、案例:为抵抗洪水,战士连续作战89小时,编程计算共多少天零多少小时?
定义一个int类型变量hours,赋值为89
定义一个int类型变量day,用来保存89小时中天数的结果
定义一个int类型变量hour,用来保存89小时中不够一天的剩余小时数的结果
输出结果
```java
public class Homework1 {
public static void main(String[] args) {
int hours,day,hour;
hours = 89;
day = hours/24;
hour = hours%24;
// hour = hours-day*24;
System.out.println("天数:" + day + "小时:" + hour);
day = 0;
hour = 0;
while (true) {
if(hours >= 24) {
hours-=24;
day++;
} else {
hour = hours;
break;
}
}
System.out.println("day = " + day + ",hour = " + hour);
}
}
二、今天是周2,100天以后是周几?
定义一个int类型变量week,赋值为2
修改week的值,在原值基础上加上100
修改week的值,在原值基础上模以7
输出结果,在输出结果的时候考虑特殊值,例如周日
public class Homework2 {
public static void main(String[] args) {
int week = 2;
week+=5;
week%=7;
if(week==0) {
week=7;
}
System.out.println(week);
}
}
三、求三个整数x,y,z中的最大值
定义三个int类型变量,x,y,z,随意赋值整数值
定义一个int类型变量max,先存储x与y中的最大值(使用三元运算符)
再次对max赋值,让它等于上面max与z中的最大值(使用三元运算符)
输出结果
public class Homework3 {
public static void main(String[] args) {
int x,y,z,max;
x = 11;
y = 19;
z = 15;
max = x > y ? (x > z ? x : z) : (y > z ? y : z);
System.out.println("max = " + max);
}
}
四、计算折扣后金额
从键盘输入订单总价格totalPrice(总价格必须>=0),根据优惠政策计算打折后的总价格。
编写步骤:
判断当totalPrice >=500 ,discount赋值为0.8
判断当totalPrice >=400 且<500时,discount赋值为0.85
判断当totalPrice >=300 且<400时,discount赋值为0.9
判断当totalPrice >=200 且<300时,discount赋值为0.95
判断当totalPrice >=0 且<200时,不打折,即discount赋值为1
判断当totalPrice<0时,显示输入有误
输出结果
public class Homework4 {
public static void main(String[] args) {
int totalPrice = 800;
//从键盘输入的订单总价格
System.out.println("请输入订单总价格:");
int totalPrice1 = new Scanner(System.in).nextInt();
System.out.println("totalPrice1 = " + totalPrice1);
//从键盘输入的订单总价格
System.out.println("请输入订单总价格:");
Scanner sc = new Scanner(System.in);
// int totalPrice2 = sc.nextInt();//接收int类型的值、
double totalPrice2 = sc.nextDouble(); //接收double类型的值
System.out.println("totalPrice2 = " + totalPrice2);
if(totalPrice2 >= 500) {
totalPrice2*=0.8;
} else if(totalPrice2 >= 400) {
totalPrice2*=0.85;
} else if(totalPrice2>=300) {
totalPrice2*=0.9;
} else if(totalPrice2>=200) {
totalPrice2*=0.95;
} else if(totalPrice2>=0){
totalPrice2*=1;
} else {
System.out.println("你输入的订单总价格有误!");
}
System.out.println("您的订单总价格为:" + totalPrice2);
}
}
五、 计算今天是星期几
定义变量week赋值为上一年12月31日的星期值(可以通过查询日历获取),定义变量year、month、day,分别赋值今天日期年、月、日值。计算今天是星期几。
public class Homework5 {
public static void main(String[] args) {
int week,year,month,day;
week = 6;
//判断今年是平年还是闰年
year = 2023;
month = 7;
day = 12;
System.out.println((year%400==0) || (year%4==0 && year%100!=0));
month--;
while(true) {
if(month<1) {
break;
}
switch (month) {
case 4:
case 6:
case 9:
case 11:
day+=30;
break;
case 2:
if((year%400==0) || (year%4==0 && year%100!=0)) { //如果是闰年
day+=29;
break;
} else {//否则是平年
day+=28;
break;
}
default:
day+=31;
break;
}
month--;
}
System.out.println(day);
week+=day;
week%=7;
System.out.println("今天是周" + week);
}
}
六、5个一行输出1-100之间的偶数
输出1-100偶数,每5个一行,一行中的每个数字之间使用逗号分隔
public class Homework7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year,month,day;
System.out.println("请输入年份:");
year = sc.nextInt();
System.out.println("请输入月份:");
month = sc.nextInt();
System.out.println("请输入今天是多少号:");
day = sc.nextInt();
System.out.println("year: " + year + ",month: " + month + ",day: " + day);
month--;
for(;;) {
if(month<1) {
break;
}
if(month==4 || month==6 || month==9 || month==11) {
day+=30;
} else if(month==2) {
if(year%400==0 || (year%4==0 && year%100!=0)) {//如果是闰年
day+=29;
} else {//否则是平年
day+=28;
}
} else {
day+=31;
}
month--;
}
System.out.println("今天是这一年当中的第:" + day + "天");
}
}
七、计算这一天是这一年的第几天
案例需求:从键盘分别输入年、月、日,使用循环for+if实现,判断这一天是当年的第几天
public class Homework7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year,month,day;
System.out.println("请输入年份:");
year = sc.nextInt();
System.out.println("请输入月份:");
month = sc.nextInt();
System.out.println("请输入今天是多少号:");
day = sc.nextInt();
System.out.println("year: " + year + ",month: " + month + ",day: " + day);
month--;
for(;;) {
if(month<1) {
break;
}
if(month==4 || month==6 || month==9 || month==11) {
day+=30;
} else if(month==2) {
if(year%400==0 || (year%4==0 && year%100!=0)) {//如果是闰年
day+=29;
} else {//否则是平年
day+=28;
}
} else {
day+=31;
}
month--;
}
System.out.println("今天是这一年当中的第:" + day + "天");
}
}
package cn.tedu.day02;
/**
* 嵌套循环-乘法口诀表
*/
public class Loop6 {
public static void main(String[] args) {
//do while
int k = 1;
do {
int x = 1;
do {
System.out.print(x + "*" + k + "=" + x*k + " ");
x++;
} while(x < k + 1);
//换行操作
System.out.println();
k++;
} while(k < 10);
//while
int y=1;
while(y < 10) {
int z=1;
while(z < y+1) {
System.out.print(z + "*" + y + "=" + z*y + " ");
z++;
}
System.out.println();
y++;
}
}
}
是一个容器,可以存放同一种类型的多个数据(可重复)
①数组的类型[] 数组名 = new 数组的类型[] {值1,值2,值3,值4…};
②数组的类型[] 数组名 = {值1,值2,值3…};
数组的类型[] 数组名 = new 数组的类型[数组的长度];
数组的长度=数组元素的个数
数组的下标:[0,数组的长度-1]
获取的数组的长度:数组名.length
package cn.tedu.day03;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import org.w3c.dom.ls.LSOutput;
/**
* 数组创建方式
*/
public class Array1 {
public static void main(String[] args) {
//静态
//①数组的类型[] 数组名 = new 数组的类型[]{值1,值2,值3,值4....};
int[] arr1 = new int[]{10,20,30,40,50};
int num1 = arr1[0];
int num2 = arr1[2];
int num3 = arr1[4];
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
System.out.println("num3 = " + num3);
System.out.println(arr1.length);//5 [0,4]
System.out.print("数组arr1遍历后的结果:");
int x = 0;
do {
int num = arr1[x];
System.out.print(num + " ");
x++;
} while(x < 5);
System.out.println();
//②数组的类型[] 数组名 = {值1,值2,值3...};
int[] arr2 = {60,70,80,90,100};
int num4 = arr2[1];
int num5 = arr2[3];
System.out.println("num4 = " + num4);
System.out.println("num5 = " + num5);
System.out.print("数组arr2 while遍历的结果:");
int k = 0;
while(k < arr2.length) {
System.out.print(arr2[k] + " ");
k++;
}
System.out.println();
//动态
//数组的类型[] 数组名 = new 数组的类型[数组的长度];
byte[] arr3 = new byte[10];
System.out.print("数组arr3遍历后的结果:");
for(int i = 0;i < arr3.length;i++) {
int value = arr3[i];
System.out.print(value + " ");
}
//换行操作
System.out.println();
boolean[] arr4 = new boolean[3];
char[] arr5 = new char[2];
System.out.print("数组arr4遍历后的结果:");
int z = 0;
do {
System.out.print(arr4[z] + " ");
z++;
} while(z < arr4.length);
System.out.println();
System.out.print("数组arr5的遍历结果:");
int m = 0;
while(m < arr5.length) {
System.out.print(arr5[m] + " ");
m++;
}
System.out.println();
}
}
获取数组元素的方式主要是通过下标,而遍历数组主要靠循环来解决
动态数组是有值的,默认值是对于类型的默认值
package cn.tedu.day03;
import java.util.Random;
/**
* 动态赋值
*/
public class Array2 {
public static void main(String[] args) {
int[] arr1 = new int[6];
//手动赋值
arr1[0] = 2;
arr1[1] = 4;
arr1[2] = 6;
arr1[3] = 8;
arr1[4] = 10;
arr1[5] = 12;
System.out.print("arr1 手动赋值后的结果:");
for (int i = 0; i < arr1.length; i++) {
System.out.print(arr1[i] + " ");
}
//换行
System.out.println();
int[] arr2 = new int[5];
//赋随机值:Math.random
for (int i = 0; i < arr2.length; i++) {
double num1 = Math.random()*100;//(0.0,1.0) -> (0.0,100.0)
arr2[i] = (int)num1;
}
System.out.print("arr2 赋随机值后的结果:");
for (int i = 0; i < arr2.length; i++) {
System.out.print(arr2[i] + " ");
}
System.out.println();
int[] arr3 = new int [5];
//赋随机值 Random.nextInt()
Random random = new Random();
//int num2 = random.nextInt();//int范围
for (int i = 0; i < arr3.length; i++) {
int num2 = random.nextInt(100);//[0,bound) -> [0,100)
arr3[i] = num2;
}
System.out.print("arr3 赋随机值后的结果:");
for (int i = 0; i < arr3.length; i++) {
System.out.print(arr3[i] + " ");
}
}
}
/**
* 数组-验证码生成
*/
public class Array3 {
public static void main(String[] args) {
//1、定义一个数组来保存验证码
char[] vc = new char[4];
//2、给数组赋随机验证码值
//1-9 49-57
//A-Z 65-90
//a-z 97-122
int count = 0;
Random rd = new Random();
do {
if(count==vc.length) {
break;
}
int num = rd.nextInt(123);//[0,123]
// int num = 97;
if(num>48 && num<58) {
vc[count] = (char)num;
count++;
} else if(num>64 && num<91) {
vc[count] = (char)num;
count++;
} else if(num>96 && num<123) {
vc[count] = (char)num;
count++;
}
} while(true);
System.out.print("验证码:");
for (int i = 0; i < vc.length; i++) {
System.out.print(vc[i]);
}
//3、用户从键盘输入验证码进行比对
System.out.println();
System.out.println("请输入");
Scanner sc = new Scanner(System.in);
String str = sc.next();//只能得到一个字符串
int x,count1;
x = 0;//下标
count1 = 0;//计数器,记录的是正确的次数
while(x < vc.length) {
if(vc[x]==str.charAt(x)) {
count1++;
}
x++;
}
//4、如果比对成功,进入该系统,如果错误,提示失败
if(count1==4) {
System.out.println("验证码正确,欢迎进入本系统!");
} else {
System.out.println("验证码输入有误!");
}
}
}
package cn.tedu.day03;
import java.util.Arrays;
import java.util.Random;
/**
* 数组的复制
*/
public class Array4 {
public static void main(String[] args) {
int[] arr1 = new int[8];
Random rd = new Random();
//赋值
for (int i = 0; i < arr1.length; i++) {
arr1[i] = rd.nextInt(100);
}
//System.out.println(arr1);//地址值
//遍历
System.out.println(Arrays.toString(arr1));
//复制的第一种
int[] arr3 = Arrays.copyOf(arr1,3);//原数组 复制的长度
System.out.println(Arrays.toString(arr3));
//复制的第二种
int[] arr2 = new int[5];
System.arraycopy(arr1,3,arr2,0,5);//原数组 起始位置 目标数组 起始位置 复制的长度
System.out.println(Arrays.toString(arr2));
}
}
package cn.tedu.day04;
/**
* for死循环
*/
public class Loop1 {
public static void main(String[] args) {
/**
* for(①变量声明初始化;②判断条件;④表达式) {
* ③执行循环体
* }
*
* for(;;) {
*
* }
*/
/*for(;;) {
System.out.println("for死循环1");
}*/
/*for(int i = 0;;) {
System.out.println("for死循环2");
}*/
for(int i = 0;i > -1;i++) {
System.out.println("for死循环3");
}
}
}
package cn.tedu.day04;
/**
* do while死循环
*
* ①变量定义 初始化
* do {
* ②执行循环体 表达式
* } while(③判断条件);
*/
public class Loop2 {
public static void main(String[] args) {
/*boolean value1 = false;
do {
System.out.println("do while死循环1");
} while(value1);*/
/*do {
System.out.println("do while死循环2");
} while(10 > 0);*/
int k = 1;
do {
System.out.println("do while");
k++;
} while(k < 11);
}
}
package cn.tedu.day04;
/**
* 打印三角形
*/
public class Loop3 {
public static void main(String[] args) {
/**
* *****
* ****
* ***
* **
* *
*/
for(int i = 6;i > 1;i--) { //控制星号的行数
for(int j = 1;j < i;j++) {//控制每一行星号的个数
System.out.print("*");
}
//换行
System.out.println();
}
}
}
package cn.tedu.day04;
/**
* for循环
*/
public class Loop4 {
/**
* for(①变量定义初始化;②判断条件;④表达式) {
*
* //③循环体
* for(①变量定义初始化;②判断条件;④表达式) {
* ③循环体
* }
* }
*/
public static void main(String[] args) {
for(int y=1;y < 10;y++) {//控制行数
for(int x=1;x < y+1;x++) {//控制每行的乘法口诀
System.out.print(y + "*" + x + "=" + y*x + " ");
}
System.out.println();
}
}
}
package cn.tedu.day04;
/**
* do while 乘法口诀表
* ①变量定义和初始化
* do {
* ②循环体 表达式
* }while(③判断条件);
*/
public class Loop5 {
public static void main(String[] args) {
int k = 1;
do {
int x = 1;
do {
System.out.print(x + "*" + k + "=" + x*k + " ");
x++;
} while(x < k+1);
k++;
System.out.println();
} while(k < 10);
}
}
package cn.tedu.day04;
/**
* while - 乘法口诀表
* while() {
*
* }
*/
public class Loop6 {
public static void main(String[] args) {
int x = 1;
while (x< 10) {
int y = 1;
while(y < x+1) {
System.out.print(y + "*" + x + "=" + x*y + " ");
y++;
}
x++;
System.out.println();
}
}
}
package cn.tedu.day04;
import java.util.Arrays;//导别人包下面的类
import java.util.Random;
/**
* 验证码
*/
public class Array1 {
public static void main(String[] args) {
//1、定义一个存放验证码的数组(char类型,长度为4)
char[] arr1 = new char[4];
System.out.println("验证码数组赋值前的结果:" + Arrays.toString(arr1));
//2、生成验证码,并存放定义好的数组里面
//1-9 49-57
//A-Z 65-90
//a-z 97-122
int count = 0;//
Random rd = new Random();//创建随机对象
//死循环
for(int x = 0;x > -1;) {
//循环什么时候结束
if(count==4) {
break;
}
//生成随机数,范围[0,123)
int num1 = rd.nextInt(123);
//判断随机数是否符合我们要求的范围
if(num1>48 && num1<58) {
//赋值
arr1[count] = (char)num1;
count++;
} else if(num1>64 && num1<91) {
arr1[count] = (char)num1;
count++;
} else if(num1 > 96 && num1 < 123) {
arr1[count] = (char)num1;
count++;
}
}
System.out.println("验证码数组赋值后的结果:" + Arrays.toString(arr1));
//3、用户输入验证码,输入完以后和数组进行比对
//4、比对成功,进入系统,比对失败,提示验证码输入有误
}
}
package cn.tedu.day04;
import java.util.Scanner;
/**
* 猜数字
* 给定一个答案
* 让用户猜,如果用户猜的数比他大,提示:猜的数太大了,重新猜
* 如果用户猜的数比他小,提示:猜的数太小了,重新猜
* 直到猜对,结束程序
*/
public class Loop8 {
public static void main(String[] args) {
int num = 10;//给好的答案
System.out.println("请输入一个数字:");
for(;;) {
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();//用户输入的数
if(input > num) {
System.out.println("输入的数太大了,请重新输入");
} else if(input < num) {
System.out.println("输入的数太小了,请重写输入");
} else {
System.out.println("恭喜你猜对了");
break;
}
}
}
}
package cn.tedu.day04;
/**
求质数(素数)
*/
public class Loop9 {
public static void main(String[] args) {
int count = 0;//记录被整除的次数
for (int i = 2; i < 101; i++) {
for (int j = 2; j < 101; j++) {
if(i%j==0) {
count++;
}
}
if(count<=1) {
System.out.print(i + " ");
}
count = 0;
}
}
}
常用快捷:
psvm
sout
soutv
ctrl + c
ctrl + v
ctrl + shift + F10
ctrl + A
ctrl + Z
ctrl + alt + L
ctrl + /
ctrl + shift + /
System.out.println() 输出后换行
System.out.print() 输出后不换行