一、if单选泽结构
源代码:
import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) {
//if单选泽结构
Scanner scanner=new Scanner(System.in);
System.out.println("请输入内容:");
String s=scanner.nextLine();
if(s.equals("Hello")){
System.out.println(s);
}
System.out.println("End");
scanner.close();
}
}
运行结果:
请输入内容:
12
End
二、if双选择结果
源代码:
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args) {
//if双选择结构
Scanner scanner=new Scanner(System.in);
System.out.println("请输入你的成绩:");
int i=scanner.nextInt();
if(i>=60){
System.out.println("及格");
}else{
System.out.println("不及格");
}
scanner.close();
}
}
运行结果:
请输入你的成绩:
90
及格
三、if多选择结构
源代码:
import java.util.Scanner;
public class Demo3 {
public static void main(String[] args) {
//if多选择结构
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>=80 &&score<90){
System.out.println("B");
}else if(score>=60 &&score<80){
System.out.println("C");
}else if(score>=0 &&score<60){
System.out.println("不及格!");
}else{
System.out.println("您输入的成绩不合法!");
}
scanner.close();
}
}
运行结果:
请输入你的成绩:
98
A
四、嵌套的if结构
源代码:
import java.util.Scanner;
public class Demo4 {
public static void main(String[] args) {
//嵌套的if结构
Scanner scanner=new Scanner(System.in);
System.out.println("请输入一个数:");
int i=scanner.nextInt();
if(i<=100 &&i>=0){
if(i<=50){
System.out.println("您输入的数的范围为:0~50");
}else {
System.out.println("您输入的数的范围为:50~100");
}
}else {
System.out.println("你输入的数的范围不在0~100");
}
scanner.close();
}
}
运行结果:
请输入一个数:
85
您输入的数的范围为:50~100
五、switch多选择结构
源代码:
import java.util.Scanner;
public class Demo5 {
public static void main(String[] args) {
//switch多选择结构
//JDk7的新特性,表达式结果可以是字符串!!!
//字符的本质还是数字
Scanner scanner=new Scanner(System.in);
System.out.println("请输入一个等级:");
String c=scanner.nextLine();
switch(c){
case "S":
System.out.println("恭喜满分!");
break;
case "A":
System.out.println("恭喜90~100");
break;
case "B":
System.out.println("恭喜80~90");
break;
case "C":
System.out.println("及格,再接再厉!");
break;
case "D":
System.out.println("不及格,还需努力!");
break;
default:
System.out.println("您输入的不合法!");
}
scanner.close();
}
}
运行结果:
请输入一个等级:
A
恭喜90~100
六、while循环
源代码:
/**
* 循环结构:while循环、do...while循环、for循环(Java5引入的数组的增强型for循环)
*/
public class Demo6 {
public static void main(String[] args) {
/*
while循环
计算1+2+3+...+100=?
特别的 while(true){} 死循环 eg:客户端连接
*/
int i=0;
int sum=0;
while(i<=100){
sum+=i;
i++;
}
System.out.println(sum);
}
}
运行结果:
5050
七、do…while循环
源代码:
public class Demo7{
public static void main(String[] args) {
/*
do..while循环
对于while语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次。
do...while循环和while循环相似不同的是,do..while循环至少会执行一次。
*/
int i=0;
int sum=0;
do{
sum+=i;
i++;
}while(i<=100);
System.out.println(sum);
}
}
运行结果:
5050
源代码:
public class Demo8 {
public static void main(String[] args) {
//while与do..while的主要区别
int a1=0;
while(a1<0){
System.out.println(a1);
a1++;
}
System.out.println("*******************************");
int a2=0;
do{
System.out.println(a2);
a2++;
}while(a2<0);
}
}
运行结果:
*******************************
0
八、for循环
源代码:
public class Demo9 {
public static void main(String[] args) {
//for循环
// 特别的 死循环 for(; ;){}
int sum=0;
for(int i=1;i<=100;i++){
sum+=i;
}
System.out.println(sum);
}
}
运行结果:
5050
练习1:计算0~100之间的奇数和偶数的和
源代码:
public class Demo10 {
public static void main(String[] args) {
//练习1:计算0~100之间的奇数和偶数的和
int sum1=0;
int sum2=0;
for(int i=0;i<=100;i++){
if(i%2==0){
sum1+=i;
}
if(i%2!=0){
sum2+=i;
}
}
System.out.println("奇数和为:"+sum2);
System.out.println("偶数和为:"+sum1);
}
}
运行结果:
奇数和为:2500
偶数和为:2550
练习2:用while或for循环被5输出1~1000之间能被5整除的数,并且每行输出3个。
源代码:
public class Demo11 {
public static void main(String[] args) {
//练习2:用while或for循环被5输出1~1000之间能被5整除的数,并且每行输出3个。
for(int i=1;i<=1000;i++){
if(i%5==0){
System.out.print(i+"\t");
}
if(i%15==0){
System.out.println();
}
}
}
}
运行结果:
5 10 15
20 25 30
35 40 45
50 55 60
65 70 75
80 85 90
。。。。。。。
980 985 990
995 1000
练习3:打印九九乘法表
源代码:
public class Demo12 {
public static void main(String[] args) {
//练习3:打印九九乘法表
for(int i=1;i<=9;i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + "*" + j + "=" + i * j+"\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循环
源代码:
public class Demo13 {
public static void main(String[] args) {
int []numbers={10,20,30,40,50};
//增强for循环遍历数组
for(int n:numbers){
System.out.print(n+"\t");
}
System.out.println();
//for循环遍历数组
for(int i=0;i<5;i++){
System.out.print(numbers[i]+"\t");
}
}
}
运行结果:
10 20 30 40 50
10 20 30 40 50
十、break与continue语句
源代码:
public class Demo14 {
public static void main(String[] args) {
/*
break用于 强行退出 循环,不执行循环中剩余的语句(break语句也在switch语句中使用)。
continue语句用于终止 某次 循环过程,即跳过循环体中尚未执行的语句,接着执行下一次是否执行循环的判定。
*/
for(int i=0;i<=10;i++){
System.out.print(i+"\t");
if(i>7){
break;
}
}
System.out.println("End");
for(int m=1;m<=100;m++){
if(m%10==0){
System.out.println();
continue;
}
System.out.print(m+"\t");
}
}
}
运行结果
0 1 2 3 4 5 6 7 8 End
1 2 3 4 5 6 7 8 9
11 12 13 14 15 16 17 18 19
21 22 23 24 25 26 27 28 29
31 32 33 34 35 36 37 38 39
41 42 43 44 45 46 47 48 49
51 52 53 54 55 56 57 58 59
61 62 63 64 65 66 67 68 69
71 72 73 74 75 76 77 78 79
81 82 83 84 85 86 87 88 89
91 92 93 94 95 96 97 98 99
练习4:打印101~150之间的所有质数
源代码:
public class Demo15 {
public static void main(String[] args) {
//打印101~150之间的所有质数
outo: for(int i=101;i<150;i=i+2){
for(int j=2;j<i/2;j++){
if(i%j==0){
continue outo;
}
}
System.out.println(i);
}
}
}
运行结果:
101
103
107
109
113
127
131
137
139
149
练习5:打印三角形
源代码:
public class Demo16 {
public static void main(String[] args) {
//打印三角形
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();
}
}
}
运行结果:
*
***
*****
*******
*********