1、编写一个程序,实现从命令行参数输入两个字符串类型的数值,并计算输出两个数值的和
public class work2 {
public static void main(String[] args) {
String s = args[0];
int n = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'e') {
n++;
}
}
System.out.println(n);
}
}
}
}
2、编写一个程序,实现从命令行参数输入一字符串,统计该字符串中字符“e”出现的次数。
public class work2 {
public static void main(String[] args) {
String s = args[0];
int n = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'e') {
n++;
}
}
System.out.println(n);
}
}
3、生成十个0~100之间的随机数,放到数组中,然后排序输出。
public class work3 {
public static void main(String[] args) {
int[] arr = new int[10];
Random r = new Random();
for (int i = 0; i < arr.length; i++) {
int j = r.nextInt(100);
arr[i] = j;
}
Arrays.sort(arr);
for (int a : arr) {
System.out.print(a + " ");
}
}
}
4.解析一个邮箱地址是否合法,如果合法则打印出用户名部分和该邮箱所属的网站域名,如果邮箱地址不合法则显示不合法的原因 [选做题]
2.1 提示:邮箱地址不合法的因素:
2.1.1邮箱地址中不包含@或.
2.1.2邮箱地址中含有多了@或.
2.1.3邮箱地址中.出现在@的前面
2.1.4用户名里有其他字符
2.2实现步骤:
2.2.1创建一个类,类名:mailtest
类图如下:
import java.util.Scanner;
public class mailtest {
public static void main(String[] args) {
System.out.print("请输入邮箱:");
System.out.println(textmail());
}
public static boolean textmail(){
Scanner sc = new Scanner(System.in);
String mail = sc.nextLine();
int i = 0;
int j = 0;
for(int n=0;n<mail.length();n++){
if(mail.charAt(n)=='@'){
i++;
}
if(mail.charAt(n)=='.'){
j++;
}
if(!(mail.charAt(n)=='.'||mail.charAt(n)<='9'
&&mail.charAt(n)>='0'||mail.charAt(n)<='z'
&&mail.charAt(n)>='a'||mail.charAt(n)<='Z'
&&mail.charAt(n)>='@')){
return false;
}
}
if(!(i==1&&j==1)){
return false;
}
if(mail.startsWith("@")||mail.startsWith(".")){
return false;
}
return true;
}
}