打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Java语言程序设计

*定义方法:

修饰符   返回值类型   方法名(参数列表){

//方法体;

}

对于:public static int max(int num1,int num2){

...

}这个方法,public static作为修饰符,为静态修饰符;int为说明该方法的返回值是一个int型数据,这个方法包含两个int型形式参数。

主程序main()也是一种方法,它是由Java虚拟机进行调用的。

*可以看出Java中的方法几乎就等同于C语言中的函数,并且与C中在参数传递上也都是:传递的只是实参的值,而非实参本身。

*重载方法:Java中一个类里面可以有多个同名方法,但其各自的参数表不同;编译器会根据情况选择最匹配的方法。但多个方法匹配度相同时候会产生编译错误,称为歧义调用。

*变量的作用域

是指变量可在程序中引用的范围。方法中的变量称为局部变量。局部变量作用域是从其声明开始,到包含该变量声明的块结束。局部变量在使用之前要先进行声明。基于此,可以在一个方法的不同代码块里声明同名的局部变量。

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

  1. import java.util.Scanner;  
  2. public class C0501 {  
  3.     public static void main(String[] args){  
  4.         Scanner input=new Scanner(System.in);  
  5.         System.out.print("请输入年份:");  
  6.         int year=input.nextInt();  
  7.         System.out.print("请输入月份:");  
  8.         int month=input.nextInt();  
  9.         int monthDays=31;  
  10.         if(month%2==0&&month!=8)  
  11.             monthDays=30;  
  12.         if(month==2)  
  13.             monthDays=28;  
  14.         if(leapYear(year)==1&&month==2)  
  15.             monthDays=29;  
  16.         int countLeapYear=0;  
  17.         for(int i=1800;i<year;i++){  
  18.             if(leapYear(i)==1)  
  19.                 countLeapYear++;  
  20.         }  
  21.         int dateCount=(year-1800)*365+countLeapYear;  
  22.         String ms1="Jan";  
  23.         switch(month){  
  24.         case 2:dateCount+=31;ms1="Feb";break;  
  25.         case 3:dateCount+=59;ms1="Mar";break;  
  26.         case 4:dateCount+=90;ms1="Apr";break;  
  27.         case 5:dateCount+=120;ms1="May";break;  
  28.         case 6:dateCount+=151;ms1="June";break;  
  29.         case 7:dateCount+=181;ms1="July";break;  
  30.         case 8:dateCount+=212;ms1="Aug";break;  
  31.         case 9:dateCount+=243;ms1="Sep";break;  
  32.         case 10:dateCount+=273;ms1="Oct";break;  
  33.         case 11:dateCount+=304;ms1="Nov";break;  
  34.         case 12:dateCount+=334;ms1="Dec";break;  
  35.         default:break;  
  36.         }  
  37.         if(month>1&&(leapYear(year)==1))  
  38.             dateCount++;  
  39.         int n=(dateCount%7+3)%7;  
  40.         int pcount=0;  
  41.         System.out.printf("             %s  %d             \n",ms1,year);  
  42.         System.out.println("-----------------------------------");  
  43.         System.out.println("  Sun  Mon  Tue  Wed  Thu  Fri  Sat");  
  44.         for(int i=1;i<=n;i++){  
  45.             System.out.print("     ");  
  46.             pcount++;  
  47.         }  
  48.         for(int i=0;i<monthDays;i++){  
  49.             if(pcount%7==0)  
  50.                 System.out.printf("\n");  
  51.             System.out.printf("%5d",i+1);  
  52.             pcount++;  
  53.         }         
  54.     }  
  55.     public static int leapYear(int n){  
  56.         if(n%4==0&&(n%100!=0))  
  57.             return 1;  
  58.         else if(n%400==0)  
  59.             return 1;  
  60.         else   
  61.             return 0;  
  62.     }  
  63.   
  64. }  

编程练习:

