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

和。 [必做题]

package 给发个公告;

 

import java.util.Scanner;

import java.util.concurrent.atomic.AtomicInteger;

 

public class work {

 

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

String num1 ;

String num2 ;

System.out.println("请输入第一个字符串:");

num1=input.next();

System.out.println("请输入第二个字符串:");

num2=input.next();

 

 

System.out.println("和为:"+(Integer.parseInt(num1)+Integer.parseInt(num2)));

}

 

 

 

}

 

 

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

package 给发个公告;

 

 

import java.util.Scanner;

 

 

public class work {

public static void count(String str) {

 

int sum=0;

for(int i=0;i<str.length();i++) {

if(str.charAt(i)=='e') {

sum++;

}

}

System.out.println("字符串中e出现了"+sum+"");

 

}

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("请输入一个字符串:");

String s= input.next();

count(s);

}

 

 

 

}

 

 

 

 

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

package 给发个公告;

 

 

import java.util.Arrays;

import java.util.Scanner;

 

 

public class work {

public static void j() {

int []a = new int[10];

for(int i=0;i<10;i++) {

a[i]=(int) Math.round(Math.random()*100);

}

Arrays.sort(a);

for (int i : a) {

System.out.print(i+" ");

}

 

}

public static void main(String[] args) {

j();

}

 

}

 

 

 

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

package homework;

 

 

 

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

 

}

 

 

 

 

}

 

public static void main(String[] args) throws ParseException {

// TODO Auto-generated method stub

mytime();

}

 

}

 

 

5、解析一个邮箱地址是否合法,如果合法则打印出用户名部分和该邮箱所属的网站域名,如果邮箱地址不合法则显示不合法的原因 [选做题]

5.1 提示:邮箱地址不合法的因素:

5.1.1邮箱地址中不包含@.

5.1.2邮箱地址中含有多了@.

5.1.3邮箱地址中.出现在@的前面

5.1.4用户名里有其他字符

package homework;

 

import java.util.Scanner;

 

public class mytime {

 

public static boolean mail(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>=3&&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;

}

 

public static void main(String[] args)  {

// TODO Auto-generated method stub

System.out.println("请输入一个邮箱地址...");

Scanner in = new Scanner(System.in);

String str = in.nextLine();

if(mail(str)==true) {

 

}

else {

System.out.println("不合法");

}

}

 

}

 

 

posted on 2023-06-16 12:02  丝毫不慌  阅读(15)  评论(0编辑  收藏  举报