顺序结构
顺序结构是最简单的算法结构
他是所有算法离不开的结构
package JAVA.structure;
public class order {
public static void main(String[] args) {
System.out.println("hello1");
System.out.println("hello2");
System.out.println("hello3");
System.out.println("hello4");
System.out.println("hello5");
}
}
选择结构
if
if单选结构
package JAVA.structure;
import java.util.Scanner;
public class ifdemo1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入内容");
String s=scanner.nextLine();
if(s.equals("Hello")){//判断字符串是否一致
System.out.println(s);
}
scanner.close();
}
}
if双选结构
package JAVA.structure;
import java.util.Scanner;
public class ifDemo2 {
public static void main(String[] args) {
//考试大于60及格 否则不及格
Scanner scanner = new Scanner(System.in);
System.out.println("输入你的成绩");
int score= scanner.nextInt();
if(score>=60){
System.out.println("及格");
}
else {
System.out.println("不及格");
}
scanner.close();
}
}
if多选结构
package JAVA.structure;
import java.util.Scanner;
public class ifDemo3 {
public static void main(String[] args) {
//考试大于60及格 否则不及格
/*
在一个多行语句 else要放在最后 而且其中一个else if检测为true的时候 其他else if 以及else就不再判断
*/
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>=60){
System.out.println("C级");
}
//--------------------------------------------------
else if (score<60&&score>=0){
System.out.println("不及格");
}
//--------------------------------------------------
else {
System.out.println("成绩不合法");
}
scanner.close();
}
}
嵌套的if结构
省略
switch结构
JaveSE 7开始 switch开始支持 switch使用 String类型
package JAVA.structure;
public class switchDemo2 {
public static void main(String[] args) {
String name="lhc";
//JDK7的新特性 表达式结果可以是字符串
//字符的本质还是数字
//反编译 java---class(字节码文件)---反编译(IDEA)
switch (name){
case "lhc":
System.out.println("lhc");
break;
case "hh":
System.out.println("hh");
break;
case "cc":
System.out.println("cc");
break;
}
}
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package JAVA.structure;
public class switchDemo2 {
public switchDemo2() {
}
public static void main(String[] args) {
String name = "lhc";
byte var3 = -1;
switch(name.hashCode()) {
case 3168:
if (name.equals("cc")) {
var3 = 2;
}
break;
case 3328:
if (name.equals("hh")) {
var3 = 1;
}
break;
case 107111:
if (name.equals("lhc")) {
var3 = 0;
}
}
switch(var3) {
case 0:
System.out.println("lhc");
break;
case 1:
System.out.println("hh");
break;
case 2:
System.out.println("cc");
}
}
}
在class文件中 字符串的本质还是数字 lhc=3168
hh=3328 cc= 107111
匹配3168把 var3赋值为2
匹配3328把 var3赋值为1
匹配107111把 var3赋值为0
String name = "lhc";
byte var3 = -1;
switch(name.hashCode()) {
case 3168:
if (name.equals("cc")) {
var3 = 2;
}
break;
case 3328:
if (name.equals("hh")) {
var3 = 1;
}
break;
case 107111:
if (name.equals("lhc")) {
var3 = 0;
}
}
再写一个switch对var3判断
对应lhc(2) hh(1) cc(0)
得出结果
switch(var3) {
case 0:
System.out.println("lhc");
break;
case 1:
System.out.println("hh");
break;
case 2:
System.out.println("cc");
}
一定要多看源码!!!
一定要多看源码!!!
一定要多看源码!!!
循环结构
while
少数情况下需要死循环
package JAVA.structure;
public class whileDemo1 {
public static void main(String[] args) {
//输出1到100
int i=0;
i=0;
while (i<5){
System.out.println(++i);
}
}
}
package JAVA.structure;
public class whileDemo2 {
public static void main(String[] args) {
while (true){
//等待客户端连接
//定时检测
//.......
}
}
}
do…while
package JAVA.structure;
public class dowhileDemo2 {
public static void main(String[] args) {
int a=0;
while (a<0){
System.out.println(a);
a++;
}
//--------------------------------------------
do {
System.out.println(a);
a++;
}while (a<0);
}
}
for
是最有效 最灵活的循环结构
package JAVA.structure;
public class forDemo1<or> {
public static void main(String[] args) {
int a=1;//初始化条件
while (a<=100){//条件判断
System.out.println(a);
a+=2;//迭代
}
System.out.println("while循环结束");
//------------------------------------------
//初始化//条件判断//迭代//快捷键100.for 如果不确定次数可以输入 fori
for (int i=0;i<=100;i++){
System.out.println(i);
}
//三句话都可以为空 第二句默认为true
System.out.println("for循环结束");
}
}
package JAVA.structure;
public class forDemo2 {
public static void main(String[] args) {
//计算奇数偶数各自的和
int oddSum=0;
int evenSum=0;
for (int i = 1; i < 100; i++) {
if (i%2==0){//偶数
evenSum+=i;
}
else{//奇数
oddSum+=i;
}
}
System.out.println("奇数和是:"+oddSum);
System.out.println("偶数和是:"+evenSum);
}
}
package JAVA.structure;
public class forDemo3 {
public static void main(String[] args) {
//计算1-1000能被5整除的数 每行输出三个
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不会换行
//\n可以手动换行
}
}
}
package JAVA.structure;
public class forDemo4 {
public static void main(String[] args) {
//99乘法表
for (int i = 1; i <= 9; i++) {
System.out.println();
for (int j = 1; j <= i; j++) {
System.out.print(i+"*"+j+"="+i*j+"\t");
}
}
/*
1.首先打印第一列
2.把固定的i*1改成 i*j 用for包起来
3.消去多余的式子 j<=i (第一层循环表示一列 第二层循环表示一行 要消去的是多余的行 所以每行从1到9 正好对应i从小到大 所以j<i就可以解决这个问题)
4.调整样式
*/
/*
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
*/
}
}
增强for循环
用于数组和集合使用
package JAVA.structure;
public class forDemo5 {
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]+"\t");
}
//-----------------------------------------------
for(int x:numbers){
System.out.print(x+"\t");
}
//快捷键numbers.for 想要循环什么东西 前面 x.for就可以了
}
}
break
用于结束整个循环
package JAVA.structure;
public class breakDemo1 {
public static void main(String[] args) {
int i=0;
while (i<100){
i++;
System.out.println(i);
if(i==30){
break;
}
}
System.out.println("123");
}
}
continue
用于结束本次循环 但是要注意 他上面的语句还是会执行 使用一般要放在输出的上面
package JAVA.structure;
public class continueDemo1 {
public static void main(String[] args) {
int i=0;
while (i<50){
i++;
if(i%2==0){
continue;
}
//因为continue是结束本次循环 所以必须写在sout上面 不然还是会先输出 然后结束本次循环
System.out.println(i);
}
}
}
关于goto
package JAVA.structure;
public class laberDemo1 {
public static void main(String[] args) {
//打印101到150之间的质数
int count=0;
outer:for (int i = 101; i <= 150; i++) {
for(int j=2;j<i/2;j++){
if(i%j==0){
continue outer;
}
}
System.out.print(i+" ");
}
}
}
这里是label使用 目前goto只当做标识符去使用 不建议去用这个东西
test三角形
package JAVA.structure;
/*
*/
public class testDemo1 {
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-1; j++) {//为了对称 这里需要少打一个*
System.out.print("*");
}
System.out.println();
}
}
}