1,判断一个年份是否是闰年
package dictation; import java.util.Scanner; public class test2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入年份:"); int year = scanner.nextInt(); /* * 判断逻辑 * 如果年份能被4整除并且不能被100整除,或能被400整除的数为闰年 */ if((year%4 == 0&&year%100 != 0)||year%400 == 0) { System.out.println(year+"是闰年"); }else { System.out.println(year+"不是闰年"); } } }
2,输出两个int数中的最大值
1 package demo; 2 3 import java.util.Scanner; 4 5 public class demo01 { 6 7 public static void main(String[] args) { 8 Scanner scanner = new Scanner(System.in); 9 10 System.out.println("请输入两个整数:"); 11 //比较两个数的大小 12 int a = scanner.nextInt(); 13 14 int b = scanner.nextInt(); 15 16 int max; 17 18 if(a>=b) { 19 max = a; 20 }else { 21 max = b; 22 } 23 24 System.out.println("最大值为"+max); 25 26 } 27 28 }
3,使用无返回值的方法的调用
package demo; class Box{ double width; double height; double depth; void volume() { System.out.print("Volume is"); System.out.println(width*height*depth); } } public class demo1 { public static void main(String args[]) { Box box1 = new Box(); Box box2 = new Box(); double volume1; double volume2; box1.depth=1; box1.width=2; box1.height=3; box2.depth=1; box2.width=2; box2.height=3; //调用方法 box1.volume(); box2.volume(); } }
3,使用有返回值的方法的调用
package demo; class Box{ double width; double height; double depth; double volume() { //返回值返回给调用它的box1.box2对象,将数值赋给volume1,volume2 return width*height*depth; } } public class demo1 { public static void main(String args[]) { Box box1 = new Box(); Box box2 = new Box(); double volume1; double volume2; box1.depth=1; box1.width=2; box1.height=3; box2.depth=1; box2.width=3; box2.height=3; //调用方法 volume1=box1.volume(); System.out.println("voulme is"+volume1); volume2=box2.volume(); System.out.println("voulme is"+volume2); } }
原文:https://www.cnblogs.com/zhengchujie/p/11931120.html