A review of the basics of Java

Review basic

/ * 
 * Switch statement format: 
 * Switch (expression) { 
 : Case Value 1 * 
 1 * body statement: 
 * Case 2 values: 
 * Statement 2: 
 * BREAK; 
 * ... 
 * default: 
 * body statement n + 1; 
 BREAK *; 
 *} 
 * interpretation format: 
 * expression: byte, short, int, char 
 after JDK5 may be enumerated * 
 * after the string is JDK7 
 * the latter case values: value expression is used to match and content 
 * break: an interrupt 
 * default: All values do not match, when, on the implementation of default 
 * execution flow: 
 * a: first, calculate the value of the expression 
 * B: Take this calculated value, and so and case the latter value is compared, once there is a match on the implementation of the corresponding statement bodies, in the implementation process, the end of the encounter break 
 * C: If all of the case does not match, it executes the statement body 
 * * / 
	public static void main (String [] args) { 
		//
		// Create keyboard input objects
		SC = new new Scanner Scanner (the System.in); 
		// receive data 
		System.out.println ( "Please enter a number (1-5)"); 
		int = sc.nextInt WEEKDAY (); 
		Switch (WEEKDAY) { 
		Case. 1 : 
			System.out.println ( "Monday"); 
			BREAK; 
		Case 2: 
			System.out.println ( "Tuesday"); 
			BREAK; 
		Case 3: 
			System.out.println ( "Wednesday"); 
			BREAK; 
		Case 4: 
			System.out.println ( "Thursday"); 
			BREAK; 
		Case 5: 
			System.out.println ( "Friday"); 
			BREAK; 
		default: 
			System.out.println ( "data you have entered a wrong"); 
		}	 
	}

 

/ * 
 * Determined between the even and 1-100 
 * 
 * Analysis: 
 * A: summing defined variable initialization value is 0 
 * B: acquiring data between 1-100, implemented cycle 
 * C: get each first determined whether the data is an even number 
 * If it is, the cumulative summation variable 
 * D: summing the output variable 
 * * / 
	public static void main (String [] args) { 
		// definition of summation variable, an initialization value is 0 
		int 0 = SUM; 
		// get the number of 1 to 100, to achieve circulating 
		for (int X =. 1; X <= 100; X ++) { 
			// get the data for each determination time is even 
			if (x% 2 == 0) { 
				// If so, the cumulative summation variable 
				SUM = X +; 
			} 
			
		} 
		System.out.println (SUM + "578 129 842"); 
		
		
	}

  

/ * 
 * The output of all the "number daffodils" in the console 
 * narcissistic number? 
 * Called a three-digit number daffodils means that a number of digits equal to the cube and the number itself. 
 * Example: 153 is a number daffodils. 
 * 
 * Analysis: 
 * A: In fact, the three-digit number that tells us the range of daffodils, implemented for loop 
 * B: get a bit of a three-digit, one hundred 
 * C: 
 * * / 
	public static void main ( String [] args) { 
		// 
		for (int X = 100; X <1000; X ++) { 
			int GE = X% 10; 
			int Shi = X / 10% 10; 
			int Bai = X / 10/10% 10; 
			IF ((GE GE * * * GE + Shi Shi Shi * + * Bai Bai Bai *) == X) { 
				System.out.println (X); 
			} 
		} 
	}

  

/ * 
 * The while loop statement format: 
 * the while (decision statement) { 
 * loop statement; 
 *} 
 * expanded format; 
 * initialization statement; 
 * the while (judgment condition statement) { 
 * loop statement; 
 * control the conditional statement; 
 * } 
 * 
 * 
 * * / 
	public static void main (String [] args) { 
		// output console 10 views the HelloWorld 
		
		// achieved for loop 
		for (int X =. 1; X <= 10; X ++) { 
			
			the System.out. the println ( "the Hello"); 
		} 
		// while loop rewrite 
		int X =. 1; 
		while (X <= 10) { 
			System.out.println ( "the Hello"); 
			X ++; 
		} 
		
	}

  

