Welcome to visit the blog conditional Conditional statement-if
public class Test {public
static void Main (string[] args) {
int a =;
if (a==100) {
System.out.println ("A is a value");}
}
public class Test {public
static void Main (string[] args) {
int x =;
int y =;
if (x>y) {
System.out.println ("variable x is greater than variable y");
}
if (x<y) {
System.out.println ("variable x is less than variable Y");}}
public class Test {public
static void Main (string[] args) {
int math =;
if (math>60) {
System.out.println ("mathematically passed");
} else {
System.out.println ("Math failed");
}}
public class Test {public
static void Main (string[] args) {
int x =;
if (x>30) {
System.out.println ("A value greater than");
} else if (x>10) {
System.out.println ("A has a value greater than 10 but less than");
} else if (x>0) {
System.out.println ("A's value is greater than 0 but less than");
} else {
System.out.println ("A's value is less than 0");
}
}
Conditional Statement-Switch
public class Test {public
static void Main (string[] args) {
System.out.println ("Today is the day of the Week:");
Int week = 2;
Switch (week) {case
1:
System.out.println ("Monday");
break;
Case 2:
System.out.println ("Tuesday");
break;
Case 3:
System.out.println ("Wednesday");
break;
Default:
System.out.println ("Sorry, I don ' t Know");}}
Instance-Verifying the legality of login information
Import Java.util.Scanner;
public class Test {public
static void Main (string[] args) {
Scanner scan = new Scanner (system.in);
System.out.println ("Please enter login username:");
String username = scan.nextline ();
System.out.println ("Please enter login password:");
String password = scan.nextline ();
if (!username.equals ("Mr")) {
System.out.println ("User name is illegal");
} else if (!password.equals ("Mrsoft")) {
SYSTEM.OUT.PRINTLN ("Login password error");
} else {
System.out.println ("Congratulations, login information through Validation");}
}
Example-Assigning a department to a new employee
Import Java.util.Scanner;
public class Test {public
static void Main (string[] args) {
Scanner scan = new Scanner (system.in);
System.out.println ("Please enter the name of the new employee:");
String name = Scan.nextline ();
System.out.println ("Please enter the programming language for the new employee:");
String language = Scan.nextline ();
Switch (Language.hashcode ()) {case
3254818: Case
2301506: Case
2269730:
System.out.println ("employee" + Name + is assigned to the Java Program Development Department. ");
break;
Case 3104: Case
2112:
System.out.println ("employee" + name +) is assigned to the C # Project maintenance group. ");
break;
case-709190099: Case
955463181: Case
9745901:
System.out.println ("Employee +name+" is assigned to the ASP.NET Program Test Department.) ");
break;
Default:
System.out.println ("the company does not need" + language + "language Program developers.) ");
}
}
}
LoopsLoop Statement-While
public class Test {public
static void Main (string[] args) {
int x = 1;
int sum = 0;
while (x<=10) {
sum = sum+x;
x + +;
}
System.out.println ("from 1 to 10 and is:" + sum);
}
Loop Statement-Do...while
Loop body, execute at least once.
public class Test {public
static void Main (string[] args) {
int a =;
while (a==60) {//once System.out.println not executed
("ok! A==60 ");
a--;
}
int b = m;
do { //execute at least once
System.out.println ("ok! B==60 ");
b--;
}
while (b==60);
}
}
Loop Statement-For
public class Test {public
static void Main (string[] args) {
int sum = 0;
for (int i=1; i<=100; i+=1) {
sum = sum+i;
}
System.out.println (the sum of the numbers between 1 and 100 is: "+ sums";
}
}
Loop Statement-foreach
public class Test {public
static void Main (string[] args) {
int arr[] = {7, 1};
System.out.println ("Elements in one-dimensional array are:");
for (int x:arr) {
System.out.print (x+ "\ t");}}
Instance-traversing an array using a while loop
public class Test {public
static void Main (string[] args) {
string[] aves = new string[] {"Lu", "Red-crowned Crane", "Oriole", "Wu Raven "," Magpie "," cuckoo "," Gray-Bird "," Lark "};
int index = 0;
SYSTEM.OUT.PRINTLN ("There are many birds in my garden, species are:");
while (Index < aves.length) {
System.out.print (aves[index++] + " ");
}
}
Instance-Output 99 multiplication table using for loop
public class Test {public
static void Main (string[] args) {for
(int i=1, i<=9; i++) {for
(int j=1; j< =i; J + +) {
System.out.print (j + "*" + i + "=" + (i*j) + "T");
}
System.out.println ();}}
Jump Statement-Break
public class Test {public
static void Main (string[] args) {
int sum = 0;
String flag = "Number of consecutive integers from 1 to 100 is:";
for (int i=1; i<=100; i++) {
sum +=i;
if (sum>1000) {
flag = "from 1 to" + i + "The number of consecutive integers is:";
Jump out of the layer loop break
;
}
SYSTEM.OUT.PRINTLN (flag + sum);
}
Jump Statement-Continue
public class Test {public
static void Main (string[] args) {
int i = 0;
System.out.println ("Odd number within 10:");
while (i<10) {
i++;
if (i%2 = = 0) {
continue
}
System.out.print (i + " ");}}
Jump Statement-Return
Instance-end loop body (break)
public class Test {public static void main (string[] args) {System.out.println ("\n--
--an example of interrupting a single-layer cycle----");
string[] array = new string[] {"Lu", "Red-crowned Crane", "Oriole", "Parrot", "Crow", "Magpie", "Eagle", "cuckoo", "Eagle", "gray-Bird", "Eagle", "Lark"};
for (String String:array) {if (String.equalsignorecase ("Eagle")) break;
System.out.print ("Have:" + string + "");
System.out.println ("\ n \----Example of a single 2-layer loop----");
int[][] Myscores = new int[][] {{67,78,63,22,66}, {55,68,78,95,44},{95,97,92,93,81}};
System.out.println ("Baby this test score: \ n math \ t Chinese T-English T-art T-history");
No1:for (int[] is:myscores) {for (int i:is) {System.out.print (i + "t"); if (i<60) {System.out.println (\ n et cetera, "+ i +" is divided into what.) Why didn't you pass the exam?
");
Break No1;
} System.out.println (); }
}
}
Example-loop body filter (Continue)
public class Test {public
static void Main (string[] args) {
string[] array = new string[] {"Lu", "Red-crowned Crane", "Oriole", "Parrot", " Crows, "Magpie", "Eagle", "cuckoo", "Eagle", "Gray Bird", "Eagle", "Lark"};
SYSTEM.OUT.PRINTLN ("There are many birds in my garden, but recently a few eagles, please help me take them away.") ");
int eaglecount = 0;
for (String string:array) {
if (string.equals ("Eagle")) {
System.out.println ("An eagle has been caught in a cage.") ");
eaglecount++;
Continue;
}
System.out.println ("Search for birds, found:" + string);
}
System.out.println ("Caught" altogether: "+ Eaglecount +" only eagle. ");
}
}
Instance-output a hollow diamond using a for loop
public class Test {public static void main (string[] args) {Printhollowrhombus (10);
public static void Printhollowrhombus (int size) {if (size%2==0) size++;
for (int i=0; i<size/2+1; i++) {for (int j=size/2; j>i+1; j--) {System.out.print (""); for (int j=0; j<2*i+1; J + +) {if (j==0 | | j==2*i) System.
Out.print ("*");
else System.out.print ("");
} System.out.println (""); for (int i=size/2+1; i<size; i++) {for (int j=0; j<i-size/2; J + +) {System.out.
Print (""); for (int j=0; j<2*size-1-2*i; J + +) {if (j==0 | | j==2* (SIZE-I-1)) Syste
M.out.print ("*");
else System.out.print ("");
} System.out.println ("");}
}
}
Instance-use for loop output Yang Hui's triangle
public class Test {public
static void Main (string[] args) {
int triangle[][] = new int[8][];
for (int i=0; i<triangle.length; i++) {
Triangle[i] = new int[i+1];
for (int j=0; j<=triangle[i].length-1; J + +) {
if i==0| | j==0| | j==triangle[i].length-1)
Triangle[i][j] = 1;
else
triangle[i][j] = Triangle[i-1][j] + triangle[i-1][j-1];
System.out.print (Triangle[i][j] + "T");
}
System.out.println ();}}