• Java流程控制


    1.用户交互Scanner

    Scanner对象

    之前我们学的基本语法中我们并没有实现程序和人的交互,但是Java给我们提供了这样一个工具类,我们可以获取用户的输入。java.util.Scanner 是Java5的新特性,我们可以通过Scanner类来获取用户的输入。

    基本语法:

    Scanner s = new Scanner(System.in);

     next()和nextLine()方法

    通过Scanner类next()和nextLine()方法获取输入的字符串,在读取前我们一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据。

    1. package org.psmaxwell.scanner;
    2. import java.util.Scanner;
    3. public class Demo03 {
    4. public static void main(String[] args) {
    5. // receive the data from keyboard
    6. Scanner scanner = new Scanner(System.in);
    7. System.out.println("Please input the data: ");
    8. String str = scanner.nextLine();
    9. System.out.println("output's content: "+str);
    10. scanner.close();
    11. }
    12. }
    1. package org.psmaxwell.scanner;
    2. import java.util.Scanner;
    3. public class Demo02 {
    4. public static void main(String[] args) {
    5. // receive the data from keyboard
    6. Scanner scanner = new Scanner(System.in);
    7. System.out.println("use the nextLine way to receive: ");
    8. // IF there is existing input value
    9. if(scanner.hasNextLine()){
    10. String str = scanner.nextLine(); // wait user input value
    11. System.out.println("Output's content: " +str);
    12. }
    13. scanner.close();
    14. }
    15. }

    2.Scanner进阶使用

    1. package org.psmaxwell.scanner;
    2. import java.util.Scanner;
    3. public class Demo05 {
    4. public static void main(String[] args) {
    5. //我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果。
    6. Scanner scanner = new Scanner(System.in); // ctrl+ alt +v 补全代码
    7. // summary
    8. double sum = 0;
    9. //计算输入了多少个数字
    10. int m = 0;
    11. // 通过循环判断是否还有输入,并在里面对每一次进行求和和统计
    12. while (scanner.hasNextDouble()){
    13. double x = scanner.nextDouble();
    14. m = m + 1; // m++
    15. sum = sum + x;
    16. System.out.println("你输入了第"+m+"个数据,然后当前结果sum="+sum);
    17. }
    18. System.out.println(m + "个数的和为" + sum);
    19. System.out.println(m + "个数的平均值是" + (sum / m));
    20. scanner.close();
    21. }
    22. }
    1. package org.psmaxwell.scanner;
    2. import java.util.Scanner;
    3. public class Demo04 {
    4. public static void main(String[] args) {
    5. Scanner scanner = new Scanner(System.in);
    6. //input the data from keyboard
    7. int i = 0;
    8. float f = 0.0f;
    9. System.out.println("请输入整数: ");
    10. if(scanner.hasNextInt()){
    11. i = scanner.nextInt(); //补全代码 CTRL + alt + V
    12. System.out.println("整数数据 :" + i);
    13. }else{
    14. System.out.println("输入的不是整数数据!");
    15. }
    16. System.out.println("请输入小数: ");
    17. if(scanner.hasNextFloat()){
    18. f = scanner.nextFloat(); //补全代码 CTRL + alt + V
    19. System.out.println("小数数据 :" + f);
    20. }else{
    21. System.out.println("输入的不是小数数据!");
    22. }
    23. scanner.close();
    24. }
    25. }
    1. package org.psmaxwell.scanner;
    2. import java.util.Scanner;
    3. public class Demo03 {
    4. public static void main(String[] args) {
    5. // receive the data from keyboard
    6. Scanner scanner = new Scanner(System.in);
    7. System.out.println("Please input the data: ");
    8. String str = scanner.nextLine();
    9. System.out.println("output's content: "+str);
    10. scanner.close();
    11. }
    12. }

    3.顺序结构

    • JAVA的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行。
    • 顺序结构最简单的算法结构。
    • 语句与语句之间,框与框之间是按从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,它是任何一个算法都离不开的一种基本算法结构。
    1. package org.psmaxwell.structure;
    2. public class SequenceDemo01 {
    3. public static void main(String[] args) {
    4. System.out.println("hello1");
    5. System.out.println("hello2");
    6. System.out.println("hello3");
    7. System.out.println("hello4");
    8. System.out.println("hello5");
    9. }
    10. }

    4.if选择结构

    if单项选择结构

    if双项选择结构

    if多选择结构

     

    嵌套的if结构

    5.switch多选择结构

    1. package org.psmaxwell.structure;
    2. public class SwitchDemo01 {
    3. public static void main(String[] args) {
    4. // case 穿透 // switch 匹配一个具体的值
    5. char grade = 'C';
    6. switch (grade){
    7. case 'A':
    8. System.out.println("Excellent");
    9. break; //可选
    10. case 'B':
    11. System.out.println("Good");
    12. break; //可选
    13. case 'C':
    14. System.out.println("Not good");
    15. break; //可选
    16. case 'D':
    17. System.out.println("Just so so");
    18. break; //可选
    19. case 'E':
    20. System.out.println("Failed");
    21. break; //可选
    22. default:
    23. System.out.println("Not sure");
    24. }
    25. }
    26. }
    1. package org.psmaxwell.structure;
    2. public class SwitchDemo02 {
    3. public static void main(String[] args) {
    4. String name = "Maxwell";
    5. // JDK7 的新特性,表达式结果可以是字符串!!!
    6. // 字符的本质还是数字
    7. // 反编译 java----class(字节码文件)----反编译(IDEA)
    8. switch (name){
    9. case "Mountain":
    10. System.out.println("Mountain");
    11. break;
    12. case "Maxwell":
    13. System.out.println("Maxwell");
    14. break;
    15. default:
    16. System.out.println("What happen?");
    17. }
    18. }
    19. }

    6.While循环详解

     

    1. package org.psmaxwell.structure;
    2. public class WhileDemo03 {
    3. public static void main(String[] args) {
    4. // calculate 1+2+3+...+100=?
    5. int i = 0;
    6. int sum = 0;
    7. while (i<=100){
    8. sum=sum+i;
    9. i++;
    10. }
    11. System.out.println(sum);
    12. }
    13. }

     7.DoWhile循环

    1. package org.psmaxwell.structure;
    2. public class DoWhileDemo01 {
    3. public static void main(String[] args) {
    4. int i = 0;
    5. int sum = 0;
    6. do {
    7. sum = sum + i;
    8. i++;
    9. }while (i<=100);
    10. System.out.println(sum);
    11. }
    12. }

    8. while 和do...while的区别:

    1. package org.psmaxwell.structure;
    2. public class DoWhileDemo02 {
    3. public static void main(String[] args) {
    4. int a = 0;
    5. while (a<0){
    6. System.out.println(a);
    7. a++;
    8. }
    9. System.out.println("=================================");
    10. do{
    11. System.out.println(a);
    12. a++;
    13. }while (a<0);
    14. }
    15. }

     9.FOR 循环

    1. package org.psmaxwell.structure;
    2. public class ForDemo01 {
    3. public static void main(String[] args) {
    4. int a = 1; //初始化条件
    5. while(a<=100){//条件判断
    6. System.out.println(a); //循环体
    7. a+=2; //迭代
    8. }
    9. System.out.println("while循环结束!");
    10. //初始化//条件判断//迭代
    11. for (int i = 1; i < 100; i++) {
    12. System.out.println(i);
    13. }
    14. System.out.println("for循环结束!");
    15. }
    16. }

     

    在Java5中引入了一种主要用于数组的增强型for循环

    1. package org.psmaxwell.structure;
    2. public class ForDemo02 {
    3. public static void main(String[] args) {
    4. // 计算0到100之间的奇数和偶数的和
    5. int oddSum = 0;
    6. int evenSum = 0;
    7. for (int i = 0; i <= 100; i++) {
    8. if(i%2!=0){ //奇数
    9. oddSum+=i; // oddSum = oddSum + i;
    10. }else{ //偶数
    11. evenSum+=i;
    12. }
    13. }
    14. System.out.println("奇数的和: " +oddSum);
    15. System.out.println("偶数的和: " +evenSum);
    16. }
    17. }
    1. package org.psmaxwell.structure;
    2. public class ForDemo03 {
    3. public static void main(String[] args) {
    4. for (int i = 0; i <= 1000; i++) {
    5. if(i%5==0){
    6. System.out.println(i+"\t");
    7. }
    8. if(i%(5*3)==0){ //每行
    9. System.out.println();
    10. //System.out.print("\n");
    11. }
    12. }
    13. //println 输出完会换行
    14. //print 输出完不会换行
    15. }
    16. }

    10.打印九九乘法表

    1. package org.psmaxwell.structure;
    2. public class ForDemo4 {
    3. public static void main(String[] args) {
    4. //1.打印第一列,
    5. //2.将固定的1再用一个循环抱起来
    6. //3.去掉重复项目i <=j
    7. //4.调整样式
    8. for (int j = 1; j <= 9; j++) {
    9. for (int i = 1; i <= j; i++) {
    10. System.out.print(j+"*"+i+"="+(j*i) + "\t");
    11. }
    12. System.out.println();
    13. }
    14. }
    15. }

    11. 增强for循环

    1. package org.psmaxwell.structure;
    2. public class ForDemo05 {
    3. public static void main(String[] args) {
    4. int[] numbers = {10,20,30,40,50}; //定义一个数组
    5. for (int i = 0; i < 5; i++) {
    6. System.out.println(numbers[i]);
    7. }
    8. System.out.println("=================================");
    9. //遍历数组的元素
    10. for(int x:numbers){
    11. System.out.println(x);
    12. }
    13. }
    14. }

    12.break、continue、goto

     

    1. //Break
    2. package org.psmaxwell.structure;
    3. public class BreakDemo {
    4. public static void main(String[] args) {
    5. int i = 0;
    6. while(i<100){
    7. i++;
    8. System.out.println(i);
    9. if(i==30){
    10. break;
    11. }
    12. }
    13. System.out.println("123");
    14. }
    15. }
    1. //continue
    2. package org.psmaxwell.structure;
    3. public class ContinueDemo {
    4. public static void main(String[] args) {
    5. int i = 0;
    6. while (i<100){
    7. i++;
    8. if (i%10==0){
    9. System.out.println();
    10. continue;
    11. }
    12. System.out.print(i);
    13. }
    14. //break 在任何循环语句的主体部分,均可用break控制循环的流程。
    15. //break 用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)
    16. //
    17. // continue 语句用在循环体语句中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
    18. }
    19. }
    1. goto
    2. package org.psmaxwell.structure;
    3. public class LabelDemo {
    4. public static void main(String[] args) {
    5. //打印101-150之间的所有质数
    6. int count = 0;
    7. //不建议使用
    8. outer:for (int i = 101; i < 150; i++) {
    9. for (int j = 2; j < i/2; j++) {
    10. if (i % j ==0){
    11. continue outer;
    12. }
    13. }
    14. System.out.print(i+" ");
    15. }
    16. }
    17. }

    13.打印三角形及Debug

    1. package org.psmaxwell.structure;
    2. public class TestDemo {
    3. public static void main(String[] args) {
    4. //打印三角形
    5. for (int i = 1; i <= 5; i++) {
    6. for (int j = 5; j >= i; j--) {
    7. System.out.print(" ");
    8. }
    9. for (int j = 1; j <=i; j++) {
    10. System.out.print("*");
    11. }
    12. for (int j = 1; j < i; j++) {
    13. System.out.print("*");
    14. }
    15. System.out.println();
    16. }
    17. }
    18. }
  • 相关阅读:
    理解React页面渲染原理,如何优化React性能?
    雅思写作拾遗01----图表类作文
    算法进阶指南图论 通信线路
    java毕业设计宠物咖啡馆平台系统mybatis+源码+调试部署+系统+数据库+lw
    [附源码]java毕业设计基于web旅游网站的设计与实现
    CMake中while/continue/break的使用
    typeScript--[函数定义]
    IOS企业IPA软件证书 苹果签名证书 有效期到2026年
    数据结构入门 — 队列
    线性代数应用基础补充1
  • 原文地址:https://blog.csdn.net/u011868279/article/details/126289124