Java编程题集3

题目1

Java编程题集3_第1张图片

import java.util.*;
class Demo04_04{
    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");
        }
    }
    public static boolean isValid(String password){//判断是否满足三个方面,是返回true
        return isRight1(password)&&isRight2(password)&&isRight3(password);
    }
    public static boolean isRight1(String password){//判断是否字符串数目大于8
        return password.length()>=8;
    }
    public static boolean isRight2(String password){
        for(int i=0;i='0'&&c<='9';
    }
    public static boolean isLetter(char c){//判断是否为字母
        return (c>='a'&&c<='z')||(c>='A'&&c<='Z');
    }
    public static boolean isRight3(String password){//判断数字是否大于二
        int digitCount=0;
        for(int i=0;i=2;
    }
}

题目2

Java编程题集3_第2张图片

class Demo04_06{
    public static void main(String[] args){
        int count=0;    //表示回文素数的个数
        int num=2;      //表示可能的回文素数的数字 从2开始~?
        while(true){
            //如果num是 回文素数
            if(isHuiWenAndSuShu(num)){
                count++;
                System.out.print(num+" ");
                if(count%10==0){
                    System.out.println();
                }
            }
            if(count==100){
                break;
            }
            num++;
        }
    }
    public static boolean isHuiWenAndSuShu(int num){//判断是否满足条件
        return isHuiWen(num)&&isSuShu(num);
    }
    public static boolean isHuiWen(int num){//判断是否为回文
        return reverse(num)==num;
    }
    public static int reverse(int num){//将数字的顺序颠倒
        int sum=0;
        while(true){
            sum=sum*10+num%10;
            num/=10;
            if(num==0){
                return sum;
            }
        }
    }
    public static boolean isSuShu(int num){//判断是否为素数
        for(int i=2;i<=num/2;i++){
            if(num%i==0){
                return false;
            }
        }
        return true;
    }
}

题目3

Java编程题集3_第3张图片

import java.util.Scanner;
 class Demo04_08{
      public static void main(String[] args){
            Scanner scanner=new Scanner(System.in);
            System.out.print("输入字符串:");
            String str=scanner.nextLine(); 
            System.out.print("输入查找元素:");
            String ch=scanner.next();
            int num=count(str,ch); 
            System.out.println(ch+"出现次数为"+num);
        } 
        public static int count(String str,String ch){ 
            int num=0;
            while(str.indexOf(ch)!=-1){ 
                str=str.substring(str.indexOf(ch)+1);
                System.out.println(str.indexOf(ch)); 
                num++;
            } 
            return num; 
        }
 }

题目4

import java.util.Scanner;
class Demo04_09{
	public static void main(String[] args){
		Scanner scanner =new Scanner(System.in);
		System.out.print("请输入字符串");
		String a=scanner.nextLine();
		String b="";
		char c[]=a.toCharArray();
		for(int i=c.length-1;i>=0;i--){
			b=b+c[i];
		}
		System.out.print(b);
	}
}

 

你可能感兴趣的:(Java编程题集3)