1.
package day7_27;
import java.util.Scanner;
public class Zuo67 {
public static void main(String[] args) {
System.out.print("请输入一个整数:");
Scanner scanner = new Scanner(System.in);
long input = scanner.nextLong();
System.out.println(sumDigits(input));
scanner.close();
}
public static int sumDigits(long n) {
int i,b=0;
while(n!=0){
i = (int) n % 10;
n = n / 10;
b += i;
}
return b;
}
}
/*
请输入一个整数:234
9
*/
2.
package day7_27;
import java.util.Scanner;
public class Zuo68 {
public static void main(String[] args) {
System.out.print("请输入一个整数:");
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
if (isPalindrome(num)) {
System.out.println("是回文整数!");
}else {
System.out.println("不是回文数字!");
}
scanner.close();
}
public static int reverse(int number) {
int reverseNumber = 0;
do {
reverseNumber = reverseNumber * 10 + number % 10;
number /= 10;
}while(number > 0);
return reverseNumber;
}
public static boolean isPalindrome(int number) {
return (reverse(number) == number);
}
}
/*
请输入一个整数:122
不是回文数字!
*/
3.
package day7_27;
import java.util.Random;
import java.util.Scanner;
public class Zuo69 {
public static void main(String[] args) {
System.out.print("Enter n:");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
printMatrix(n);
scanner.close();
}
public static void printMatrix(int n) {
int arr[][] = new int[n][n];
Random random = new Random();
for (int i = 0; i <= (arr.length - 1); i++) {
for (int j = 0; j <= (arr[0].length - 1); j++) {
arr[i][j] = random.nextInt(2);
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
/*
Enter n:5
1 0 0 1 0
1 1 0 1 0
1 0 0 1 0
1 1 0 0 0
0 0 1 1 0
*/
4.(检测密码)一些网站对于密码具有一些规则。编写一个方法,检测字符串是否是一个有效密码。假定密码规则如下:
●密码必须至少8位字符。密码仅能包含字母和数字。密码必须包含至少两个数字。 编写一个程序,提示用户输入一个密码,如果符合规则,则显示Valid Password,否则显示Invalid Password。
package day7_27;
import java.util.Scanner;
public class Zuo70 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("请输入密码:");
String password=scanner.nextLine();
if(isValid(password)){
System.out.println("Valid Password");
}else{
System.out.println("Invalid Password");
}
scanner.close();
}
public static boolean isValid(String s){
return isLengthValid(s)&&isContentValid(s)&&isNumberValid(s);
}
public static boolean isLengthValid(String s){
return s.length()>=8;
}
public static boolean isContentValid(String s){
char c=' ';
for(int i=0;i<s.length();i++){
c=s.charAt(i);
if(!isLetter(c)&&!isDigit(c)){
return false; //如果有不是数字或字母的,则输出false
}
}
return true;
}
public static boolean isNumberValid(String s){ //第三个条件判断是否超过两个数字
int count=0;
char c=' ';
for(int i=0;i<s.length();i++){
c=s.charAt(i);
if(isDigit(c)){
count++;
}
}
return count>=2;
}
public static boolean isLetter(char c){
return c&