Get the App
SLTechnology News&Howtos  ›  Development  › 

The method of JAVA process Control

Shulou Source: shulou.com Published: 2022-06-01 05:06:46 10月18日 Update

This article mainly explains "the method of JAVA process control". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the method of JAVA process control".

JAVA input / output input / output

Two input methods:

Method 1: java.util.Scanner

The code is as follows:

Public class a {public static void main (String [] args) {var sc = new Scanner (System.in); System.out.println ("Please enter your name:"); String name = sc.nextLine (); System.out.printf ("% n Welcome to:% s", name);}}

Generate Scanner object, output "Please enter name:", return the input string and assign it to name, output "% n Welcome you% s" where% n means newline% s represents name

Results:

Method 2: JOptionPane input content is determined on the string value, as long as it is not determined is null

Public class a {public static void main (String [] args) {String w = JOptionPane.showInputDialog ("Please enter words:"); System.out.println (w);}}

Results:

Output

Three methods of output in the console

Method 1: System.out.print (); output to the console

Method 2: System.out.println (); output to the console and wrap

Method 3: System.out.printf (); format the output to the console

Code demonstration:

The first is direct output without line wrapping.

Public class a {public static void main (String [] args) {int w = 1; int a = 2; System.out.print (w); System.out.print (a);}}

Results:

The second kind of newline output

Public class a {public static void main (String [] args) {int w = 1; int a = 2; System.out.println (w); System.out.println (a);}}

Results:

The third formatted output

D means a variable of type int, which means to replace the value of w with the value of the first% d _ an and the second% d

Public class a {public static void main (String [] args) {int w = 1; int a = 2; System.out.printf ("wicked% d asides% d", w, a);}}

Results:

Branch statement if else

If () returns true as long as the condition in parentheses is correct, and false if it is wrong.

Whether or not else means

Public class a {public static void main (String [] args) {if (1 > 2) {System.out.println ("A");} else {System.out.println ("B");}

Multiple judgments are as follows: if the first judgment is incorrect, proceed to the next judgment, and execute it when the return value is true, otherwise execute else.

Public class a {public static void main (String [] args) {if (1 > 2) {System.out.println ("A");} else if (1 > 0) {System.out.println ("B");} else {System.out.println ("C");}} switch case default

Switch multi-branch switch statement

The parenthesis w in switch (w) is the judgment parameter, and the number after case is the value that matches w. When the value of w matches the value after case, the statement in the current case is executed

Break means to withdraw from the current judgment, which means that there is no need for judgment after it ends directly.

Default means the default value, which is defaulted when there is no match.

Public class a {public static void main (String [] args) {int wrought 1; String wk = ""; switch (w) {case 2: wk = "Monday"; break; case 3: wk = "Tuesday"; break Case 4: wk = "Wednesday"; break; case 5: wk = "Thursday"; break; case 6: wk = "Friday"; break; case 7: wk = "Saturday" Break; default: wk = "Sunday"; break;} System.out.println (wk);}}

Results:

Circular statement for

For (int iTunes 0; I < 5; iTunes +) is divided into three parts. Int iTunes 0 is the initial value, and exits the loop at i5.

Public class a {public static void main (String [] args) {for (int I = 0; I < 5; iTunes +) {System.out.println (I);}

Results:

For in

For in is mainly used to loop collections or arrays, using arrays to demonstrate

Public class a {public static void main (String [] args) {int [] a = {1,2,3,4,5}; for (int I: a) {System.out.println (I);}

I corresponds to the values in the following table of the array a, which is equivalent to the values of cyclic outputs a [0], a [1] a [2], a [3] a [4].

While do while

While (condition) {}

The condition is met to execute the statement, but the exit is not satisfied.

Public class a {public static void main (String [] args) {int I = 0; while (I < 5) {System.out.println (I);}

Results:

Do while

Unlike while, do while executes once before judging

Public class a {public static void main (String [] args) {int I = 0; do {ionization; System.out.println (I);} while (I < 0);}}

Here, execute the output first, and then judge. So condition I 10) break;}

When I is divisible by 2, skip this time and proceed to the next loop. When I is greater than 10, the cycle ends.

At this point, I believe that everyone on the "JAVA process control methods" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Output method result loop input control week statement meaning console condition process content array code branch name character string that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MariaDB Apple Huawei macOS