一、单选题
1.如下程序输出( )
public class Test {
public static void main(String[] args) {
String s = "Java";
StringBuilder buffer = new StringBuilder(s);
change(s);
System.out.println(s);
}
private static void change(String s) {
s = s + " and HTML";
}
}
A.Java
B.Java and HTML
C.and HTML
D.什么都不显示
2.以下语句不能通过编译的是( )。
A.int[] a={1,2,3,4};
B.int b[];
C.int c[]=new int[3];
D.int d=new int[];
3.下列方法定义中,不正确的是( )。
A.public int x( ){ … }
B.public static int x( double y ){ … }
C.void x( double d ) { … }
D.public static x( double a ){ … }
4.请选择下面程序正确的输出结果( )
public class Main{
public static void main(String args[ ]){
String a = new String("A");
String b = new String("B");
mb_operate(a,b);
System.out.println(a + "." + b);
}
static void mb_operate(String x,String y){
x.concat(y);
y=x;
}
}
A.A.B
B.A.A
C.AB.AB
D.AB.B
5.能正确表示“当x的取值在[-58,-40]和[40,58]范围内为真,否则为假”的表达式是()。
A.(x>= -58) && (x<= -40) && (x>=40) &&(x<=58)
B.(x>= -58) || (x<= -40) || (x>=40) || (x<=58)
C.(x>= -58) && (x<= -40) || (x>=40) && (x<=58)
D.(x>= -58) | |(x<= -40) && (x>=40) || (x<=58)
二、编程题
检查密码 (15 分)
本题要求你帮助某网站的用户注册模块写一个密码合法性检查的小功能。该网站要求用户设置的密码必须由不少于6个字符组成,并且只能有英文字母、数字和小数点 .,还必须既有字母也有数字。
输入格式:
输入第一行给出一个正整数 N(≤ 100),随后 N 行,每行给出一个用户设置的密码,为不超过 80 个字符的非空字符串,以回车结束。
注意: 题目保证不存在只有小数点的输入。
输出格式:
对每个用户的密码,在一行中输出系统反馈信息,分以下5种:
如果密码合法,输出Your password is wan mei.;
如果密码太短,不论合法与否,都输出Your password is tai duan le.;
如果密码长度合法,但存在不合法字符,则输出Your password is tai luan le.;
如果密码长度合法,但只有字母没有数字,则输出Your password needs shu zi.;
如果密码长度合法,但只有数字没有字母,则输出Your password needs zi mu.。
输入样例:
5
123s
zheshi.wodepw
1234.5678
WanMei23333
pass*word.6
输出样例:
Your password is tai duan le.
Your password needs shu zi.
Your password needs zi mu.
Your password is wan mei.
Your password is tai luan le.
/**
* 本题要求你帮助某网站的用户注册模块写一个密码合法性检查的小功能。该网站要求用户设置的密码必须由不少于6个字符组成,并且只能有英文字母、数字和小数点 .,还必须既有字母也有数字
*/
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
for (int i = 0; i < n; i++) {
String password = sc.nextLine();
check(password);
}
}
public static boolean check(String password) {
int count = 0;
if (password.length() < 6) {
System.out.println("Your password is tai duan le.");
return false;
}
int flagAlpha = 0;
int flagDigit = 0;
for (int i = 0; i < password.length(); i++) {
if (password.charAt(i) >= 'A' && password.charAt(i) <= 'z' || password.charAt(i) >= '0' && password.charAt(i) <= '9' || password.charAt(i) == '.') {
if (password.charAt(i) >= 'A' && password.charAt(i) <= 'z') {
flagAlpha++;
} else if (password.charAt(i) >= '0' && password.charAt(i) <= '9') {
flagDigit++;
}
} else {
System.out.println("Your password is tai luan le.");
return false;
}
}
if (flagAlpha == 0) {
System.out.println("Your password needs zi mu.");
return false;
}
if (flagDigit == 0) {
System.out.println("Your password needs shu zi.");
return false;
}
System.out.println("Your password is wan mei.");
return true;
}
}