程序结构
顺序结构
它是任何一个算法都离不开的一种基本算法结构
选择结构
if单选择
if双选择
if(){
}else{
}
if多选择结构
if(){
}else if(){
}else{
}
多选择结构switch
switch语句中的变量类型可以是:byte、short、int、char
switch(expression){
case value:
break;//可选 若不写break 则后面的语句全都会输出——case穿透
case value:
break;//可选
default: //可选
}
if 判断区间 Switch匹配具体的值
Java SE(JDK7)开始 Switch支持字符串String类型了,同时case标签必须为字符串常量或字面量
package com.msj.structure;
public class SwitchDemo02 {
public static void main(String[] args) {
String name = "狂神";
//JDK7的特性,表达式结果可以是字符串!!!
//字符的本质还是数字
//反编译 java---class(字节码文件)---反编译(IDEA)
switch(name){
case "秦疆":
System.out.println("秦疆");
break;
case"狂神":
System.out.println("狂神");
break;
default:
System.out.println("弄啥嘞!");
}
}
}
反编译
project structure–>将class文件复制到–>show in explore
在IDEA界面双击打开–>源文件右键split right
字节码文件
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.msj.structure;
public class SwitchDemo02 {
public SwitchDemo02() {
}
public static void main(String[] args) {
String name = "狂神";
byte var3 = -1;
switch(name.hashCode()) {
case 941788:
if (name.equals("狂神")) {
var3 = 1;
}
break;
case 997472:
if (name.equals("秦疆")) {
var3 = 0;
}
}
switch(var3) {
case 0:
System.out.println("秦疆");
break;
case 1:
System.out.println("狂神");
break;
default:
System.out.println("弄啥嘞!");
}
}
}
看源码
循环结构
重复执行相同的操作
我们大多数情况是会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环
while
package com.msj.structure;
import org.w3c.dom.ls.LSOutput;
public class WhileDemo03 {
public static void main(String[] args) {
//计算1+2+3+...+100=?
//高斯的故事
int i = 0;
int sum = 0;
while(i<=100){
sum=sum+i;
i=i++;
}
System.out.println(sum);
}
}
DoWhile
package com.msj.structure;
public class DoWhlieDemo01 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
do {
sum=sum+i;
i=i++;
}while(i<=100)
System.out.println(sum);
}
}
while和DoWhlie的区别
whlie先判断后执行,dowhile是先执行后判断
dowhile总是保障循环体至少会被执行一次!
for
最有效最灵活的循环结构
快捷键:100.for
package com.msj.structure;
public class ForDemo01 {
public static void main(String[] args) {
int a = 1; //初始化条件
while (a <= 100) {
System.out.println(a); //循环体
a += 2; //迭代
}
System.out.println("whlie循环结束!");
//初始化//条件判断//迭代
for (int i = 1; i <= 100; i++) {
System.out.println(a);
}
for (int i = 0; i < 100; i++) { //快捷键100.for
}
System.out.println("for循环结束!");
//死循环
for (;;){
}
}
}
练习1:计算0~100之间的奇数和偶数的和
package com.msj.structure;
public class ForDemo02 {
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; //oddSum=oddSum+i
}else {
evenSum+=i;
}
}
System.out.println("奇数的和:"+oddSum);
System.out.println("偶数的和:"+evenSum);
}
}
练习2:用while或for循环输出1-1000之间能被5整除的数,且每行输出3个S
package com.msj.structure;
public class ForDemo03 {
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.println("\n");
}
}
//println 输出完会换行
//print 输出完不会换行
}
}
练习3:打印九九乘法表
package com.msj.structure;
public class ForDemo04 {
public static void main(String[] args) {
//1.先打印第一列
//2.把固定的 1 再用一个循环包起来
//3.去掉重复项,i<=j
//4.调整样式
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(i+"*"+j+"="+(j*i)+"\t");
}
System.out.println();
}
}
}
增强for
java5引入了一种主要用于数组或集合的增强型for循环
package com.msj.structure;
public class ForDemo05 {
public static void main(String[] args) {
int [] numbers = {10,20,30,40,50};//定义了一个数组
for (int i = 0; i < 5; i++) {
System.out.println(numbers[i]);
}
System.out.println("===============");
//遍历数组的元素
for (int x:numbers){
System.out.println(x);
}
}
}
例题:输出三角形
package com.msj.structure;
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();
}
}
}
思路