/*使用下面的方法,返回一年的天数 * public static void numberOfDaysInAYear(int year) * 编写一个程序,显示从2000年到2010年每年的天数 */ public class DayOfYear { public static void main(String[] args) { // numberOfDaysInAYear(2010); // numberOfDaysInAYear(2011); // numberOfDaysInAYear(2012); // numberOfDaysInAYear(2013); // numberOfDaysInAYear(2014); // numberOfDaysInAYear(2015); //提示用户输入开始和结束的年数 Scanner sc = new Scanner(System.in); System.out.println("请输入开的年数:"); int start = sc.nextInt(); System.out.println("请输入结束年数:"); int end = sc.nextInt(); days(start,end); } // public static void numberOfDaysInAYear(int year){ // if(year%4==0 &&year%100!=0 ||year %400 ==0){ // System.out.println(year+"是闰年,一共有366天"); // }else{ // System.out.println(year+"是平年,一共有365天"); // } // // } public static void days(int start, int end){ for(int x =start;x<=end;x++){ if(x%4==0&&x%100!=0 ||x%400==0){ System.out.println(x+"是闰年,一共有366天"); }else{ System.out.println(x+"是平年,一共有365天"); } } } }
*编写一个方法,使用下面的方法头显示n X n 的矩阵 * public static void printMatrix(int n) * * */ public class Private { public static void main(String[] args) { printMatrix(3); } public static void printMatrix(int n){ for(int x =0;x<n;x++){ for(int y=0;y<n;y++){ System.out.print((int)(Math.random()*2)); } System.out.println(); } } } ========================== 111 000 110
/*使用math类中的sqrt 方法编程 打印表格 * */ public class Sqrt { public static void main(String[] args) { System.out.print("数字"+" "+"平方根"); System.out.println(); sqtr(20); } public static void sqtr(int a){ for(int x=0;x<a;x++){ System.out.print(x+" "); System.out.print( Math.sqrt(x)); System.out.println(); } } } ============================= 数字 平方根 0 0.0 1 1.0 2 1.4142135623730951 3 1.7320508075688772 4 2.0 5 2.23606797749979 6 2.449489742783178 7 2.6457513110645907 8 2.8284271247461903 9 3.0 10 3.1622776601683795 11 3.3166247903554 12 3.4641016151377544 13 3.605551275463989 14 3.7416573867739413 15 3.872983346207417 16 4.0 17 4.123105625617661 18 4.242640687119285 19 4.358898943540674
/* * 编写一个程序,读入3条边的值, * 如果输入有效,则计算面积,否则显示输入无效 * */ public class MyTrangle { public static void main(String[] args) { //提示用户输入3条边 Scanner sc = new Scanner(System.in); System.out.println("请输入3条边:"); double side1 = sc.nextDouble(); double side2 = sc.nextDouble(); double side3 = sc.nextDouble(); boolean flag =isValid(side1, side2, side3); if(flag == true){ System.out.println("该三角形的面积是:"+area(side1, side2, side3)); }else{ System.out.println("你输入的边长不对"); } } public static boolean isValid(double side1,double side2,double side3){ boolean flag; //判断是否是3角型 if((side1+side2)> side3 && Math.abs(side1-side2)<side3){ flag = true; }else{ flag = false; } return flag; } public static double area(double side1,double side2,double side3){ double area; double s; //计算周长 s =(side1+side2+side3)/2; //计算面积 area =Math.sqrt(s*(s-side1)*(s-side2)*(s-side3)) ; return area; } }