用户注册
package www.hpe.com.test;
import java.util.Scanner;
public class UserRegist {
public static void main(String[] args) {
UserRegist userRegist = new UserRegist();
Scanner sc = new Scanner(System.in);
String userName, pwd1, pwd2;
boolean flag = false;
do{
System.out.println("请输入用户名:");
userName=sc.next();
System.out.println("请输入密码:");
pwd1=sc.next();
System.out.println("请再次确认密码:");
pwd2=sc.next();
flag=userRegist.verify(userName,pwd1,pwd2);}
while(!flag);
}
private boolean verify(String userName, String pwd1, String pwd2) {
// TODO Auto-generated method stub
boolean flag=false;
if (userName.length()<3||pwd1.length()<6) {
System.out.println("账户长度不能小于3位,密码长度不能小于6位!");
}
else if (!pwd1.equals(pwd2)) {
System.out.println("两次输入密码不一致!");
}
else
{
System.out.println("注册成功,请牢记用户名和密码");
flag=true;
}
return flag;
}
}
判断字符串个数:
package www.hpe.com.test;
import java.util.Scanner;
public class CountString {
public static void main(String[] args) {
CountString cs = new CountString();
String isGo = cs.init();
if (isGo.equals("y")) {
init();
}
}
private static String init() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一连串字符串:");
String str = sc.nextLine();
System.out.println("请输入你要查找的字符:");
String ch = sc.next();
int num = find(str, ch);
System.out.println("出现了" + num + "个" + ch);
System.out.println("是否需要继续:y/n");
String isGo = sc.next();
return isGo;
}
private static int find(String str, String ch) {
// TODO Auto-generated method stub
int count = 0;
boolean flag = false;
String[] arr = new String[str.length()];
for (int i = 0; i < arr.length; i++) {
arr[i] = str.substring(i, i + 1);
}
for (int i = 0; i < arr.length; i++) {
while (arr[i].equals(ch)) {
count++;
flag = true;
if (flag) {
break;
}
}
}
if (!flag) {
System.out.println("字符串里面不含该字符!");
}
return count;
}
}
计算周数:
package www.hpe.com.test;
import java.util.Calendar;
public class FindWeak {
public static void main(String[] args) {
Calendar ca = Calendar.getInstance();
ca.set(Calendar.MONTH, 7);
ca.set(Calendar.YEAR, 2015);
ca.set(Calendar.DAY_OF_MONTH, 25);
int week = ca.get(Calendar.WEEK_OF_YEAR);
System.out.println("2015年7月25日是一年当中的第"+week+"周");
}
}