顺序结构
- java的基本结构就是顺序结构,除非特别说明,否则就按照顺序一句一句执行。
选择结构
if选择结构
- if单选择结构
package com.cheng.struct;
import java.util.Scanner;
public class IfDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容:");
String s = scanner.nextLine();
//equals:判断字符串是否相等
//if单选择结构
if(s.equals("Hello")){
System.out.println(s);
}
System.out.println("End");
scanner.close();
}
}
- if多选择结构
package com.cheng.struct;
import java.util.Scanner;
public class IfDemo02 {
//if多项选择结构
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt();
if(score == 100){
System.out.println("恭喜满分!");
}else if(score < 100&&score >=90){
System.out.println("A");
}else if(score < 90&&score >=80){
System.out.println("B");
}else if(score < 80&&score >=70){
System.out.println("C");
} else if(score < 70&&score >=60){
System.out.println("D");
}
else if(score < 60&&score >=0){
System.out.println("不及格");
}else
System.out.println("成绩不合法!");
scanner.close();
}
}
switch多选择结构
- 多选择结构还有一个实现方式就是switch case语句
- switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
- switch语句中的变量类型可以是:
- byte、short、int和char
- 从java SE 7开始
- switch支持字符串String类型
- 同时case标检必须为字符串常量或字面量
package com.cheng.struct;
public class SwitchDemo01 {
public static void main(String[] args) {
char grade ='C';
switch (grade){
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("好");
break;
case 'D':
System.out.println("一般");
break;
case 'E':
System.out.println("挂科");
break;
default:
System.out.println("未知等级");
}
}
}
循环结构
package com.cheng.struct;
public class WhileDemo01 {
public static void main(String[] args) {
//计算1+2+3+....+100=?
int i = 0;
int sum = 0;
while (i<100){
i++;
sum+=i;
}
System.out.println(sum);//sum = 5050
}
}
do while循环
for循环(重要)
- 练习1:计算0到100之间的奇数和偶数的和
package com.cheng.struct;
public class ForDemo01 {
public static void main(String[] args) {
//练习1:计算0到100之间的奇数和偶数的和
int oddSum = 0;//初始化奇数
int evenSum = 0;//初始化偶数
for (int i= 0;i<100;i++){
if (i%2!=0){
oddSum+=i;
}else
evenSum+=i;
}
System.out.println("奇数的和:"+oddSum);
System.out.println("偶数的和:"+evenSum);
System.out.println("==============================");
int [] number = {1,2,3,4,5};
for (int x:number){
System.out.print(x+"\t");
}
/*
相当于for(int i=0;i<5;i++){
System.out.println(number[i]);
}
*/
}
}
- 练习2:用while或for循环输出1-1000之间能被5整除的书数,并且每行输出3个
package com.cheng.struct;
public class ForDemo02 {
public static void main(String[] args) {
//练习2.用while或for循环输出1-1000之间能被5整除的书数,并且每行输出3个
for (int i =0;i<=1000;i++){
if(i%5==0){
System.out.print(i+"\t");
}
if(i%(5*3)==0){
System.out.println();
//System.out.print("\n");
}
}
//println 输出完会换行
//print 输出完不会换行
}
}
- 练习3:打印九九乘法表
package com.cheng.struct;
public class ForDemo03 {
public static void main(String[] args) {
//打印九九乘法表
/*
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
*/
for (int j=1;j<=9;j++){
for (int i = 1;i <= j;i++){
System.out.print(i+"*"+j+"="+(i*j)+"\t");
}
System.out.println();
}
}
}
附加1:break continue
附加2:打印三角形
package com.cheng.struct;
public class TestDemo {
public static void main(String[] args) {
//打印三角形 5行
for (int i= 1;i<=5;i++){
for (int j = 5;j>=i;j--){
System.out.print(" ");
}
for (int j= 1;j<=i;j++){
System.out.print("*");
}
for (int j= 1;j<i;j++){
System.out.print("*");
}
System.out.println();
}
}
}
运行截图:
附加3:打印平行四边形
package com.cheng.struct;
public class TestDemo02 {
public static void main(String[] args) {
//打印平行四边形 4行
for (int i= 1;i<=4;i++){
for (int j = 4;j>=i;j--){
System.out.print(" ");
}
for (int j= 1;j<=i;j++){
System.out.print("*");
}
for (int j = 4;j>i;j--){
System.out.print("*");
}
System.out.println();
}
}
}
运行截图:
附加4:打印菱形
package com.cheng.struct;
public class TestDemo03 {
public static void main(String[] args) {
//打印菱形 9行
for (int i= 1;i<=5;i++){
for (int j = 5;j>=i;j--){
System.out.print(" ");
}
for (int j= 1;j<=i;j++){
System.out.print("*");
}
for (int j= 1;j<i;j++){
System.out.print("*");
}
System.out.println();
}
for (int i= 1;i<=4;i++){
for (int j= 0;j<=i;j++){
System.out.print(" ");
}
for (int j = 4;j>=i;j--){
System.out.print("*");
}
for (int j = 4;j>i;j--){
System.out.print("*");
}
System.out.println();
}
}
}
运行截图: