■ Normally, in order to use the fields of a class, it is necessary to instantiate the class using new, but the System class is instantiated when the program is started. Therefore, it can be used immediately (class variable) without instantiation.
import java.util.Scanner;
public class Season{
	public static void main(String[] args){
		System.out.println("Please enter an integer");
	    //The scanner next to the input stream new is the constructor
		Scanner scan = new Scanner(System.in);
		int a = scan.nextInt();
		switch (a) {
			case 1 : case 2: case 12:
				System.out.println("It's winter");
				break;
			case 3 : case 4 : case 5:
				System.out.println("It's spring");
				break;
			case 6 : case 7: case 8:
				System.out.println("It's summer");
				break;
			case 9 : case 10: case 11:
				System.out.println("It's Autumn");
				break;
			default :
				System.out.println(a+"The moon does not exist");
			}
	    //Use the close method to destroy the instance and
	    //Free the used memory area.
		scan.close();
	}
}
■ A mistake that can still be made to forget the break.
Recommended Posts