一、流程控制语句
结构化程序设计有三种基本结构:顺序结构、选择结构和循环结构。Java语言提供支持结构化程序设计的所有语句。
(1)if语句:格式 if(布尔表达式)
{
语句一 }
package work;
import java.util.Scanner;
public class Demo7 {
public static void main(String[] args) {
int k;
Scanner scanner=new Scanner(System.in);
k=scanner.nextInt();
if(k<=10) {
k++;
System.out.println(k);
}
}
}
分支语句
(1)if-else:格式:if(布尔表达式)
{语句一}
else
{语句二}
package work;
import java.util.Scanner;
public class Demo5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner=new Scanner(System.in);
System.out.println("请输入一个五位数:");
int x, a,b,c,d,e;
x=scanner.nextInt();
a=x/10000;
b=(x%10000)/1000;
c=(x%1000)/100;
d=(x%100)/10;
e=(x%10);
if(a==e&&b==d) {
System.out.println(x+"是对称数");
}
else {
System.out.println(x+"不是对称数");
}
}
}
(2)switch:
package work;
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("请输入数字:");
int x;
x=sc.nextInt();
switch(x) {
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期天");
break;
default:
System.out.println("输入错误,请重新输入");
}
}
}
循环语句
while:while语句循环体执行次数至少为0.
package work;
import java.util.Scanner;
public class Demo7 {
public static void main(String[] args) {
while(i<=10);
System.out.println(x);