码迷,mamicode.com
首页 > 编程语言 > 详细

Java语言程序设计基础篇 方法(五)

时间:2014-05-08 03:19:35      阅读:435      评论:0      收藏:0      [点我收藏+]

标签:java

生成随机字符

生成随机字符就是生成0到65535之间的一个随机整数,因为0<=Math.random()<1.0,必须在65535+1

(int) (Math.random() * (65535+1))

随机生成小写字母

public class RandomCharacter {
    public static char getRandomCharacter(char ch1,char ch2){
        return (char)(ch1 +Math.random() * (ch2 - ch1 + 1));
    }
    public static char getRandomLowerCaseLetter(){
        return getRandomCharacter(‘a‘,‘z‘);
    }  
}

测试程序

public class TestRandomCharacter {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        for (int i = 0; i < 10; i++) {
            char ch = RandomCharacter.getRandomLowerCaseLetter();
            System.out.println(ch);
        }
    }
}

*输入年份和月份,显示该月日历

import java.util.Scanner;
public class C0501 {
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.print("请输入年份:");
        int year=input.nextInt();
        System.out.print("请输入月份:");
        int month=input.nextInt();
        int monthDays=31;
        if(month%2==0&&month!=8)
            monthDays=30;
        if(month==2)
            monthDays=28;
        if(leapYear(year)==1&&month==2)
            monthDays=29;
        int countLeapYear=0;
        for(int i=1800;i<year;i++){
            if(leapYear(i)==1)
                countLeapYear++;
        }
        int dateCount=(year-1800)*365+countLeapYear;
        String ms1="Jan";
        switch(month){
        case 2:dateCount+=31;ms1="Feb";break;
        case 3:dateCount+=59;ms1="Mar";break;
        case 4:dateCount+=90;ms1="Apr";break;
        case 5:dateCount+=120;ms1="May";break;
        case 6:dateCount+=151;ms1="June";break;
        case 7:dateCount+=181;ms1="July";break;
        case 8:dateCount+=212;ms1="Aug";break;
        case 9:dateCount+=243;ms1="Sep";break;
        case 10:dateCount+=273;ms1="Oct";break;
        case 11:dateCount+=304;ms1="Nov";break;
        case 12:dateCount+=334;ms1="Dec";break;
        default:break;
        }
        if(month>1&&(leapYear(year)==1))
            dateCount++;
        int n=(dateCount%7+3)%7;
        int pcount=0;
        System.out.printf("             %s  %d             \n",ms1,year);
        System.out.println("-----------------------------------");
        System.out.println("  Sun  Mon  Tue  Wed  Thu  Fri  Sat");
        for(int i=1;i<=n;i++){
            System.out.print("     ");
            pcount++;
        }
        for(int i=0;i<monthDays;i++){
            if(pcount%7==0)
                System.out.printf("\n");
            System.out.printf("%5d",i+1);
            pcount++;
        }      
    }
    public static int leapYear(int n){
        if(n%4==0&&(n%100!=0))
            return 1;
        else if(n%400==0)
            return 1;
        else
            return 0;
    }
}

显示前100个回文素数

public class C05T26 {
    public static void main(String[] args){
        int counter=0;
        int i=2;
        while(counter<100){
            if(judgeHuiwen(i)&&judgePrime(i)){
                counter++;
                System.out.printf("%8d",i);
                if(counter%10==0)
                    System.out.printf("\n");
            }
            i++;
        }
    }
    public static boolean judgeHuiwen(int n){
        int k=n;
        int rNum=0,remain;
        while(k!=0){
            remain=k%10;
            rNum=rNum*10+remain;           
            k=k/10;        
        }
        if(rNum==n)
            return true;
        else
            return false;
    }
    public static boolean judgePrime(int n){
        int i;
        if(n==0||n==1)
            return false;
        for(i=2;i<=n;i++){
            if(n%i==0)
                break;
        }
        if(i==n)
            return true;
        else
            return false;          
    }
}


Java语言程序设计基础篇 方法(五),布布扣,bubuko.com

Java语言程序设计基础篇 方法(五)

标签:java

原文地址:http://coolview.blog.51cto.com/7138551/1408104

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!