第十一次作业
1.编写一个程序,实现从命令行参数输入两个字符串类型的数值,并计算输出两个数值的和。[必做题]
package 作业10;
import java.util.Scanner;
public class text1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("输入两个整数:");
String n = sc.nextLine();
String n1 = sc.nextLine();
int s = Integer.parseInt(n);
int c = Integer.parseInt(n1);
int sum = s + c;
System.out.println(sum);
}
}

2.编写一个程序,实现从命令行参数输入一字符串,统计该字符串中字符“e”出现的次数。(识点:String中常用的方法)[必做题]
package 作业10;
import java.util.Scanner;
public class text2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
String s = in.next();
int num = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'e') {
num++;
}
}
System.out.println(num);
}
}

3.生成十个0~100之间的随机数,放到数组中,然后排序输出。(知识点:Math类取整,获得随机数等)[必做题]
package 作业10;
import java.util.Arrays;
public class text3 {
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 作业10;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class text4 {
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.解析一个邮箱地址是否合法,如果合法则打印出用户名部分和该邮箱所属的网站域名,如果邮箱地址不合法则显示不合法的原因[选做题]
•5.1提示:邮箱地址不合法的因素:
•5.1.1邮箱地址中不包含@或.
•5.1.2邮箱地址中含有多了@或.
•5.1.3邮箱地址中.出现在@的前面
•5.1.4用户名里有其他字符
•5.2实现步骤:
•5.2.1创建一个类,类名:mailtest
package 作业10;
import java.util.Scanner;
public class mailtest {
public static boolean testMail() {
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 (mailtest.testMail()) {
System.out.println("邮箱格式合法");
} else {
System.out.println("邮箱格式不合法");
}
}
}

6.分别在控制台输入字符串和子字符串,并计算字符串中子字符串出现的次数。[选做题]
package 作业10;
import java.util.Scanner;
public class text6 {
public static void main(String[] args) {
System.out.println("请输入字符串与子字符串");
Scanner in = new Scanner(System.in);
String s = in.next();
String a = in.next();
int num = 0;//i是循環的次數
for(int i = 0;i<s.length()-a.length();i=s.indexOf(a, i)+1){
if(s.indexOf(a, i)!=-1){
num++;
}
}
System.out.println(num);
}
}

7.有一个字符串,其中包含中文字符、英文字符和数字字符,请统计和打印出各个字符的个数。[选做题]
package 作业10;
import java.util.Scanner;
public class text7 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next();
int num = 0, eng = 0, china = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
num++;
} else if
((s.charAt(i) >= 'a' && s.charAt(i) <= 'z') || (s.charAt(i) >= 'A' &&
s.charAt(i) <= 'Z')) {
eng++;
} else if((19968 <= s.charAt(i) && s.charAt(i) < 40623)) {
china++;
}
}
System.out.println("数字是:" + num);
System.out.println("英文是:" + eng);
System.out.println("中文是:" + china);
}
}

8.有一种数叫回文数,正读和反读都一样,如12321便是一个回文数。编写一个程序,从命令行得到一个整数,判断该数是不是回文数。[选做题]
package 作业10;
import java.util.Scanner;
public class text8 {
public static boolean foo() {
Scanner in=new Scanner(System.in);
String s =in.next();
int j = s.length()-1;
for(int i=0;i<s.length()/2;i++) {
if(s.charAt(i)!=s.charAt(j)) {
return false;
}
j--;
}
return true;
}
public static void main(String[] args) {
if(foo()) {
System.out.println("是回数");
}else {
System.out.println("不是回数");
}
}
}


浙公网安备 33010602011771号