day37

題目:利用條件運算符的嵌套來完成此題:學習成績>=90分的同學用A表示,60-89分之間的用B表示,60分以下的用C表示。

程序分析:(a>b)?a:b這是條件運算符的基本例子。

public class Prog5{

       public static void main(String[] args){

              int n = -1;

              try{

                     n = Integer.parseInt(args[0]);

              }catch(ArrayIndexOutOfBoundsException e){

                     System.out.println("請輸入成績");

                     return;

              }

              grade(n);

       }

       //成績等級計算

       private static void grade(int n){

              if(n>100 || n<0)

                System.out.println("輸入無效");

              else{

                String str = (n>=90)?"分,屬於A等":((n>60)?"分,屬於B等":"分,屬於C等");

                System.out.println(n+str);

              }

       }

}

 

題目:輸入兩個正整數m和n,求其最大公約數和最小公倍數。

程序分析:利用輾除法。

public class Prog6{

    public static void main(String[] args){

        int m,n;

        try{

            m = Integer.parseInt(args[0]);

            n = Integer.parseInt(args[1]);

        }catch(ArrayIndexOutOfBoundsException e){

            System.out.println("輸入有誤");

            return;

        }

        max_min(m,n);

    }

    //求最大公約數和最小公倍數

    private static void max_min(int m, int n){

        int temp = 1;

        int yshu = 1;

        int bshu = m*n;

        if(n<m){

            temp = n;

            n = m;

            m = temp;

        }

        while(m!=0){

            temp = n%m;

            n = m;

            m = temp;

        }

        yshu = n;

        bshu /= n;

        System.out.println(m+"和"+n+"的最大公約數爲"+yshu);

        System.out.println(m+"和"+n+"的最小公倍數爲"+bshu);

    }

}

 

題目:輸入一行字符,分別統計出其中英文字母、空格、數字和其它字符的個數。

程序分析:利用while語句,條件爲輸入的字符不爲'\n'.

import java.util.Scanner;

public class Prog7_1{

    public static void main(String[] args){

        System.out.print("請輸入一串字符:");

        Scanner scan = new Scanner(System.in);

        String str = scan.nextLine();//將一行字符轉化爲字符串

        scan.close();

        count(str);

    }

    //統計輸入的字符數

    private static void count(String str){

        String E1 = "[\u4e00-\u9fa5]";//漢字

        String E2 = "[a-zA-Z]";

        String E3 = "[0-9]";

        String E4 = "\\s";//空格

        int countChinese = 0;

        int countLetter = 0;

        int countNumber = 0;

        int countSpace = 0;

        int countOther = 0;

        char[] array_Char = str.toCharArray();//將字符串轉化爲字符數組

        String[] array_String = new String[array_Char.length];//漢字只能作爲字符串處理

        for(int i=0;i<array_Char.length;i++)

          array_String[i] = String.valueOf(array_Char[i]);

        //遍歷字符串數組中的元素

        for(String s:array_String){

            if(s.matches(E1))

              countChinese++;

            else if(s.matches(E2))

              countLetter++;

            else if(s.matches(E3))

              countNumber++;

            else if(s.matches(E4))

              countSpace++;

            else

              countOther++;

        }

        System.out.println("輸入的漢字個數:"+countChinese);

        System.out.println("輸入的字母個數:"+countLetter);

        System.out.println("輸入的數字個數:"+countNumber);

        System.out.println("輸入的空格個數:"+countSpace);

        System.out.println("輸入的其它字符個數:"+countSpace);

    }

}

import java.util.*;

public class Prog7_2{

    public static void main(String[] args){

      System.out.println("請輸入一行字符:");

      Scanner scan = new Scanner(System.in);

      String str = scan.nextLine();

      scan.close();

      count(str);

    }

    //統計輸入的字符

    private static void count(String str){

        List<String> list = new ArrayList<String>();

        char[] array_Char = str.toCharArray();

        for(char c:array_Char)

          list.add(String.valueOf(c));//將字符作爲字符串添加到list表中

        Collections.sort(list);//排序

        for(String s:list){

            int begin = list.indexOf(s);

            int end = list.lastIndexOf(s);

            //索引結束統計字符數

            if(list.get(end)==s)

              System.out.println("字符‘"+s+"’有"+(end-begin+1)+"個");

        }

    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章