1编写一个程序,实现从命令行参数输入两 个字符串类型的数值,并计算输出两个数值的 和。 [必做题]
package test; import java.util.Scanner; public class test02 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int sum=0; String a=sc.next(); String b=sc.next(); int number1 =Integer.parseInt(a); int number =Integer.parseInt(b); sum=number1+number; System.out.println("和为"+sum); } }
2.、编写一个程序,实现从命令行参数输入一 字符串,统计该字符串中字符“e”出现的次数 。(识点:String中常用的方法) [必做题]
package test; import java.util.Scanner; public class test02 { public static void main(String[] args) { String s = "AAABBCCDD"; Scanner sc = new Scanner(System.in); char c = sc.next().charAt(0); int count = 0 ; char [] c1 = s.toCharArray();//把字符串变成字符数组, for(int i = 0; i < c1.length; i++) { if(c == c1[i]) { count++; } } System.out.println(count); } }
3.生成十个0~100之间的随机数,放到数组中 ,然后排序输出。(知识点:Math类取整,获 得随机数等) [必做题] 课后作业
package test; import java.util.Arrays; public class test02{ public static void num() { int[] a = new int[10]; for (int i = 0; i < a.length; i++) { a[i] = (int) (Math.random() * 101); } Arrays.sort(a); for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } public static void main(String[] args) { // TODO Auto-generated method stub num(); } }
4、巴黎时间比北京时间晚7个小时,纽约时间比 北京时间晚12个小时,试编写一程序,根据输入 的北京时间输出相应的巴黎和纽约时间。[选做题 ]
package test; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Scanner; public class test02{ public static void main(String[] args) throws ParseException { System.out.println("请输入北京时间"); Scanner sc = new Scanner(System.in); String s = sc.next(); // 注意格式 大小写 SimpleDateFormat si = new SimpleDateFormat("yyyy-MM-dd-HH"); Date parse = si.parse(s); Calendar c = Calendar.getInstance(); // 设置时间 c.setTime(parse); // 修改成巴黎的时间 c.add(c.HOUR_OF_DAY, -7); // 巴黎时间 double y1 = c.get(c.YEAR); double m1 = c.get(c.MONTH); double d1 = c.get(c.DAY_OF_MONTH); double h1 = c.get(c.HOUR_OF_DAY); // 修改成纽约的时间 c.add(c.HOUR_OF_DAY, -12); // 纽约时间 double y2 = c.get(c.YEAR); double m2 = c.get(c.MONTH); double d2 = c.get(c.DAY_OF_MONTH); double h2 = c.get(c.HOUR_OF_DAY); // 输出巴黎时间 System.out.println("巴黎时间是" + y1 + "-" + m1 + "-" + d1 + "-" + h1); // 输出纽约时间 System.out.println("纽约时间是" + y2 + "-" + m2 + "-" + d2 + "-" + h2); }
- • 5、解析一个邮箱地址String s="abc123qqq";
char []c=s.toCharArray();//把字符串变成字符数组,是否合法,如果合法则打印 出用户名部分和该邮箱所属的网站域名,如果邮 箱地址不合法则显示不合法的原因 [选做题]
- • 5.1 提示:邮箱地址不合法的因素:
- • 5.1.1邮箱地址中不包含@或.
-
package test;
import java.util.Scanner; public class test02 { public static boolean testMail() { System.out.println("请输入邮箱"); Scanner in = new Scanner(System.in); String s = in.next(); // indexOf方法 在字符串检索str 返回第一个出现的位置,如果找不到则返回-1 if (s.indexOf("@") == -1 || s.indexOf(".") == -1) { System.out.println("邮箱地址中不包含@或."); return false; } // lastIndexOf方法 在str字符串中多次出现时,将返回最后一个出现的位置 if (s.indexOf("@") != s.lastIndexOf("@") || s.indexOf(".") != s.lastIndexOf(".")) { System.out.println("邮箱地址中含有多了@或."); return false; } // @的位置比 。大, @的位置靠后 if (s.indexOf("@") > s.lastIndexOf(".")) { System.out.println("邮箱地址中.出现在@的前面"); return false; } // 遍历@位置前面的数据 for (int i = 0; i < s.indexOf("@"); i++) { // charAt() 方法用于返回指定索引处的字符 if ((s.charAt(i) >= 'a' && s.charAt(i) <= 'z') || (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') || (s.charAt(i) >= '0' && s.charAt(i) <= '9')) { } else { System.out.println("用户名里有其他字符"); return false; } } return true; } public static void main(String[] args) { // TODO Auto-generated method stub if (test02.testMail()) { System.out.println("邮箱格式合法"); } else { System.out.println("邮箱格式不合法"); } } }