IF单选择结构
package ifjava;
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容:>");
String s = scanner.nextLine();
// equals 用来比较字符串是否相同
if (s.equals("hello")) {
System.out.println(s);
}
System.out.println("end");
scanner.close();
}
}
// -----------------输出--------------
hello
end
IF双结构
package ifjava;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
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、else if、else结构
package ifjava;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩》");
int score = scanner.nextInt();
if (score > 60 && score < 75) {
System.out.println("成绩为C");
} else if (score >= 75 && score < 90) {
System.out.println("成绩为B");
} else if (score >= 90 && score < 100) {
System.out.println("成绩为A");
} else if (score >= 0 && score < 60) {
System.out.println("不及格");
} else {
System.out.println("满分牛逼");
}
scanner.close();
}
}
Switch
package switchjava;
public class Demo01 {
public static void main(String[] args) {
// case 穿透 switch 匹配一个具体的值
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
default:
System.out.println("这。。。");
}
}
}
while
package xhjava;
public class whilejava {
public static void main(String[] args) {
int i = 0;
while (i <= 100) {
System.out.println(i);
i += 1;
}
int k = 0;
int sum = 0;
while (k <= 100) {
sum = sum + k;
k++;
System.out.println(sum);
}
}
}
do while
package xhjava;
public class DoWhileJava {
public static void main(String[] args) {
// do while 即使不满足条件也要执行一次
int i = 0;
int sum = 0;
// 先执行do里面的内容 : i=100的时候,在执行while判断
do {
sum = sum + i;
i++;
} while (i <= 100);
System.out.println(sum);
}
}
For循环
package xhjava;
public class Forjava {
public static void main(String[] args) {
int i = 1; // 初始化条件
while (i <= 100) {
System.out.println(i);
i += 2; //迭代
}
System.out.println("while end---");
// 初始化 // 条件判断 // 迭代
for (int a = 1; a <= 100; a++) {
System.out.println(a);
}
System.out.println("for end--");
}
}
0-100的奇数合偶数的和
package xhjava;
public class ForTestJava {
public static void main(String[] args) {
// 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);
}
}
用while方法或for循环 1-1000之间能被5整数的数,并每行打印3个
package xhjava;
public class WhileTestJava {
public static void main(String[] args) {
// 用while方法或for循环 1-1000之间能被5整数的数,并每行打印3个
for (int i = 0; i < 1000; i++) {
if (i % 5 == 0) {
// print 输出完不会换行
System.out.print(i + "\t");
}
if (i % (5 * 3) == 0) {
System.out.println("\n");
}
}
int i = 0;
while (i <= 1000) {
i += 1;
if (i % 5 == 0) {
// print 输出完不会换行
System.out.print(i + "\t");
}
if (i%(5*3)==0){
System.out.println("\n");
}
}
}
}
99乘发表
package xhjava;
public class NineNineJava {
public static void main(String[] args) {
// 99 乘发表
for (int i = 1; i <= 9; i++) {
for (int a = 1; a <= i; a++) {
System.out.print(i+"*"+a+"="+(a * i) + "\t");
}
System.out.println(" ");
}
}
}
// ===========================================
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 xhjava;
public class ZforJava {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};// 定义数组
// 遍历数组中的元素
for (int x : numbers) {
System.out.println(x);
}
}
}
//=============================
10
20
30
40
50