• java 小练习


    1、 正则表达式来验证邮箱格式是否合法:

    match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scanner = new Scanner(System.in);
    5. String str = scanner.next();
    6. String emailMatcher="[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+";
    7. //write your code here......
    8. if(str.matches(emailMatcher)){
    9. System.out.print("邮箱格式合法");
    10. }else{
    11. System.out.print("邮箱格式不合法");
    12. }
    13. //或者
    14. //System.out.println(str.matches(emailMatcher)?"邮箱格式合法":"邮箱格式不合法");
    15. }
    16. }

    2、找两个数的最小公倍数

    1. import java.util.*;
    2. public class Main {
    3. public static void main(String[] args) {
    4. //标准输入
    5. Scanner console = new Scanner(System.in);
    6. int m = console.nextInt();
    7. int n = console.nextInt();
    8. //计算最小公倍数
    9. int result = getCM(m, n);
    10. //输出结果
    11. System.out.println(result);
    12. }
    13. //计算最小公倍数
    14. public static int getCM(int m, int n){
    15. //计算m、n中较大者
    16. int max=Math.max(m,n);
    17. //从max到m*n之间找最小公倍数
    18. for(int i=max;i<=m*n;i++){
    19. //如果既能被m整除又能被n整除,说明是最小公倍数,直接返回
    20. if(i%m==0&&i%n==0){
    21. return i;
    22. }
    23. }
    24. return -1;
    25. }
    26. }

    3、键盘输入任意多个10000以内正整数(负数代表结束),求出它们的平均数,

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner scan = new Scanner(System.in);
    5. //计数
    6. int cnt=0;
    7. //记录平均数
    8. double avg=0;
    9. //记录累加和
    10. double sum=0;
    11. while(scan.hasNext()){
    12. int num=scan.nextInt();
    13. //如果小于0,直接终止循环
    14. if(num<0){
    15. break;
    16. }
    17. //累加和加上对应num
    18. sum+=num;
    19. //计数加一
    20. cnt++;
    21. }
    22. //计算平均数
    23. avg=sum/cnt;
    24. System.out.println(String.format("%.2f",avg));
    25. }
    26. }

    4、倒叙输出数组

    1. import java.util.Arrays;
    2. import java.util.Scanner;
    3. public class Main {
    4. public static void main(String[] args) {
    5. int[] arr = new int[6];
    6. int[] reverse=new int [6];
    7. Scanner scanner = new Scanner(System.in);
    8. for (int i = 0; i < arr.length; i++) {
    9. arr[i] = scanner.nextInt();
    10. }
    11. System.out.println(Arrays.toString(arr));
    12. //write your code here......
    13. for(int i =0;i
    14. {
    15. reverse[i]=arr[arr.length-i-1];
    16. }
    17. System.out.println(Arrays.toString(reverse));
    18. }
    19. }

    5、二维数组求和

    1. public class Main {
    2. public static void main(String[] args) {
    3. int[][] arr = {{11,33,55},{22,44,66,88},{131,214,315,146},{928,827,726,625},{424,525}};
    4. int sum=add(arr);
    5. System.out.println(sum);
    6. }
    7. public static int add(int[][] arr) {
    8. int sum=0;
    9. for(int i=0;i
    10. for(int j=0;j
    11. sum =sum+arr[i][j];
    12. }
    13. return sum;
    14. }
    15. }

  • 相关阅读:
    后缀表达式求值
    计算机网络中的应用层和传输层(http/tcp)
    C++基础——static成员
    Spring Data JPA使用自定义查询进行分页
    操作文档的用户故事怎么写,敏捷开发
    Cisco.Packet.Tracer思科模拟器浮动路由讲解(含实例步骤)
    Centos7.9 一键脚本部署 LibreNMS 网络监控系统
    怎么压缩成mp4视频?
    海外IP代理科普——API代理是什么?怎么用?
    gcc: error: : No such file or directory
  • 原文地址:https://blog.csdn.net/qq_45047809/article/details/126073534