基础结构
Java中的基础结构就是顺序结构,也就是程序在执行的时候从上至下去执行,顺序结构最简单却容易被忽视的。
public static void main(String[] args) {
System.out.println("1");
System.out.println("2");
System.out.println("3");
System.out.println("4");
}
输出的是1234,而不是其它方面的内容
选择结构
if单选择结构
很多时候我们需要判断某个东西是否可行,使用if单选择结构,语法
if(表达式){
表达式为true时就是我们执行的方法
}
程序演示:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String i = scanner.nextLine();
//如果输入等于四执行括号内的语句
if(i.equals("4")){
System.out.println("是4没错了!!!");
}
System.out.println("啥也不是!!!");
scanner.close();
}
if双选择结构
两种选择方式,要么执行 if,要么执行else,语法参照:
if(布尔表达式){
表达式为true执行此处
}else{
表达式为false执行此处代码
}
程序演示:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String i = scanner.nextLine();
if(i.equals("4")){
System.out.println("是4没错了!!!");
}else {
System.out.println("啥也不是!!!");
}
scanner.close();
}
if多选择结构
多种 if else选择:
if(布尔表达式){
表达式为true执行此处
}else if(布尔表达式){
表达式为true执行此处
}else if(布尔表达式){
表达式为true执行此处
}else if(布尔表达式){
表达式为true执行此处
}else{
表达式为flase执行此处
}
程序演示:输入一个正确的成绩然后判断成绩的等级
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt();
if(score == 100){
System.out.println("满分");
}else if (90 <= score && 100 > score){
System.out.println("A");
}else if (80 <= score && 90 > score){
System.out.println("B");
}else if (70 <= score && 80 > score){
System.out.println("C");
}else if (60 <= score && 70 > score){
System.out.println("D");
}else if(60 > score){
System.out.println("不及格");
}else{
System.out.println("输入的成绩不合法~~~");
}
scanner.close();
}
嵌套的if结构
多个 if 进行嵌套判断
程序演示:程序自动生成一个随机数1-100,用户进行猜数字的游戏(敲一遍自行测试!!!!)
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//生成一个随机数
int i = new Random().nextInt(100) + 1;
//设置猜的次数
int count = 1;
while(count <= 100){
System.out.println("请输入1-100的整数:");
int num = scanner.nextInt();
if(i == num){
System.out.println("猜对了!!!这个数是:" + i);
break;
} else if(num > i){
System.out.println("猜大了");
} else{
System.out.println("猜小了");
}
if(count == 100){
System.out.println("游戏结束!");
}
count++;
}
switch选择结构
switch搭配case进行选择的判断,它也是属于一种多选的选择结构
switch(expression){
case value:
//语句
break;//可选
case value:
//语句
break;//可选
case value:
//语句
break;//可选
defalut://可选
}
- switch中的变量类型可以是byte,short,int, char
- 支持String类型
- case中的值为字符串常量或者是字面量
程序演示:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入等级:");
String score = scanner.nextLine();
switch(score){
case "A":
System.out.println("等级A");
break;
case "B":
System.out.println("等级B");
break;
case "C":
System.out.println("等级C");
break;
case "D":
System.out.println("等级D");
break;
default:
System.out.println("啥也不是!!!");
}
这里面用了个关键字break,这个关键字的作用就是当循环执行到某处时,符合条件就退出整个循环,还有一个continue关键字,与break不同的是,它不是退出整个循环,而是触发到相应的条件时,程序会从循环头开始执行。
循环结构
while循环
语法格式:
while(布尔表达式){
表达式为true时进行执行
}
- 只要表达式为true就会一直执行下去
- 大部分情况下,我们需要对循环进行结束
- 少部分情况下的循环需要一直进行下去,比如服务器的请求响应监听等
- while循环可能会导致死循环,会让程序崩溃
程序演示:输出1-100的整数和
public static void main(String[] args) {
int i = 0;
int sum = 0;
while(i<=100){
sum = sum + i;
i++;
}
System.out.println(sum);
}
do…while循环
语法格式:
do{
代码执行
}while(布尔表达式);
while与do…while的区别
- while先判断后执行,do…while是先执行后判断
- do…while中do中的方法体必须执行一次,这是他们的主要区别
程序演示:
int i = 0;
int sum = 0;
do{
i++;
}while (i<0);
System.out.println(i);
}
代码中布尔表达式为false,输出 i 的值为1.因为执行了一次,如果为while循环输出的 i 的值就是0了
for循环
所有的循环都可以用while和do…while语句进行表示,Java中也提出了另一种更简单表示循环的结构for循环,它可以使代码结构表现的更加简单:
语法结构:
for(变量初始化;布尔表达式;变量更新){
//代码语句
}
程序演示:计算出1-100之间的偶数和奇数的和
//计算0-100奇数和偶数的和
int sum1 = 0;
int sum2 = 0;
for (int i = 0; i <= 100 ; i++) {
if(i % 2 == 0){
sum1 += i;
}else {
sum2 += i;
}
System.out.println(i);
}
System.out.println(sum1);//偶数2550
System.out.println(sum2);//奇数2500
}
使用for循环输出1-1000中能够被5整除的数,并隔三个数进行换行
public static void main(String[] args) {
for (int i = 0; i <=1000 ; i++) {
if(i % 5 == 0){
System.out.print(i + "\t");//不换行
}
if(i % (5*3) == 0){
System.out.println();//每隔三个数换行
}
}
}
输出九九乘法表(使用for嵌套循环)
public static void main(String[] args) {
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(i + "*" + j + "=" + i*j + "\t");
}
System.out.println();
}
}
输出结果如下所示:
下面再做一个练习:打印三角形
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();
}
}
输出如下:
可能有点不理解,我们将上面的图片进行拆分就明白了,看下图,图片被分成了三个部分,分别表示为1, 2,3三个区域,这三个区域分别表示程序中的一,二,三,最后一部分是输入的四行,所以将=号去掉就可以了