Day037

1.题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。

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);
              }
       }
}

2.题目:输入两个正整数mn,求其最大公约数和最小公倍数。

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);
    }
}

3.输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

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)+"个");
        }
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值