5.26,显示前100个回文素数

  1. public class C05T26 {  
  2.     public static void main(String[] args){  
  3.         int counter=0;  
  4.         int i=2;  
  5.         while(counter<100){  
  6.             if(judgeHuiwen(i)&&judgePrime(i)){  
  7.                 counter++;  
  8.                 System.out.printf("%8d",i);  
  9.                 if(counter%10==0)  
  10.                     System.out.printf("\n");  
  11.             }  
  12.             i++;  
  13.         }  
  14.     }  
  15.     public static boolean judgeHuiwen(int n){  
  16.         int k=n;  
  17.         int rNum=0,remain;  
  18.         while(k!=0){  
  19.             remain=k%10;  
  20.             rNum=rNum*10+remain;              
  21.             k=k/10;           
  22.         }  
  23.         if(rNum==n)  
  24.             return true;  
  25.         else  
  26.             return false;  
  27.     }  
  28.     public static boolean judgePrime(int n){  
  29.         int i;  
  30.         if(n==0||n==1)  
  31.             return false;  
  32.         for(i=2;i<=n;i++){  
  33.             if(n%i==0)  
  34.                 break;  
  35.         }  
  36.         if(i==n)  
  37.             return true;  
  38.         else  
  39.             return false;             
  40.     }  
  41.   
  42. }  

