Java使用条件语句和循环结构确定控制流(2)

import java.util.Arrays;
public class SuZu3{
    public static void main(String[] args) {
        int [] x={2,-1,7,777,6,764,85896,65554,0,874785,417825,74};   
        sort(x,'n');
        for(int i=0;i<x.length;i++){
            System.out.print(x[i]+"\t");
            }
        }
    //给数组进行选择性排序
    //调用API进行升序
    static void sort(int[]x,char Flag){
        if('A'==Flag){
            Arrays.sort(x);
            }
        else {
            for(int i=0;i<x.length-1;i++){
                for(int j=0;j<x.length-1-i;j++){
                    int temp=0;
                    if(x[j]<x[j+1]){
                        temp=x[j];
                        x[j]=x[j+1];
                        x[j+1]=temp;
                        }
                    }
                }
            }
        }
    }

import java.util.Scanner;

public class Suzu4 {
    public static void main(String[] args) {
        System.out.println("请输入");//这个命令只能紧贴在Scanner scan = new Scanner(System.in);的上面或下面才有效。
        Scanner scan = new Scanner(System.in);
        //System.out.println("请输入");或者放在Scanner scan = new Scanner(System.in);的下面
        String str = scan.nextLine();// nextLine才是接收一行
       
        char[] s = str.toCharArray();// 把字符串转换称一个字符数组
        scan.close();
        int letterCount = 0;
        int numberCount = 0;
        int spaceCount = 0;
        int otherCount = 0;
        for (int i = 0; i < s.length; i++) {
            if (s[i] >= 'a' && s[i] <= 'z' || s[i] >= 'A' && s[i] <= 'Z') {
                letterCount++;
            } else if (s[i] >= '1' && s[i] <= '9') {
                numberCount++;
            } else if (s[i] == ' ') {
                spaceCount++;
            } else {
                otherCount++;
            }
        }
        System.out.println("字母有" + letterCount + "个");
        System.out.println("数字有" + numberCount + "个");
        System.out.println("空格有" + spaceCount + "个");
        System.out.println("其他有" + otherCount + "个");
    }
}//ctrl+shift+f 是代码格式化
//ctrl+shift+o  是导包

五、多重选择:switch语句

格式类似如下:

switch(choice)

case 1:

........

break;

case 2:

.......

break;

.........

//可以再来几个case(用break结束一下)

default:

.......

break;

注意:

case标签可以是:

* 类型为char、byte、short或int的常量表达式。

* 枚举常量

* 从Java SE 7开始,case标签还可以是字符串字面量。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/427be50d33a2d38a3ffb401f8c9a3dfe.html