目录
一、选择结构1.单分支2.双分支3.多分支4.嵌套分支
二、循环结构1.循环结构四要素2.基础循环3.增强循环
三、一维数组1.使用步骤2.优缺点3.特性
一、选择结构
1.单分支
if(条件){代码}
public class Test {
public static void main(String
[] args
){
int score
= 90;
if(score
>=80){
System
.out
.println("成绩达标");
}
}
}
2.双分支
if(条件){代码}else{代码}
import java
.util
.Scanner
;
public class Test {
public static void main(String
[] args
){
Scanner input
= new Scanner(System
.in
);
int score
= input
.nextInt();
if(score
>=80){
System
.out
.println("成绩达标");
}else{
System
.out
.println("成绩不达标");
}
}
}
3.多分支
if做区间条件 if(条件1){代码1}else if(条件2){代码2}…else{代码N}
import java
.util
.Scanner
;
public class Test {
public static void main(String
[] args
){
Scanner input
= new Scanner(System
.in
);
int score
= input
.nextInt();
if(score
>=90){
System
.out
.println("特等奖");
}else if (score
>=80){
System
.out
.println("一等奖");
}else if (score
>=75){
System
.out
.println("二等奖");
}else if (score
>=70){
System
.out
.println("三等奖");
}else{
System
.out
.println("参与奖");
}
}
}
switch做等值条件 switch(算术表达式){case 常量1:代码1;break;…default:代码N;}
import java
.util
.Scanner
;
public class Test {
public static void main(String
[] args
){
Scanner input
= new Scanner(System
.in
);
int year
,month
,day
=0;
System
.out
.println("请输入年份:");
year
= input
.nextInt();
System
.out
.println("请输入月份:");
month
= input
.nextInt();
System
.out
.println(year
+(year
%4==0 ? "是":"非")+"闰年");
switch(month
){
case 1: case 3: case 5:case 7:case 8:case 10:case 12:
day
= 31;
break;
case 4: case 6: case 9:case 11:
day
= 30;
break;
case 2:
day
= year
%4==0 ? 29 :28;
break;
}
System
.out
.println(MessageFormat
.format("{0}年{1}月有{2}天",year
,month
,day
));
4.嵌套分支
if(条件){if(条件){代码}}
import java
.util
.Scanner
;
public class Test {
public static void main(String
[] args
){
Scanner input
= new Scanner(System
.in
);
int score
;
char gender
;
score
= input
.nextInt();
gender
= input
.next().charAt(0);
if(score
<=10){
gender
= input
.next().charAt(0);
if(gender
!= '男' && gender
!= '女'){
System
.out
.println("性别有误");
}else{
System
.out
.println("恭喜你进入"+gender
+"子组决赛!");
}
}else{
System
.out
.println("成绩不达标,淘汰");
}
}
}
二、循环结构
1.循环结构四要素
循环变量的声明和初始化:初始化表达式循环条件(变量的作用域/生命周期):布尔表达式循环变量的更新:更新表达式循环体:Java语句构成的重复执行代码
2.基础循环
循环次数不确定
while(条件){循环体}
public class Test {
public static void main(String
[] args
){
byte count
= 1;
while(count
<=100){
System
.out
.println("第"+count
+"次:我爱你");
count
++;
}
}
}
do{循环体}while(条件)
public class Test {
public static void main(String
[] args
){
byte count
= 1;
do{
System
.out
.println("第"+count
+"次:我爱你");
count
++;
}while(count
<=100);
}
}
循环次数确定
for(循环变量的声明和初始化;条件;循环变量的更新){循环体}
public class Test {
public static void main(String
[] args
){
for (byte count
= 1;count
<=100;count
++){
System
.out
.println("第"+count
+"次:我爱你");
}
}
}
循环跳转语句
continue;(结束本次循环,进入下次循环) break;(默认情况终止当前循环,进入循环外的下一条语句,也可以指定终止某个循环)
public class Test {
public static void main(String
[] args
){
Random rand
= new Random();
int num
= 1+rand
.nextInt(100);
boolean isPrime
= true;
for(int i
= 2;i
<=num
/2;i
++){
if(num
%i
==0){
isPrime
= false;
break;
}
}
System
.out
.println(num
+(isPrime
? "是" : "非")+"质数");
}
}
3.增强循环
for(xxx v : arr){ v[0 ~ arr.length-1] }
三、一维数组
1.使用步骤
声明:xxx[] array;分配空间:array = new xxx[int LENGTH];赋值:array[int INDEX]使用:System.out.println(array[int INDEX]);复合语法(1+2+3) xxx[] array = {VALUE1,VALUE2,…} xxx[] array = new int[]{VALUE1,VALUE2,…}
2.优缺点
优点 类型相同:不用考虑类型 连续:遍历(寻址)缺点 类型相同:无法解决不同类型多个值的存储 连续:插入,移除
3.特性
不可变长度:int len = array.length;下标(索引):0 ~ array.length-1
public class Test {
public static void main(String
[] args
){
Random rand
= new Random();
int[] arr
= new int[10];
for (int i
= 0; i
< arr
.length
; i
++) {
arr
[i
] = rand
.nextInt(100)+1;
}
int num
= rand
.nextInt(100)+1;
boolean has
= false;
for (int i
= 0; i
< arr
.length
; i
++) {
if(num
== arr
[i
]){
has
= true;
break;
}
}
for (int i
= 0; i
< arr
.length
; i
++) {
System
.out
.print(arr
[i
]+"\t");
}
System
.out
.println();
System
.out
.println("该数"+num
+has
? "存在" : "不存在"+"于此数组中");
}
}