本文主要是介绍java流程控制(重点),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
java流程控制
目录
- java流程控制
- 结构有
- 选择结构
- if
- if else
- if ,else if,
- switch,break,default
- String
- 循环结构
- Break及Continue
- 练习及Debug
结构有
-
顺序结构
-
选择结构
-
循环结构
选择结构
if
![image-20210803030857316.png](/upload/202108/05/202108050106282072.png)
package struct;
import java.util.Scanner;
public class dome1 {
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();
}
}
if else
![image-20210803030857316.png](/upload/202108/05/202108050106287772.png)
package struct;
import java.util.Scanner;
public class dome2 {
public static void main(String[] args) {
//考试分数大于60及格,小于60不及格
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,
![image-20210802040050747.png](/images/baidian.png)
package struct;
import java.util.Scanner;
public class dome3 {
public static void main(String[] args) {
/**
* if 语句至少有一个else语句,else语句在所有else if 之后
* 一旦其中有一个else if 语句为true ,其他else if及else语句都将跳过执行
*/
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<90 && score>=80){
System.out.println("B");
}else if (score<80 && score>=70){
System.out.println("C");
}else if (score<70 && score>=60){
System.out.println("D");
}else if (score<60 && score>=0){
System.out.println("不及格");
}else {
System.out.println("成绩不合法");
}
scanner.close();
}
}
switch,break,default
![image-20210802040233188.png](/images/baidian.png)
package struct;
public class dome4 {
public static void main(String[] args) {
//case穿透 //switch匹配一个具体值
char grade = 'A';
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;
default:
System.out.println("未知");
}
}
}
String
![image-20210802034432411.png](/images/baidian.png)
循环结构
while
![image-20210803014632345.png](/images/baidian.png)
package struct;
public class dome6 {
public static void main(String[] args) {
//输出1~100
int i = 0;
while (i < 100) {
i++;
System.out.println(i);
}
}
}
package struct;
public class whlieDome {
public static void main(String[] args) {
//计算1+2+3...100
int i = 0;
int sum = 0;
while (i<=100){
sum = sum + i;
i++;
}
System.out.println("100以内和为;"+sum);//5050
}
}
do while
![image-20210803014713720.png](/images/baidian.png)
package struct;
public class dowhlie {
public static void main(String[] args) {
int i = 0;
int sum = 0;
do {
sum = sum + i;
i++;
} while (i <= 100);
System.out.println(sum);
}
}
for循环
![image-20210803015015383.png](/images/baidian.png)
1.基础语法
package struct;
public class fordome {
public static void main(String[] args) {
int a = 1;//初始化条件
while (a<=100){//条件
System.out.println(a);//循环体
a+=2;//迭代
}
System.out.println("while循环结束");
//初始化 //条件 //迭代
for (int i = 1; i <= 100; i = i+2) {
System.out.println(i);
}
System.out.println("for循环结束");
}
}
2.练习1
package struct;public class fordome1 { public static void main(String[] args) { 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);//2500 System.out.println("偶数和:"+evenSum);//2550 }}
3.练习2
package struct;public class fordome2 { public static void main(String[] args) { //for循环输出1-1000之间能被5整除的,并且每行输出3个 for (int i = 0; i <= 1000; i++) { if (i%5==0){ System.out.print(i+"\t"); } if (i%(5*3)==0){//每行3个 System.out.println(); //System.out.print("\n"); //println 输出完换行 //print 输出完不换行 } } }}
4.练习3
package struct;public class fordome3 { public static void main(String[] args) { //99乘法表 //打印第一列 //把固定的1再用循环包起来 //去掉重复值 i<=j //调整样式 for (int j = 1; j <= 9; j++) { for (int i = 1; i <= j; i++) { System.out.print(j+"*"+i+"="+(j*i) + "\t"); } System.out.println(); } }}
Break及Continue
1.基础
![image-20210803015825457.png](/images/baidian.png)
2.Break
package struct;public class BreakDome { public static void main(String[] args) { for (int i = 1; i < 100; i++) { System.out.println(i); if (i==30){ break; } } System.out.println("结束"); }}
3.Continue
package struct;public class ContinueDome { public static void main(String[] args) { for (int i = 0; i < 100; i++) { if (i % 10 == 0) { System.out.println(); continue; } System.out.print(i); } }}
练习及Debug
1.练习
package struct;public class Exercise { public static void main(String[] args) { //打印三角形 5 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(); } }} * *** ***** ******* *********
2.Debug
![image-20210803020336498.png](/images/baidian.png)
这篇关于java流程控制(重点)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!