ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java流程控制

2022-03-30 16:02:07  阅读:145  来源: 互联网

标签:控制 java Scanner 流程 System String println public out


java流程控制

1.Scanner对象

java.util.Scanner,通过Scanner类来获取用户的输入

基本语法:Scanner s = new Scanner(System.in);

通过Scanner类的next(),nextLine()方法获取输入的字符串,读取前先用hasNexr(),hasNextLine()判断

(1).next()

  1. 遇到空格结束,会忽略有效字符前的空格
  2. 一定要获取字符
package Scanner;
import java.util.Scanner;
public class Demo01 {
    public static void main(String[] args) {

        // 创建一个扫描器对象,用于接受键盘数据
        Scanner scanner = new Scanner(System.in);

        System.out.println("使用next方法接收:");
        // 判断用户有没有输入字符串
        if (scanner.hasNext()){
            // 使用next方法接收:
            String str = scanner.next();
            System.out.println("输出的内容为:"+str);
        }
        // 凡是属于io流的的类如果不关闭会一直占用资源,要用完就关
        scanner.close();
    }
}

(2).nextLine()

  1. 一Enter为结束符
  2. 可以获得空白
package Scanner;
import java.util.Scanner;
public class Demo02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入:");
        if (scanner.hasNextLine()){
           String str = scanner.nextLine();
            System.out.println("输出内容:"+str);
        }
        scanner.close();
    }
}

2.Scanner进阶使用方法

(1).数据类型

package Scanner;
import java.util.Scanner;
public class Demo04 {
    public static void main(String[] args) {
        int i = 0;
        float f = 0.0f;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个整数:");
        if(scanner.hasNextInt()){
            i = scanner.nextInt();
            System.out.println("整数数据:"+i);
        }else {
            System.out.println("你输入的不是整数!");
        }
        System.out.println("请输入一个小数:");
        if(scanner.hasNextFloat()){
            f = scanner.nextFloat();
            System.out.println("小数数据:"+f);
        }else {
            System.out.println("你输入的不是小数!");
        }

        scanner.close();
    }
}

2.顺序结构

从上到下,一句一句来

package struct;

public class ShunXuDemo {
    public static void main(String[] args) {
        System.out.println("hello1");
        System.out.println("hello2");
        System.out.println("hello3");
        System.out.println("hello4");
        System.out.println("hello5");
    }
}

3.选择结构

(1).if单选择结构

if(boolean){

​ // 如果Boolean==true执行的语句。

}

package struct;
import java.util.Scanner;
public class IfDemo01 {
    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();
    }
}

(3).if双选择结构

if(boolean){

​ // 如果Boolean==true执行

}else {

​ // 如果Boolean==false执行

}

package struct;

import java.util.Scanner;

public class IfDemo02 {
    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("不及格");
        }

    }
}

(3).if多选择语句

if(){

}else if(){

}else if(){

}else {

}

package struct;

import java.util.Scanner;

public class IfDemo03 {
    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(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){
            System.out.println("不及格");
        }else {
            System.out.println("你输入的是啥?!");
        }
    }
}

(4).嵌套if语句

(5).switch多选择结构

switch和case合用

switch(x){

​ case value:

​ // 语句

​ break;//可选

​ default:

​ // 语句

}

支持变量类型:byte,short,int,char

从Javase7开始,支持String类型

case标签必须为字符串常量或字面量

package struct;

import java.util.Scanner;

public class SwtchDemo01 {
    public static void main(String[] args) {
        // case穿透
        // switch 匹配一个具体的值
        Scanner scanner = new Scanner(System.in);
        String grade = scanner.next();
        // 反编译 (idea)
        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;
            case "E":
                System.out.println("挂科");
                break;
            default:
                System.out.println("你是什么等级?!");
        }
    }
}

循环结构

java5引入了增强型for循环

(1).while循环

最基本的循环(莫要写死循环)

while(Boolean表达式){

​ // 循环内容

}

package struct;

public class WhileDemo01 {
    public static void main(String[] args) {
        // 输出1~100
        int i = 0;
        while (i<100){
            i++;
            System.out.println(i);
        }
    }
}
package struct;

public class WhileDemo02 {
    public static void main(String[] args) {
        while (true){
            // 等待客户端响应
            // 定时检查
            // ......
        }
    }
}
package struct;

