编写一个程序,实现从命令行参数输入两个字符串类型的数值,并计算输出两个数值的和
public class Test_1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String a = input.nextLine();
String b = input.nextLine();
int i = Integer.parseInt(a);
int o = Integer.parseInt(b);
int q = i+o;
System.out.println("两数的和为:"+q);
input.close();
}
}
编写一个程序,实现从命令行参数输入一字符串,统计该字符串中字符“e”出现的次数。(识点:String中常用的方法)
public class Test_2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入一段字符串:");
String s = input.nextLine();
int o = 0;
for(int i = 0;i<s.length();i++) {
if(s.charAt(i)=='e') {
o++;
}
}
System.out.println("e出现的次数为:"+o);
input.close();
}
}
解析一个邮箱地址是否合法,如果合法则打印出用户名部分和该邮箱所属的网站域名,如果邮箱地址不合法则显示不合法的原因 [选做题]
2.1 提示:邮箱地址不合法的因素:
2.1.1邮箱地址中不包含@或.
2.1.2邮箱地址中含有多了@或.
2.1.3邮箱地址中.出现在@的前面
2.1.4用户名里有其他字符
2.2实现步骤:
2.2.1创建一个类,类名:mailtest
public class Test_4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String email = input.nextLine();
if (email.indexOf('@') >= 0 && email.indexOf('.') >= 0) {
if (email.indexOf('@') == email.lastIndexOf('@') && email.indexOf('.') == email.lastIndexOf('.')) {
if (email.indexOf('@') > email.indexOf('.')) {
System.out.println("此邮箱合法!");
} else {
System.out.println("邮箱@不在.前");
}
} else {
System.out.println("邮箱含有多个@或.请修改!");
}
} else {
System.out.println("邮箱不含有@或.");
}
input.close();
}
}
分别在控制台输入字符串和子字符串,并计算字符串中子字符串出现的次数
public class Test_5 {
public static void main(String[] args) {
String a = "sddnwsdjkfnsddnrenojwnsddninrngoipsddn";
String b = "sddn";
int num = 0;
for(int i = 0;i<=a.length()-b.length();i++) {
if(b.equals(a.substring(i, b.length()+i))) {
num++;
}
}
System.out.println("字符串中子字符串出现的次数:"+num);
}
}
有一种数叫回文数,正读和反读都一样,如12321便是一个回文数。编写一个程序,从命令行得到一个整数,判断该数是不是回文数
public static void main(String[] args) {
String a = "1023105013201";
boolean b = true;
for(int i = 0;i<a.length()/2;i++) {
if(a.charAt(i) != a.charAt(a.length()-i-1)) {
b = false;
}
}
if(b) {
System.out.println("该数为回文数");
}else {
System.out.println("该数不是回文数");
}
}