知识点1:switch-case
import java
.util
.Scanner
;
class SwitchCaseTest {
public static void main(String
[] args
) {
Scanner scan
= new Scanner(System
.in
);
System
.out
.println("请输入年份:");
int year
= scan
.nextInt();
System
.out
.println("请输入月份:(1-12)");
int month
= scan
.nextInt();
System
.out
.println("请输入日:(1-31)");
int day
= scan
.nextInt();
int sumDays
= 0;
switch (month
){
case 12:
sumDays
+=30;
case 11:
sumDays
+=31;
case 10:
sumDays
+=30;
case 9:
sumDays
+=31;
case 8:
sumDays
+=31;
case 7:
sumDays
+=30;
case 6:
sumDays
+=31;
case 5:
sumDays
+=30;
case 4:
sumDays
+=31;
case 3:
if((year
% 4 == 0 && year
% 100 != 0) || (year
% 400 == 0)){
sumDays
+=29;
}else{
sumDays
+=28;
}
case 2:
sumDays
+=31;
case 1:
sumDays
+= day
;
}
System
.out
.println(year
+ "年" + month
+ "月" + day
+ "日是当前的第" + sumDays
+ "天");
}
}
switch-case中不一定要使用break
class SwitchCaseTest1 {
public static void main(String
[] args
) {
int score
= 78;
switch(score
/ 10){
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
System
.out
.println("不合格");
break;
case 6:
case 7:
case 8:
case 9:
case 10:
System
.out
.println("合格");
break;
}
switch(score
/ 60){
case 0:
System
.out
.println("不合格");
break;
case 1:
System
.out
.println("合格");
break;
}
}
}
说明:如果多个case结构的执行语句相同,则可以考虑合并执行语句。
知识点2:for
循环结构的4个要素
① 初始化条件
② 循环条件 --->boolean类型
③ 循环体
④ 迭代部分
for(①;②;④){
③
}
执行过程:① - ② - ③ - ④ - ② - ③ - ④ - ② - ③ - ④ - .... - ②
class ForTest {
public static void main(String
[] args
) {
for(int i
= 1;i
<= 5;i
++){
System
.out
.println("Hello World!");
}
int i
= 1;
for(System
.out
.print('a');i
<= 3;System
.out
.print('c'),i
++){
System
.out
.print('b');
}
System
.out
.println();
int sum
= 0;
int count
= 0;
for(int j
= 1;j
<= 100;j
++){
if(j
% 2 == 0){
System
.out
.println(j
);
sum
+= j
;
count
++;
}
}
System
.out
.println("偶数总和为:" + sum
);
System
.out
.println("偶数个数为:" + count
);
}
}
练习1
class ForTest1 {
public static void main(String
[] args
) {
for(int i
= 1;i
<= 150;i
++){
System
.out
.print(i
+ " ");
if(i
% 3 == 0){
System
.out
.print("foo ");
}
if(i
% 5 == 0){
System
.out
.print("biz ");
}
if(i
% 7 == 0){
System
.out
.print("baz ");
}
System
.out
.println();
}
}
}
练习2
class ForTest2 {
public static void main(String
[] args
) {
int m
,n
;
m
= 12;
n
= 20;
int min
= (m
< n
)? m
: n
;
for(int i
= min
;i
>= 1;i
--){
if(m
% i
== 0 && n
% i
== 0){
System
.out
.println("最大公约数为:" + i
);
break;
}
}
int max
= (m
> n
)? m
: n
;
for(int i
= max
;i
<= m
* n
;i
++){
if(i
% m
== 0 && i
% n
== 0){
System
.out
.println("最小公倍数为:" + i
);
break;
}
}
}
}
知识点3:while
① 初始化条件
② 循环条件
--->boolean类型
③ 循环体
④ 迭代部分
while循环的结构
:
①
while(②
){
③
④
}
执行过程:①
- ②
- ③
- ④
- ②
- ③
- ④
- ②
- ③
- ④
- ... - ②
说明:
1. for循环和
while循环是可以相互转换的
2. while循环结束后,初始化条件中如果定义了变量,还可以继续使用。而
for循环不可以。
class WhileTest {
public static void main(String
[] args
) {
int i
= 1;
while(i
<= 100){
if(i
% 2 == 0){
System
.out
.println(i
);
}
i
++;
}
System
.out
.println(i
);
}
}
知识点4:do-while
① 初始化条件
② 循环条件
--->boolean类型
③ 循环体
④ 迭代部分
do-while循环的结构
:
①
do{
③
④
}while(②
);
执行过程:①
- ③
- ④
- ②
- ③
- ④
- ②
- ... - ②
说明:
do-while循环的循环体至少执行一次!
开发中使用
for和
while居多。
class DoWhileTest {
public static void main(String
[] args
) {
int i
= 1;
do{
if(i
% 2 == 0){
System
.out
.println(i
);
}
i
++;
}while(i
<= 100);
int num
= 10;
do{
System
.out
.println("hello,do-while!");
num
--;
}while(num
> 10);
int num1
= 10;
while(num1
> 10){
System
.out
.println("hello,while!");
num1
--;
}
}
}
知识点5:嵌套循环
嵌套循环的使用
1. 理解:在一个循环结构A的循环体中,又出现了另一个循环结构B。则构成嵌套循环。
其中,循环结构A称为外层循环;循环结构B称为内层循环
2. 外层循环每执行一次,内层循环就需要完成的遍历一遍所有的数据。
3. 外层循环遍历m次,内层循环遍历n次,则内层循环的循环体一共需要执行m
* n次
4. 技巧:外层循环控制行数,内层循环控制列数
class ForForTest {
public static void main(String
[] args
) {
for(int i
= 1;i
<= 8;i
++){
System
.out
.print("*");
}
System
.out
.println();
System
.out
.println();
/*
********
********
********
********
********
*/
for(int i
= 1;i
<= 5;i
++){
for(int j
= 1;j
<= 8;j
++){
System
.out
.print("*");
}
System
.out
.println();
}
for(int i
= 1;i
<= 5;i
++){
for(int j
= 1;j
<= i
;j
++){
System
.out
.print("*");
}
System
.out
.println();
}
for(int i
= 1;i
<= 4;i
++){
for(int j
= 1;j
<= 5 - i
;j
++){
System
.out
.print("*");
}
System
.out
.println();
}
for(int i
= 1;i
<= 5;i
++){
for(int j
= 1;j
<= 5 - i
;j
++){
System
.out
.print(" ");
}
for(int j
= 1;j
<= i
;j
++){
System
.out
.print("* ");
}
System
.out
.println();
}
}
}
练习:九九乘法表
/*
九九乘法表
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 ... 3 * 3 = 9
...
1 * 9 = 9 ... 9 * 9 = 81
*/
class NineNineTable {
public static void main(String[] args) {
for(int i = 1;i <= 9;i++){
for(int j = 1;j <= i;j++){
System.out.print(j + "*" + i + "=" + j * i + " ");
}
System.out.println();
}
}
}
质数的输出
基本实现:
class PrimeNumberTest {
public static void main(String
[] args
) {
boolean flag
= true;
for(int i
= 2;i
<= 100;i
++){
for(int j
= 2;j
< i
;j
++){
if(i
% j
== 0){
flag
= false;
}
}
if(flag
){
System
.out
.println(i
);
}
flag
= true;
}
}
}
优化以后:
class PrimeNumberTest1 {
public static void main(String
[] args
) {
long start
= System
.currentTimeMillis();
int count
= 0;
boolean flag
= true;
for(int i
= 2;i
<= 100000;i
++){
for(int j
= 2;j
<= Math
.sqrt(i
);j
++){
if(i
% j
== 0){
flag
= false;
break;
}
}
if(flag
){
count
++;
}
flag
= true;
}
long end
= System
.currentTimeMillis();
System
.out
.println("花费的时间为:" + (end
- start
));
System
.out
.println(count
);
}
}
使用带标签的continue的实现:
class PrimeNumberTest2 {
public static void main(String
[] args
) {
l
:for(int i
= 2;i
<= 100;i
++){
for(int j
= 2;j
<= Math
.sqrt(i
);j
++){
if(i
% j
== 0){
continue l
;
}
}
System
.out
.println(i
);
}
}
}
如何结束一个循环结构
1. 循环结构如果不结束,那就是一个死循环!
2. 开发中,要避免出现死循环!
3. 如何结束一个循环结构呢?
> 方式一:循环条件返回false
> 方式二:循环体中执行break
4. while(true) 或 for(;;)的使用
练习
class ForWhileTest {
public static void main(String
[] args
) {
Scanner scan
= new Scanner(System
.in
);
int positiveNumber
= 0;
int negativeNumber
= 0;
for(;;){
System
.out
.println("请输入一个整数。(退出请输入0)");
int num
= scan
.nextInt();
if(num
== 0){
break;
}else if(num
> 0){
positiveNumber
++;
}else{
negativeNumber
++;
}
}
System
.out
.println("正数的个数为:" + positiveNumber
);
System
.out
.println("负数的个数为:" + negativeNumber
);
}
}
知识点6:break和continue
class BreakContinueTest {
public static void main(String
[] args
) {
for(int i
= 1;i
<= 10;i
++){
if(i
% 4 == 0){
continue;
}
System
.out
.print(i
);
}
System
.out
.println("#######################");
label
:for(int i
= 1;i
<= 5;i
++){
for(int j
= 1;j
<= 10;j
++){
if(j
% 4 == 0){
continue label
;
}
System
.out
.print(j
);
}
System
.out
.println();
}
}
}
练习
class Exer{
public static void main(String
[] args
){
int a
= 10;
int b
= 35;
int c
= 45;
int max
;
if(a
>= b
){
if(a
>= c
){
max
= a
;
}else{
max
= c
;
}
}else{
if(b
>= c
){
max
= b
;
}else{
max
= c
;
}
}
System
.out
.println(max
);
int max1
;
if(a
>= b
&& a
>= c
){
max1
= a
;
}else if(b
>= a
&& b
>= c
){
max1
= b
;
}else{
max1
= c
;
}
}
}
import java
.util
.Scanner
;
class Exer1 {
public static void main(String
[] args
) {
Scanner scan
= new Scanner(System
.in
);
System
.out
.println("请输入第一个整数num1:");
int num1
= scan
.nextInt();
System
.out
.println("请输入第二个整数num2:");
int num2
= scan
.nextInt();
System
.out
.println("请输入第三个整数num3:");
int num3
= scan
.nextInt();
if(num1
>= num2
&& num1
>= num3
){
if(num2
> num3
){
System
.out
.println("从小到大排列为" + num3
+"-" + num2
+ "-" + num1
);
}else{
System
.out
.println("从小到大排列为" + num2
+ "-" + num3
+ "-" + num1
);
}
}else if(num1
<= num2
&& num3
<= num2
){
if(num1
> num3
){
System
.out
.println("从小到大排列为" + num3
+ "-" + num1
+ "-" + num2
);
}else{
System
.out
.println("从小到大排列为" + num1
+ "-" + num3
+ "-" + num2
);
}
}else if(num3
>= num1
&& num3
>= num2
){
if(num1
> num2
){
System
.out
.println("从小到大排列为" + num2
+ "-" + num1
+ "-" + num3
);
}else{
System
.out
.println("从小到大排列为" + num1
+ "-" + num2
+ "-" + num3
);
}
}
Scanner scan
= new Scanner(System
.in
);
System
.out
.println("请输入第一个数字");
int num1
= scan
.nextInt();
System
.out
.println("请输入第二个数字");
int num2
= scan
.nextInt();
System
.out
.println("请输入第三个数字");
int num3
= scan
.nextInt();
int temp
= 0;
if(num1
> num2
){
temp
= num2
;
num2
= num1
;
num1
= temp
;
}
if(num1
> num3
){
temp
= num3
;
num3
= num1
;
num1
= temp
;
}
if(num2
> num3
){
temp
= num3
;
num3
= num2
;
num2
= temp
;
}
System
.out
.println("从小到大输出:" + num1
+ " "+ num2
+ " " + num3
);
Scanner scan
= new Scanner(System
.in
);
System
.out
.println("请输入三个整数:");
int num1
= scan
.nextInt();
int num2
= scan
.nextInt();
int num3
= scan
.nextInt();
if(num1
> num2
){
if(num2
> num3
){
System
.out
.println(num1
+ "," + num2
+ "和" + num3
+"的从小到大的顺序为:" + num3
+ "," + num2
+ "," + num1
);
}else if(num3
> num1
){
System
.out
.println(num1
+ "," + num2
+ "和" + num3
+"的从小到大的顺序为:" + num2
+ "," + num1
+ "," + num3
);
}else{
System
.out
.println(num1
+ "," + num2
+ "和" + num3
+"的从小到大的顺序为:" + num2
+ "," + num3
+ "," + num1
);
}
}else{
if(num2
< num3
){
System
.out
.println(num1
+ "," + num2
+ "和" + num3
+"的从小到大的顺序为:" + num1
+ "," + num2
+ "," + num3
);
}else if(num3
< num1
){
System
.out
.println(num1
+ "," + num2
+ "和" + num3
+"的从小到大的顺序为:" + num3
+ "," + num1
+ "," + num2
);
}else{
System
.out
.println(num1
+ "," + num2
+ "和" + num3
+"的从小到大的顺序为:" + num1
+ "," + num3
+ "," + num2
);
}
}
}
}
import java
.util
.Scanner
;
class Exer2 {
public static void main(String
[] args
) {
Scanner scan
= new Scanner(System
.in
);
System
.out
.println("请输入月份:(1-12)");
int month
= scan
.nextInt();
System
.out
.println("请输入日:(1-31)");
int day
= scan
.nextInt();
int sumDays
= 0;
switch (month
){
case 12:
sumDays
+=30;
case 11:
sumDays
+=31;
case 10:
sumDays
+=30;
case 9:
sumDays
+=31;
case 8:
sumDays
+=31;
case 7:
sumDays
+=30;
case 6:
sumDays
+=31;
case 5:
sumDays
+=30;
case 4:
sumDays
+=31;
case 3:
sumDays
+=29;
case 2:
sumDays
+=31;
case 1:
sumDays
+= day
;
}
}
}
import java
.util
.Scanner
;
class Exer3 {
public static void main(String
[] args
) {
Scanner scan
= new Scanner(System
.in
);
System
.out
.println("请输入月份:(1-12)");
int month
= scan
.nextInt();
System
.out
.println("请输入日:(1-31)");
int day
= scan
.nextInt();
int sumDays
= 0;
switch (month
){
case 12:
sumDays
+=30;
case 11:
sumDays
+=31;
case 10:
sumDays
+=30;
case 9:
sumDays
+=31;
case 8:
sumDays
+=31;
case 7:
sumDays
+=30;
case 6:
sumDays
+=31;
case 5:
sumDays
+=30;
case 4:
sumDays
+=31;
case 3:
sumDays
+=29;
case 2:
sumDays
+=31;
case 1:
sumDays
+= day
;
}
String info
;
switch(sumDays
% 7 ){
case 0:
info
= "星期二";
break;
case 1:
info
= "星期三";
break;
case 2:
info
= "星期四";
break;
case 3:
info
= "星期五";
break;
case 4:
info
= "星期六";
break;
case 5:
info
= "星期日";
break;
case 6:
info
= "星期一";
break;
default:
info
= "";
}
System
.out
.println(month
+ "月" + day
+ "日是2020年的" + info
);
}
}