match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
-
- Scanner scanner = new Scanner(System.in);
- String str = scanner.next();
- String emailMatcher="[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+";
-
- //write your code here......
- if(str.matches(emailMatcher)){
- System.out.print("邮箱格式合法");
- }else{
- System.out.print("邮箱格式不合法");
- }
-
- //或者
-
- //System.out.println(str.matches(emailMatcher)?"邮箱格式合法":"邮箱格式不合法");
-
- }
- }
- import java.util.*;
-
- public class Main {
- public static void main(String[] args) {
- //标准输入
- Scanner console = new Scanner(System.in);
- int m = console.nextInt();
- int n = console.nextInt();
- //计算最小公倍数
- int result = getCM(m, n);
- //输出结果
- System.out.println(result);
- }
-
- //计算最小公倍数
- public static int getCM(int m, int n){
- //计算m、n中较大者
- int max=Math.max(m,n);
- //从max到m*n之间找最小公倍数
- for(int i=max;i<=m*n;i++){
- //如果既能被m整除又能被n整除,说明是最小公倍数,直接返回
- if(i%m==0&&i%n==0){
- return i;
- }
- }
- return -1;
- }
-
- }
- import java.util.Scanner;
-
- public class Main {
-
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- //计数
- int cnt=0;
- //记录平均数
- double avg=0;
- //记录累加和
- double sum=0;
- while(scan.hasNext()){
- int num=scan.nextInt();
- //如果小于0,直接终止循环
- if(num<0){
- break;
- }
- //累加和加上对应num
- sum+=num;
- //计数加一
- cnt++;
- }
- //计算平均数
- avg=sum/cnt;
- System.out.println(String.format("%.2f",avg));
-
- }
- }
- import java.util.Arrays;
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- int[] arr = new int[6];
- int[] reverse=new int [6];
- Scanner scanner = new Scanner(System.in);
- for (int i = 0; i < arr.length; i++) {
- arr[i] = scanner.nextInt();
- }
- System.out.println(Arrays.toString(arr));
-
- //write your code here......
- for(int i =0;i
- {
- reverse[i]=arr[arr.length-i-1];
- }
-
- System.out.println(Arrays.toString(reverse));
- }
- }
5、二维数组求和
- public class Main {
- public static void main(String[] args) {
- int[][] arr = {{11,33,55},{22,44,66,88},{131,214,315,146},{928,827,726,625},{424,525}};
- int sum=add(arr);
- System.out.println(sum);
- }
-
- public static int add(int[][] arr) {
- int sum=0;
-
- for(int i=0;i
- for(int j=0;j
- sum =sum+arr[i][j];
- }
-
-
- return sum;
- }
- }
-
相关阅读:
centos7安装kubernets集群
JS常用事件,使用方法
第九章-线程
每天一点python——day75
AUTOSAR知识点 之 多核启动 (一):英飞凌单片机的多核启动详细解析
Nginx 配置 SSL(HTTPS)
随机性检测模块支持GM/T 0005-2021标准的升级建议
Nginx与Tomcat部署Vue前后端分离应用
Mac安装nacos超详细步骤|解决打不开http://127.0.0.1:8848/nacos
第一章 教育基础(04 教师专业发展)
-
原文地址:https://blog.csdn.net/qq_45047809/article/details/126073534