JavaSE高级应用编程——工具类练习题

1、 编写一个程序,实现从命令行参数输入两个字符串类型的数值,并计算输出两个数值的和。 [必做题]

//编写一个程序,实现从命令行参数输入两个字符串类型的数值,并计算输出两个数值的和。 [必做题]
import java.util.Scanner;
public class MySum {
    public static void main(String[] args) {
        System.out.println("请输入两个字符串类型的数值……");
        Scanner in = new Scanner(System.in);
        String str1 = in.next();
        String str2 = in.next();
        MySum(str1,str2);
    }
    public static void MySum(String str1,String str2){
        int x = Integer.parseInt(str1);
        int y = Integer.parseInt(str2);
        int sum = x+y;
        System.out.println(x+"与"+y+"两个数值的和为"+sum);
    }
}

这里写图片描述
• 2、编写一个程序,实现从命令行参数输入一字符串,统计该字符串中字符“e”出现的次数。(识点:String中常用的方法) [必做题]

//编写一个程序,实现从命令行参数输入一字符串,统计该字符串中字符“e”出现的次数。(识点:String中常用的方法) [必做题]
import java.util.Scanner;

public class eCount {
    public static void main(String[] args) {
        System.out.println("请输入一个字符串");
        Scanner in = new Scanner(System.in);
        String str = in.next();
        MyeCount(str);
    }

    public static void MyeCount(String str){
        String str2 = "";
        int sum = 0;

        for(int i = 0;i<str.length();i++){
            if(str.charAt(i) == 'e'){
                sum++;
            }
        }
        System.out.println("字符串\""+str+"\"中有"+sum+"个e字符");
    }
}

这里写图片描述
• 3、生成十个0~100之间的随机数,放到数组中,然后排序输出。(知识点:Math类取整,获得随机数等) [必做题]

import java.util.Arrays;

//生成十个0~100之间的随机数,放到数组中,然后排序输出。(知识点:Math类取整,获得随机数等) [必做题]
//注*对于数组排序有冒泡排序和数组排序方法两种,任选其一即可
public class MyScort {
    public static void main(String[] args) {
        MyScort();
    }

    public static void MyScort(){

//      定义一个长度为10的数组用来存储随机数
        int[] arr = new int[10];

//      依次为数组存入是个随机数
        for(int i = 0;i<10;i++){
            arr[i] =  (int) Math.round(Math.random()*100);
        }

//      方法一—……冒泡排序,对数组的元素进行排序
        for(int j = 1;j<=arr.length;j++){
            for(int i = 0;i<arr.length-j;i++){
                int m = 0;
                if(arr[i]>arr[i+1]){
                    m = arr[i];
                    arr[i] = arr[i+1];
                    arr[i+1] = m;
                }
            }           
        }

//      方法二……数组方法排序
        Arrays.sort(arr);


//      遍历输出数组
        for(int i = 0;i<arr.length;i++){
            System.out.print(arr[i]+" ");
        }
    }
}

这里写图片描述
• 4、巴黎时间比北京时间晚7个小时,纽约时间比北京时间晚12个小时,试编写一程序,根据输入的北京时间输出相应的巴黎和纽约时间。[选做题]

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class MyTime {
    public static void main(String[] args) throws ParseException{
        MyTime();
    }
    public static void MyTime() throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cd = Calendar.getInstance();  
        Scanner in = new Scanner(System.in);    
        System.out.println("请依次输入北京时间的年份、月份、日期、时间数、分钟数,秒数用空格隔开……");
        String str1 = in.nextLine();
        String[] arr = str1.split(" ");
        if(arr.length != 6){
            System.out.println("你输入的数据个数不符合题意");
        }else{
            String str = arr[0]+"-"+arr[1]+"-"+arr[2]+" "+arr[3]+":"+arr[4]+":"+arr[5];

            Date date = sdf.parse(str);

             System.out.println("北京时间为:" + sdf.format(date.getTime()));
             System.out.println("巴黎时间为:" + sdf.format(date.getTime() - (long)7* 60 * 60 * 1000));
             System.out.println("纽约时间为:" + sdf.format(date.getTime() - (long)12* 60 * 60 * 1000));
        }
    }
}

这里写图片描述
• 5、解析一个邮箱地址是否合法,如果合法则打印出用户名部分和该邮箱所属的网站域名,如果邮箱地址不合法则显示不合法的原因 [选做题]
• 5.1 提示:邮箱地址不合法的因素:
• 5.1.1邮箱地址中不包含@或.
• 5.1.2邮箱地址中含有多了@或.
• 5.1.3邮箱地址中.出现在@的前面
• 5.1.4用户名里有其他字符
• 5.2实现步骤:
• 5.2.1创建一个类,类名:mailtest
• 类图如下:这里写图片描述

import java.util.Scanner;

