流程控制
1.分支結構的概念
- 當需要進行條件判斷并做出選擇時,使用分支結構
2.if分支結構
格式:
if(條件表達式){
語句塊;
}
package com.lagou.Day04;
import java.util.Scanner;
/**
* 程式設計使用if分支結構模拟網吧上網的過程
*/
public class Demo01 {
public static void main(String[] args) {
//1.提示使用者輸入年齡資訊并使用變量記錄
System.out.println("請輸入您的年齡:");
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
//2.使用if分支結構判斷是否成年并給出對應的提示
if (age>=18){
//3.列印一句話
System.out.println("開心的浏覽起了網頁...");
}
System.out.println("美好的時光總是短暫的!");
}
}
3.if分支結構找最大值的方式一
package com.lagou.Day04;
import java.util.Scanner;
/**
* 程式設計使用if分之結構查找兩個整數中的最大值
*/
public class Demo02 {
public static void main(String[] args) {
//1.提示使用者輸入兩個整數并使用變量記錄
System.out.println("請輸入兩個整數");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
//2.使用if分支結構找到最大值并列印
if (a>=b){
System.out.println("最大值"+a);
}
if (a<b){
System.out.println("最大值"+b);
}
}
}
4.if分支結構查找最大值的方式二
package com.lagou.Day04;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
//1.提示使用者輸入兩個整數并使用變量記錄
System.out.println("請輸入兩個整數");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
//方式二
int max = a;
if (b>max){
max=b;
}
System.out.println("最大值是:"+max);
}
}
5.ifelse分支結構的概念和使用
package com.lagou.Day04;
import java.util.Scanner;
/**
* 程式設計使用ifelse分支結構來模拟考試成績查詢的過程
*/
public class Demo04 {
public static void main(String[] args) {
//1.提示使用者輸入考試成績并使用變量記錄
System.out.println("請輸入您的考試成績:");
Scanner sc = new Scanner(System.in);
int score = sc.nextInt();
//2.使用if else分支結構判斷考試成績是否及格并給出對應的提示
if (score >= 60){
System.out.println("恭喜你考試通過了!");
}else {
System.out.println("下學期來補考吧!");
}
}
}
6.ifelse分支結構判斷負數和非負數
- 提示使用者輸入一個整數,使用if else分支結構判斷該整數是負數還是非負數并列印。
package com.lagou.Day04;
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
System.out.println("請輸入一個整數");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if (num<0){
System.out.println(num+"是負數");
}else {
System.out.println(num+"是非負數");
}
}
}
- 使用if else分支結構判斷該整數是正數、負數還是零
package com.lagou.Day04;
import java.util.Scanner;
public class Demo06 {
public static void main(String[] args) {
System.out.println("請輸入一個整數");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if (num<0){
System.out.println(num+"是負數");
}else {
if (num>0){
System.out.println(num+"是正數");
}else {
System.out.println(num+"是零");
}
}
}
}
7.if else if else分支結構的概念和使用
結構
if(條件表達式1){
語句塊1;
}else if(條件表達式2){
語句塊2;
}else{
語句塊n;
}
package com.lagou.Day04;
import java.util.Scanner;
public class Demo07 {
public static void main(String[] args) {
System.out.println("請輸入身份資訊");
Scanner sc = new Scanner(System.in);
String str = sc.next();
if ("軍人".equals(str)){
System.out.println("免費乘車");
}else if ("學生".equals(str)){
System.out.println("請購買半價票");
}else {
System.out.println("請購買全價票");
}
}
}
8.個人所得稅的計算方式一
package com.lagou.Day04;
import java.util.Scanner;
public class Demo08 {
public static void main(String[] args) {
System.out.println("請輸入個人薪水");
Scanner sc = new Scanner(System.in);
int salary = sc.nextInt();
double salaryPrice = 0.0;
if (salary<=5000){
System.out.println("無需納稅");
}else if (salary<=8000){
salaryPrice = (salary-5000)*0.03;
}else if (salary <= 17000){
salaryPrice = (salary-8000)*0.1+(8000-5000)*0.03;
}else if (salary <= 30000){
salaryPrice = (salary-17000)*0.2+(17000-8000)*0.1+(8000-5000)*0.03;
}
System.out.println(salaryPrice);
}
}
9.個人所得稅的計算方式二
package com.lagou.Day04;
import java.util.Scanner;
public class Demo09 {
public static void main(String[] args) {
System.out.println("請輸入你的薪水");
Scanner sc = new Scanner(System.in);
int salary = sc.nextInt();
double salaryPrice = 0.0;
if (salary<=5000){
System.out.println("無需納稅");
}else if (salary <= 8000){
salaryPrice = (salary-5000)*0.03 -0;
}else if (salary<=17000){
salaryPrice=(salary-5000)*0.1-210;
}else if (salary<=30000){
salaryPrice=(salary-5000)*0.2-1410;
}
System.out.println(salaryPrice);
}
}
10.if分支結構實作等級判斷
package com.lagou.Day04;
import java.util.Scanner;
public class Demo10 {
public static void main(String[] args) {
System.out.println("請輸入考試成績");
Scanner sc = new Scanner(System.in);
int score = sc.nextInt();
if (score >= 90 && score <= 100){
System.out.println("等級A");
}else if (score >= 80){
System.out.println("等級B");
}else if (score >= 70){
System.out.println("等級C");
}else if (score >= 60){
System.out.println("等級D");
}else {
System.out.println("等級E");
}
}
}
11.switch case分支結構概念
12.switch case代碼
package com.lagou.Day04;
import java.util.Scanner;
public class Demo11 {
public static void main(String[] args) {
System.out.println("請輸入你的成績");
Scanner sc = new Scanner(System.in);
int score = sc.nextInt();
switch (score / 10){
case 10:
System.out.println("等級A");
break;
case 9:
System.out.println("等級A");
break;
case 8:
System.out.println("等級B");
break;
case 7:
System.out.println("等級C");
break;
default:
System.out.println("等級D");
}
}
}
- switch()中支援的資料類型有:byte、short、char以及int類型,jdk1.5開始支援枚舉類型,從jdk1.7開始支援String類型
13.switch case分支結構實作字元界面
package com.lagou.Day04;
import java.util.Scanner;
/**
* 模拟菜單的效果
*/
public class Demo12 {
public static void main(String[] args) {
//1.繪制字元界面
System.out.println(" 歡迎來到lg教育 ");
System.out.println("-----------------------------");
System.out.print("[1]學員系統 ");
System.out.print("[2]管理者系統");
System.out.println("[0]退出系統");
System.out.println("------------------------------");
System.out.println("請選擇要進入的系統");
Scanner sc = new Scanner(System.in);
int choose = sc.nextInt();
switch (choose){
case 1:
System.out.println("正在進入學員系統");break;
case 2:
System.out.println("正在進入管理者系統");break;
case 0:
System.out.println("謝謝使用,下次再見!");
default:
System.out.println("輸入錯誤,請重新選擇!");
}
}
}
14.循環結構
- 在Java程式中若希望重複執行一段代碼時,就需要使用循環結構
- 任何複雜的程式邏輯都可以通過順序、分支、循環三種程式結構實作。
15.for循環
- for(初始化表達式;條件表達式;修改初始值表達式){
- 循環體;
- }
package com.lagou.Day04;
public class Demo13 {
public static void main(String[] args) {
for (int i = 1;i<=10;i++){
System.out.println("大吉大利,今晚吃雞"+"第"+i+"場");
}
}
}
16.for列印奇數
package com.lagou.Day04;
/**
* 列印奇數 方式一
*/
public class Demo14 {
public static void main(String[] args) {
for (int i = 1;i<=100;i++){
if ((i%2)!=0)
System.out.println(i);
}
}
}
package com.lagou.Day04;
/**
* 方式二
*/
public class Demo15 {
public static void main(String[] args) {
for (int i = 1;i<=100;i+=2){
System.out.println(i);
}
}
}
package com.lagou.Day04;
/**
* 方式三
*/
public class Demo16 {
public static void main(String[] args) {
for (int i = 1;i<=50;i++){
System.out.println(2*i-1);
}
}
}
17.for循環實作累加和
package com.lagou.Day04;
/**
* 程式設計使用for循環實作1-10000之間所有整數的累加和
*/
public class Demo17 {
public static void main(String[] args) {
int sum = 0;
for (int i = 0; i<=10000;i++){
sum += i;
}
System.out.println(sum);
}
}
18.for循環實作水仙花數的列印
package com.lagou.Day04;
/**
* 使用for循環列印三位數中所有水仙花數
* 所謂"水仙花數"即一個整數滿足其值等于各個數位的立方和
* 如 153 是:1^3+5^3+3^3
*/
public class Demo18 {
public static void main(String[] args) {
for (int i = 0;i<1000;i++){
int bai = i/10/10%10;
int shi = i/10%10;
int ge = i%10;
if ((bai*bai*bai)+(shi*shi*shi)+(ge*ge*ge)==i){
System.out.println(i);
}
}
}
}
19.continue關鍵字
- continue語句用在循環體中,用于結束本次循環而開始下一次循環。
- 提前結束本次循環,執行下一次循環;循環繼續
package com.lagou.Day04;
/**
* 遇到5的倍數不列印
*/
public class Demo19 {
public static void main(String[] args) {
for (int i = 1;i<=20;i++){
if (0==i%5){
continue;
}else {
System.out.println(i);
}
}
}
}
20.break關鍵字
- break用于退出目前語句塊,break用在循環體中用于退出循環。
- 把整個循環都跳過了;不走了
- for(;;)這種沒有循環條件的循環叫做 無限循環,俗稱死循環
package com.lagou.Day04;
import java.util.Scanner;
/**
* 不斷地提示使用者輸入聊天内容并輸出,直到使用者輸入bye結束聊天
*/
public class Demo20 {
public static void main(String[] args) {
for (;;){
System.out.println("請輸入聊天内容");
Scanner sc = new Scanner(System.in);
String str = sc.next();
if ("bye".equals(str)){
System.out.println("聊天結束!");
break;
}
System.out.println("聊天内容:"+str);
}
}
}
21.猜數字
package com.lagou.Day04;
import java.util.Random;
import java.util.Scanner;
/**
* 猜數字
* 随機生成數字n(1-100),等待使用者輸入猜測資料,根據使用者的輸入比較輸出猜大了、猜小了、猜對了
* 如果使用者猜對了就結束遊戲。
*/
public class Demo21 {
public static void main(String[] args) {
Random r = new Random();
int temp =r.nextInt(100)+1;
for (;;){
System.out.println("請輸入1-100");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if (num>temp){
System.out.println("猜大了");
}else if (num < temp){
System.out.println("猜小了");
}else {
System.out.println("猜對了");
break;
}
}
}
}
22.雙重for循環的格式
package com.lagou.Day04;
/**
* 列印五行無列 厲害了我的哥
*/
public class Demo22 {
public static void main(String[] args) {
for (int i = 1;i<=5;i++){
for (int j = 1;j<=5;j++){
System.out.print("厲害了我的哥!");
}
System.out.println();
}
}
}
23.雙重for循環的執行流程和特點
- 外層循環用于控制列印的行數,内層循環用于控制列印的列數,外層循環改一下,内層循環從頭到尾跑一圈。
- 列印多行多列,用雙重循環。
24.雙重循環列印星星圖
package com.lagou.Day04;
/**
* 列印星星
*/
public class Demo23 {
public static void main(String[] args) {
//5行5列
for (int i = 1;i<=5;i++){
for (int j = 1;j<=5;j++){
System.out.print("*");
}
System.out.println();
}
//
for (int i = 1;i<=5;i++){
for (int j = 1;j<=i;j++){
System.out.print("*");
}
System.out.println();
}
for (int i = 1;i<=5;i++){
for (int j=1;j<=6-i;j++){
System.out.print("*");
}
System.out.println();
}
}
}
25.九九乘法表
package com.lagou.Day04;
/**
* 九九乘法表
*/
public class Demo24 {
public static void main(String[] args) {
for (int i = 1;i<=9;i++){
for (int j=1;j<=i;j++){
System.out.print(j+" * "+i+" = "+j*i +" ");
}
System.out.println();
}
}
}
- 在嵌套的循環中,break用于退出所在的循環體
- 如果要退出外層循環體,需要使用标号的方式
package com.lagou.Day04;
public class Demo25 {
public static void main(String[] args) {
outer:for (int i = 1;i<=9;i++){
for (int j=1;j<=i;j++){
System.out.print(j+" * "+i+" = "+j*i +" ");
if (6 == j){
break outer;
}
}
System.out.println();
}
}
}
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36
26.素數和質數
package com.lagou.Day04;
/**
* 素數:當一個數隻能被1和它本身整除
*/
public class Demo26 {
public static void main(String[] args) {
for (int i = 2;i<=100;i++){
boolean flag = true;
for (int j = 2;j<i;j++){
if (0 == i % j){
flag = false;
break;
}
}
if (flag){
System.out.println(i);
}
}
}
}
27.while循環
while(條件表達式){
循環體;
}
package com.lagou.Day04;
/**
* while
*/
public class Demo27 {
public static void main(String[] args) {
int i = 1;
while (i <= 10){
System.out.println(i);
i++;
}
}
}
28.while和for的比較
- while和for循環完全可以互換,當然推薦使用for循環
- while循環更适合于明确循環條件但不明确循環次數的場合中
- for循環更适合于明确循環次數或者範圍的場合中
- while(true)等價于for(;;)都表示無限循環
29.while反向輸出
- 提示使用者輸入一個任意位數的正整數然後反響輸出
package com.lagou.Day04;
import java.util.Scanner;
public class Demo28 {
public static void main(String[] args) {
System.out.println("請輸入一個任意整數");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
while (num>0){
System.out.print(num % 10);
num /=10;
}
}
}
30.do while循環
do{
循環體;
}while(條件表達式);
package com.lagou.Day04;
public class Demo29 {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
}while (i<=10);
}
}
- do while至少執行一次循環體
31.dowhile循環模拟任務檢查
package com.lagou.Day04;
import java.util.Scanner;
/**
* 使用dowhile循環來模拟學習任務是否合格的檢查,如果合格則停止,
* 否則就重新完成學習任務
*/
public class Deme30 {
public static void main(String[] args) throws InterruptedException {
String msg = null;
do {
System.out.println("正在瘋狂學習中");
Thread.sleep(5000);
System.out.println("是否合格?(Y/N)");
Scanner sc = new Scanner(System.in);
msg = sc.next();
}while (!"Y".equals(msg));
System.out.println("恭喜你任務合格!");
}
}
32.筆試
package com.lagou.Day04;
public class Demo31 {
public static void main(String[] args) {
//下面代碼有沒有問題?
/**
* 分号
*/
int i = 1;
while (i<100);{
System.out.println("i love you!");
i++;
}
}
}