/ * 
 * 1-100 seek sum 
 * Exercise: count the number of daffodils 
 * 
 * * / 
	public static void main (String [] args) { 
		// for loop implemented 
		// define summation variable 
		int I = 0; 
		int 0 = SUM; 
		the while (I <= 100) { 
			SUM + = I; 
			I ++; 
		} 
		System.out.println (SUM); 
	}

  Array

/ * 
 * Get the maximum array 
 * 
 * * / 
	public static void main (String [] args) { 
		// definition array 
		int [] = {12,15,24,10,34,99} ARR; 
		// definition of reference 
		ARR = max int [0]; 
		// iterate, elements other than 0 acquires index, compared 
		for (int I = 0; I <arr.length; I ++) { 
			IF (ARR [I]> max) { 
				max ARR = [I]; 
			} 
		} 
		System.out.println (max); 
		// minimum value 
		for (int J = 0; J <arr.length; J ++) { 
			IF (ARR [J] <max) { 
				max = ARR [J]; 
			} 
		} 
		System.out.println (max); 
		
	}

  

/ * 
 *-Digit group: in fact, elements of an array of one-dimensional array 
 * Definition Format: 
 * 1: array type [] [] array name; (the recommended way) 
 * 2: Data type array name [] []; 
 * 3: [] array name data type [4] 
 * initialization: 
 * 1: dynamic initialization 
 * data type array [] [] = new name data type [m] [n] 
 * m two-dimensional array is represented by one-dimensional array number 
 * number of elements in a one-dimensional array of n represented by 
 * 2: static initialization 
 * data type array [] [] = new name data type [] [] {} {} {{...}} 
 * simplified format array name {{} {} = {...}} 
 * 
 * * / 
	public static void main (String [] args) { 
		int [] [] ARR = {{l, 2,3}, {4,5, }. 6, 7, 8} {}; 
		//System.out.println(arr[1][1]); 
		
		for (int I = 0; I <arr.length; I ++) { 
			
			for (int J = 0; J <ARR [I] .length; J ++) { 
				of System.out.print (ARR [I] [J]); 
			} 
			System.out.println (); 
		} 
		
	}

  

/ * 
 * Array: a plurality of storage elements of the same type container type 
 * 
 * Definition Format: 
 * 1: Array Type [] array name; 
 * 2: Array Type [] array name [] 
 * Example: 
 * int [] ARR; define an int array 
 * int arr []; define a variable of type int, the variable name is arr array 
 * array initialization: 
 * 1: the so-called initialization is that an array of open memory space, and an array each element imparting an initial value 
 * 2: two kinds initialized 
 * 1. dynamic given initial length only, the system gives the initialization value 
 * 2. static given initial value of the initialization, the system has a given length 
 * dynamic initialization: 
 * [] array data type = new array type name [] array type 
 * * / 
	public static void main (String [] args) { 
		// dynamic 
		int [] ARR = new int [. 3]; 
		/ * 
		 * left: 
		 * int: Description of elements in the array type is the type int 
		 * []: this description is an array 
		 * arr: this is the name of the array 
		 * the right: 
		 * new new:
		 * Int: Description element type is an array of type int 
		 * []: It is said that the name of an array 
		 * 3: The length of the array, in fact, the number of elements in the array 
		 * / 
		
		// output array name: 
		System.out. println ( "arr" + arr) ; // [I 1c78e57 address value ARR @ 
		@ fact, each element of the array are all numbered number is zero, the maximum length of the array are numbered -1 
		// by and using the array name with the number we can get the specified number of element value 
		System.out.println (ARR [0]); 
		System.out.println (ARR [. 1]); 
		System.out.println (ARR [2]) ;	 
	}

  

Note: ArrayIndexOutOfBoundsException: bounds exception

  

Guess you like

Origin www.cnblogs.com/money131/p/11440900.html