java classic cases (1)

1, to determine whether a year is a leap year

Package Dictation; 

Import java.util.Scanner; 

public  class test2 { 

    public  static  void main (String [] args) { 
        Scanner Scanner = new new Scanner (the System.in); 
        System.out.println ( "Please enter the year:" ); 
        
        int year = scanner.nextInt ();
         / * 
         * Analyzing logic 
         * If the year is not divisible by four and divided by 100, or a number divisible by 400 is a leap year 
         * / 
        IF ((4 == 0 && year% 100% year! 0 =) == 0 || year 400% ) { 
            System.out.println (year + "is a leap year" ); 
        } the else {
            System.out.println (year + "is not a leap year" ); 
        } 
        
        
    } 

}

2, the maximum output of the two numbers 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 new Scanner (the System.in);
 . 9          
10          the System.out. the println ( "enter two integers:" );
 11          // size comparison of two numbers 
12 is          int a = scanner.nextInt ();
 13 is          
14          int B = scanner.nextInt ();
 15          
16          int max;
 . 17         
18 is          IF (A> = B) {
 . 19              max = A;
 20 is          } the else {
 21 is              max = B;
 22 is          }
 23 is          
24          System.out.println ( "maximum value" + max);
 25          
26 is      }
 27  
28 }

 3, use no return value of call

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, there are calls using the method's return value

Package Demo; 

    class Box {
         Double width;
         Double height;
         Double depth; 
    
    
        Double Volume () {
         // return value to the calling box1.box2 its objects, the value assigned to Volume1, volume2 
        return width * height * depth; 
        } 
    } 
public  class the demo1 {
     public  static  void main (String args []) { 
        Box box1 = new new Box (); 
        Box BOX2 = new 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);
        
    }
}

 

Guess you like

Origin www.cnblogs.com/zhengchujie/p/11931120.html