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;
- }
- }
-
相关阅读:
CSS 滚动驱动动画 animation-range
西门子6ES72881ST200AA1
MySQL高可用
IT行业变成了夕阳行业
2022最令人惊艳的人工智能论文整理分享(附视频、代码)
JDK安装详细教程
[补题记录] Codeforces Round 904 (Div. 2)(C)
Linux Nacos2.2.0版本集群搭建,常见报错问题解决
压力测试:Jmeter自动化测试详解
Jvm(二)新生代和老年代与GC回收
-
原文地址:https://blog.csdn.net/qq_45047809/article/details/126073534