//5、解析一个邮箱地址是否合法,如果合法则打印出用户名部分和该邮箱所属的网站域名,如果邮箱地址不合法则显示不合法的原因 [选做题]
//• 5.1 提示:邮箱地址不合法的因素:
//• 5.1.1邮箱地址中不包含@或.
//• 5.1.2邮箱地址中含有多了@或.
//• 5.1.3邮箱地址中.出现在@的前面
//• 5.1.4用户名里有其他字符
//• 5.2实现步骤:
//• 5.2.1创建一个类,类名:mailtest
public class MyEmil {
    public static void main(String[] args) {
        System.out.println("请输入一个邮箱地址……");
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();

        if(TestMail(str) == true){

        }else{
            System.out.println("不合法");
        }

    }
    public static boolean TestMail(String str){
        boolean b = true;
        if(str.indexOf('@') == -1 || str.indexOf('.') == -1){
            System.out.println("邮箱地址中不包含@或.");
            return  false;
        }
        if(str.indexOf('@') != str.lastIndexOf('@') ||str.indexOf('.') != str.lastIndexOf('.')){
            System.out.println("邮箱地址中含有多了@或.");
            return false;
        }
        if(str.lastIndexOf('@') > str.indexOf('.')){
            System.out.println("邮箱地址中.出现在@的前面");
            return false;
        }
            for(int i = 0;i<str.indexOf('@');i++){
                char c = str.charAt(i);
                if( (c>=4&&c<=57) || (c>=65&&c<=90) || (c>=97&&c<=122) ){

                }else{
                    System.out.println("邮箱用户名包含非法字符");
                    return false;
                }
            }

            String[] array = str.split("@");
            System.out.println("用户名为:"+array[0]);
            System.out.println("该邮箱所属的网站域名为"+array[1]);

            return true;    
    }
}

这里写图片描述
• 6、分别在控制台输入字符串和子字符串,并计算字符串中子字符串出现的次数。 [选做题]


import java.util.Scanner;

//分别在控制台输入字符串和子字符串,并计算字符串中子字符串出现的次数。 [选做题]
public class MyCount {
    public static void main(String[] args) {
        MyCount();
    }
    public static void MyCount(){
        System.out.println("请输入一个字符串");
        Scanner in = new Scanner(System.in);
        String str = in.next();
        System.out.println("请输入需要统计的子字符串");
        String str1 = in.next();

        String str2 = "";
        int sum = 0;

        for(int i = 0;i<str.length();i++){
            if(str.charAt(i) == str1.charAt(0)){
                for(int j = i;j<str1.length();j++){
                    str2 = str2 + str.charAt(j);
                }
                sum++;
                i=i+str2.length()-1;
            }
        }
        System.out.println("字符串\""+str+"\"中有"+sum+"个\""+str1+"\"");
    }
}

这里写图片描述
• 7、有一个字符串,其中包含中文字符、英文字符和数字字符,请统计和打印出各个字符的个数。 [选做题]


import java.util.Scanner;

public class MyGroup {
    public static void main(String[] args) {
        MyGroup();
    }

    public static void MyGroup(){

        int sum1 = 0;
        int sum2 = 0;
        int sum3 = 0;

        System.out.println("请输入一个字符串");
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();

        for(int i = 0 ;i<str.length();i++){
            if(str.charAt(i) >=0x30 && str.charAt(i) <=0x39){
                sum1++;
            }
            if((str.charAt(i) >=0x61 && str.charAt(i) <=0x7a)||(str.charAt(i) >=0x41 && str.charAt(i) <=0x5a)){
                sum2++;
            }
            if(str.charAt(i)>0x0391 && str.charAt(i) <= 0xFFE5){
                sum3++;
            }
        }
        System.out.println("字符串\""+str+"\"中有\n"+sum1+"个数字字符,有"+sum2+"个英文字符"+sum3+"个中文字符");
    }

}

这里写图片描述
• 8、有一种数叫回文数,正读和反读都一样,如12321便是一个回文数。编写一个程序,从命令行得到一个整数,判断该数是不是回文数。 [选做题]


import java.util.Scanner;

public class MyNum {
    public static void main(String[] args) {
        System.out.println("请输入一个数……");
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();

        if(MyNum(num)==true){
            System.out.println(num+"是回文数");
        }else{
            System.out.println(num+"不是回文数");
        }
    }
    public static boolean MyNum(int num){

//      将数字转化成字符串
        String str = String.valueOf(num);
        boolean b = true;

//      从两端向中间遍历字符串,并进行回文数条件判断
        for(int i = 0,j = str.length()-1;i<str.length()/2 ;i++,j--){
            if(str.charAt(i) != str.charAt(j) ){
                b = false;
                break;
            }
        }
        return b;
    }
}

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_37067955/article/details/81903890
今日推荐