1.if条件语句
package daigua2;
/*
* if条件语句有三种写法
* */
public class IfElse1 {
public static void main(String[] args) {
int a = 10;
int b = 10;
// 写法1
if(a==b) {
System.out.println("a等于b!");
}
// 写法2
a = 99;
if (a % 2 == 0) {
System.out.println("a是偶数!");
} else {
System.out.println("a是奇数!");
}
// 写法3
if (a < 10) {
System.out.println("a太小了!");
} else if (a < 50){
System.out.println("a还可以!");
} else {
System.out.println("a太大了!");
}
}
}
2.switch语句
package daigua2;
import java.util.Scanner;
public class Switch1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入星期数:");
int weekDay = sc.nextInt();
switch (weekDay) {
case 1:
System.out.println("今天是星期1");
break;
case 2:
System.out.println("今天是星期2");
break;
case 3:
System.out.println("今天是星期3");
break;
case 4:
System.out.println("今天是星期4");
break;
case 5:
System.out.println("今天是星期5");
break;
case 6:
System.out.println("今天是星期6");
break;
case 7:
System.out.println("今天是星期7");
break;
default:
System.out.println("输入错误!");
break;
}
sc.close();
}
}
3.for循环
package daigua2;
/*
* for循环的使用
* */
public class For1 {
public static void main(String[] args) {
int sum = 0;
for(int i=1; i<10 ; i++) {
System.out.println(i);
sum += i;
}
System.out.println(sum);
}
}
4.while循环
package daigua2;
public class While1 {
public static void main(String[] args) {
int sum = 0;
int i = 1;
while(i<=100) {
sum += i++;
}
System.out.println(sum);
}
}
5.do while循环
package daigua2;
public class DoWhile {
public static void main(String[] args) {
int sum = 0;
int i = 1;
do {
sum += i++;
} while (i <= 100);
System.out.println(sum);
}
}
6.循环控制break 和 continue
package daigua2;
/*
* break 跳出当前循环
* continue 跳出本次循环
* */
public class Break_Continue {
public static void main(String[] args) {
for(int i=0; i<10; i++) {
if(i==5) {
break;
}
System.out.print(i + " ");
}
System.out.println();
for(int i=0; i<10; i++) {
if(i==5) {
continue;
}
System.out.print(i + " ");
}
}
}