5.33调用System.currentTimeMillis()显示当前日期和时间。

  1. import javax.swing.JOptionPane;  
  2. public class C05T33 {  
  3.     public static void main(String[] args){  
  4.         long totalms=System.currentTimeMillis();  
  5.         long year=cYear(totalms);  
  6.         String month=cMonth(totalms);  
  7.         long date=cDate(totalms);  
  8.         long hour=cHour(totalms);  
  9.         long min=cMin(totalms);  
  10.         long sec=cSec(totalms);  
  11.         JOptionPane.showMessageDialog(null,"Current date and" +  
  12.                 " time is "+month+" "+date+", "+year+" "+  
  13.                 hour+":"+min+":"+sec);  
  14.     }  
  15.     public static long cYear(long n){  
  16.         long x=0;  
  17.         long days=n/1000/3600/24;  
  18.         long i=1969;  
  19.         while(days>=x){  
  20.             if(leapYear(i))  
  21.                 x+=366;  
  22.             else  
  23.                 x+=365;  
  24.             i++;  
  25.         }  
  26.         return i;  
  27.     }  
  28.     public static String cMonth(long n){  
  29.         long days=n/1000/3600/24;  
  30.         long i;  
  31.         for(i=1970;i<cYear(n);i++){  
  32.             days=days-365;  
  33.             if(leapYear(i))  
  34.                 days--;  
  35.         }  
  36.         String x="";  
  37.         if(leapYear(cYear(n))){  
  38.             if(days<=31)  
  39.                 x="Jan";  
  40.             if(days<=60&&days>31)  
  41.                 x="Feb";  
  42.             if(days<=91&&days>60)  
  43.                 x="Mar";  
  44.             if(days<=121&&days>91)  
  45.                 x="Apr";  
  46.             if(days<=152&&days>121)  
  47.                 x="May";  
  48.             if(days<=182&&days>152)  
  49.                 x="Jun";  
  50.             if(days<=213&&days>182)  
  51.                 x="Jul";  
  52.             if(days<=244&&days>213)  
  53.                 x="Aug";  
  54.             if(days<=274&&days>244)  
  55.                 x="Sep";  
  56.             if(days<=304&&days>274)  
  57.                 x="Oct";  
  58.             if(days<=335&&days>304)  
  59.                 x="Nov";  
  60.             if(days<=366&&days>335)  
  61.                 x="Dec";  
  62.         }  
  63.         else{  
  64.             if(days<=31)  
  65.                 x="Jan";  
  66.             if(days<=59&&days>31)  
  67.                 x="Feb";  
  68.             if(days<=90&&days>59)  
  69.                 x="Mar";  
  70.             if(days<=120&&days>90)  
  71.                 x="Apr";  
  72.             if(days<=151&&days>120)  
  73.                 x="May";  
  74.             if(days<=181&&days>151)  
  75.                 x="Jun";  
  76.             if(days<=212&&days>181)  
  77.                 x="Jul";  
  78.             if(days<=243&&days>212)  
  79.                 x="Aug";  
  80.             if(days<=273&&days>243)  
  81.                 x="Sep";  
  82.             if(days<=303&&days>273)  
  83.                 x="Oct";  
  84.             if(days<=334&&days>303)  
  85.                 x="Nov";  
  86.             if(days<=365&&days>334)  
  87.                 x="Dec";              
  88.         }  
  89.         return x;  
  90.     }  
  91.     public static long cDate(long n){  
  92.         long days=n/1000/3600/24;  
  93.         long i,x=0;  
  94.         for(i=1970;i<cYear(n);i++){  
  95.             days=days-365;  
  96.             if(leapYear(i))  
  97.                 days--;  
  98.         }  
  99.         if(leapYear(cYear(n))){  
  100.             if(days<=31)  
  101.                 x=31-days;  
  102.             if(days<=60&&days>31)  
  103.                 x=60-days;  
  104.             if(days<=91&&days>60)  
  105.                 x=91-days;  
  106.             if(days<=121&&days>91)  
  107.                 x=121-days;  
  108.             if(days<=152&&days>121)  
  109.                 x=152-days;  
  110.             if(days<=182&&days>152)  
  111.                 x=182-days;  
  112.             if(days<=213&&days>182)  
  113.                 x=213-days;  
  114.             if(days<=244&&days>213)  
  115.                 x=244-days;  
  116.             if(days<=274&&days>244)  
  117.                 x=274-days;  
  118.             if(days<=304&&days>274)  
  119.                 x=304-days;  
  120.             if(days<=335&&days>304)  
  121.                 x=335-days;  
  122.             if(days<=366&&days>335)  
  123.                 x=366-days;  
  124.         }  
  125.         else{  
  126.             if(days<=31)  
  127.                 x=31-days;  
  128.             if(days<=59&&days>31)  
  129.                 x=59-days;  
  130.             if(days<=90&&days>59)  
  131.                 x=90-days;  
  132.             if(days<=120&&days>90)  
  133.                 x=120-days;  
  134.             if(days<=151&&days>120)  
  135.                 x=151-days;  
  136.             if(days<=181&&days>151)  
  137.                 x=181-days;  
  138.             if(days<=212&&days>181)  
  139.                 x=212-days;  
  140.             if(days<=243&&days>212)  
  141.                 x=243-days;  
  142.             if(days<=273&&days>243)  
  143.                 x=273-days;  
  144.             if(days<=303&&days>273)  
  145.                 x=303-days;  
  146.             if(days<=334&&days>303)  
  147.                 x=334-days;  
  148.             if(days<=365&&days>334)  
  149.                 x=365-days;           
  150.         }  
  151.         return x;  
  152.     }  
  153.     public static long cHour(long n){  
  154.         long hours=(n/1000/3600+8)%24;  
  155.         return hours;  
  156.     }  
  157.     public static long cMin(long n){  
  158.         long hours=n/1000/60%60;  
  159.         return hours;  
  160.     }  
  161.     public static long cSec(long n){  
  162.         long hours=n/1000%60;  
  163.         return hours;  
  164.     }  
  165.     public static boolean leapYear(long n){  
  166.         if(n%4==0&&(n%100!=0))  
  167.             return true;  
  168.         else if(n%400==0)  
  169.             return true;  
  170.         else   
  171.             return false;  
  172.     }  
  173.   
  174. }  



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Java中byte数组与long数组相互转化
日期操作类 DateUtil
java基本数据类型转换
java类型之间的转换
Proguard使用最新,最全教程,亲自试验
【性能】Java BigDecimal和double性能比较
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服