public class WhileDemo03 {
    public static void main(String[] args) {
        // 1加到100
        int sum = 0;
        int i = 1;
        while(i<=100){
            sum += i;
            i++;
        }
        System.out.println(sum);
    }
}

(3).do...while循环

do{

​ // 代码语句

}while(Boolean表达式);

do...while至少保证循环体运行一次

package struct;

public class DoWhileDemo01 {
    public static void main(String[] args) {
        int i = 1;
        int sum = 0;
        // 先判断再执行
        do {
            sum += i;
            i++;
        }while (i <= 100);
    }
}

package struct;

public class DoWhileDemo02 {
    public static void main(String[] args) {
        int a = 0;
        while (a<0){
            System.out.println(a);
            a++;
        }
        System.out.println("--------------");
        do {
            System.out.println(a);
            a++;
        }while (a < 0);
    }
}

(4).for循环

支持迭代的一种通用结构,最有效,最灵活的结构

for循环的执行次数在执行前确定的

for(初始化;布尔表达式;更新){

​ // 代码语句;

}

实例:

  1. 1-100奇数,偶数和
package struct;

public class ForDemo02 {
    public static void main(String[] args) {
        // 计算0-100之间奇数和偶数的和
        int a = 0;
        int b = 0;
        for (int i = 0; i <= 100; i++) {
            if(i % 2 == 0){
                a+=i;
            }else {
                b+=i;
            }
        }
        System.out.println("奇数和为:"+b);
        System.out.println("偶数和为:"+a);
    }
}
  1. 1-1000之间能被5整除的数
package struct;

public class ForDemo03 {
    public static void main(String[] args) {
        // 输出1-1000之间能被5整除的数,一行三个
        for (int i = 1; i <= 1000; i++) {
            if(i % 5 == 0){
                System.out.print(i+"\t");
            }
            if(i % (5*3) == 0){
                System.out.print("\n");
            }
        }
        // println 输出会换行
        // print 输出不会换行
    }
}

  1. 九九乘法表
package struct;

public class FoeDemo04 {
    public static void main(String[] args) {
        // 先打印第一列
        // 循环嵌套
        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();
        }

    }
}

(5).增强型的for循环

java5新增的用于数组和集合的

for(声明类型 :表达式){

​ // 代码句子

}

类似python中的for循环(遍历)

package struct;

public class ForDemo05 {
    public static void main(String[] args) {
        int[] numbers = {10,20,30,40,50};
        for(int i = 0;i<numbers.length;i++){
            System.out.println(numbers[i]);
        }
        // 遍历数组的元素
        for(int x : numbers){
            System.out.println(x);
        }
    }
}


break,continue

(1).break

在循环任何主体部分均可使用,强制退出循环,不执行循环剩余的语句。

package struct;

public class BreakDemo {
    public static void main(String[] args) {
        int i = 0;
        while (i<100){
            i++;
            System.out.println(i);
            if(i==30){
                break;
            }
        }
        System.out.println("123");
    }
}


(2).continue

终止某次循环,进行下一次循环的判定

package struct;

public class ContinueDemo {
    public static void main(String[] args) {
        for (int i = 1; i <= 100; i++) {
            if(i%10==0){
                System.out.println();
                continue;
                // 执行下一次循环的判断
            }
            System.out.print(i);
        }
    }
}

(3).goto关键字(无条件限制转移符)

java中保留了goto关键字,但是没有使用,但是break和continue有goto的影子--加标签的break和continue(例如:label:)

package struct;

public class LabelDemo {
    // 打印101-150之间的质数
    public static void main(String[] args) {
        int count = 0;
        // 不建议使用
        outer:for(int i = 101;i<150;i++){
            for(int j = 2;j<i/2;j++){
                if (i%j==0){
                    continue outer;
                }
            }
            System.out.println(i+" ");
        }
    }
}

实例:

打印三角形:

package struct;

public class TestDemo01 {

    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("*");
            }
            for(int j = 5;j>=i;j--){    // 右空白
                System.out.print(" ");
            }
            System.out.println();
        }
    }
}

标签:控制,java,Scanner,流程,System,String,println,public,out
来源: https://www.cnblogs.com/taohaojie/p/16077484.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有