------variable parameters and health conditions for Java

Source: Internet
Author: User

Variable parameter: Applies to the indeterminate number of parameters. Type determines, Java handles variable parameters as arrays.

The variable parameter must be in the last item. When there is more than one variable number, there must be one that is not the last, so only a variable parameter is supported.


The written form of a variable number of parameters such as:
int add (int x,int ... args) {method body}

... Is between the variable type and the variable name. There are no spaces before and after the ability;
When a variable-parameter method is called. The compiler creates an array for the variable-number implication and, in the method body, an array of variable parameters.



Application of conditional logic inference (simulated artificial inference)

/* Someone invited A,B,C,D,E,F 6 to participate in a meeting, these 6 people are a bit strange. Because they have very many requirements. Known: 1) A, b two have at least 1 participants in the Conference; 2) A,e,f 3 out of 2 participants attended the Conference; 3) and C two were unanimously determined. or both of them. or neither of them; 4) only 1 of the A,d two participated in the conference. 5) Only 1 people in the C,d two have participated in the conference. 6) Suppose D does not go. So e also decided not to go. So what are the last few people who have joined the meeting?  */public class Meeting {public static void main (string[] args) {int a = 0, b = 0, c = 0, d = 0, E = 0, F = 0;/*for (int A1 = 1; A1 <= 2; a1++) {for (int a2 = 1, A2 <= 2; a2++) {for (int a3 = 1, A3 <= 2; a3++) {for (int a4 = 1; A4 <= 2; a4++) { t a5 = 1; A5 <= 2; a5++) {for (int a6 = 1; A6 <= 2; a6++) {*///Similar above 6 forfor (int i = 0; I <=, i++) {//binary means six persons (000000 ~ 111111) Go or is not to go 1 means to go, 0 means not to go a = ((I & 1) = = 0)? 0:1;b = ((I & 2) = = 0)? 0:1;c = ((i & 4) = = 0)?

0:1;d = ((I & 8) = = 0)? 0:1;e = ((i & 16) = = 0)? 0:1;f = ((I & 32) = = 0)? 0:1;if (Fun (A, B, C, D, E, F)) {System.out.println ("a=" + A + "b=" + B + "c=" + C + "d=" + D + "e=" + E + "f=" + f);}}} Statistics go to the number of private static int Countgo (int ... args) {//variable parameter int sum = 0;for (int a:args) {if (a = = 1) {//1 means go 0 means not to sum++;}} return sum;} To interpret whether the condition is the private static Boolean fun (int a, int b, int c, int d, int e, int f) {//Health condition, each guardian condition is like a guard, which specifically intercepts the illegal result data of the condition. After all the guards. The rest of the data is legal if (!) ( Countgo (A, B) >= 1) {return false;} if (! ( Countgo (A, E, f) = = 2)) {return false;} if (! ( Countgo (b, c) = = 0 | | Countgo (b, c) = = 2) {return false;} if (! ( Countgo (A, d) = = 1) {return false;} if (! ( Countgo (c, d) = = 1) {return false;} if (Countgo (d) = = 0 && Countgo (e) = = 1) {return false;} return true;}} Results: a= 1 b= 1 c= 1 d= 0 e= 0 f= 1









------variable parameters and health conditions for Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.