What is the logical structure and method function of Java
This article mainly explains "what is the logical structure and method function of Java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Java logical structure and method function is what" it!
? Logical structure Branch if// format 1if (a) {} / / format 2if (a) {} else {} / / format 3if (a) {} else if (b) {} else if (c) {} else {}
It should be noted that the condition must be a Boolean expression
In C language, if (1) means to enter the execution. In Java, it must be if (true).
In C language, 0 stands for false, non-0 represents true, but in java it must be true or false
Need to pay attention to the problem of else drape
Int x = 10 int y = 20 x if (x = 10) if (y = 10) System.out.println ("true"); else System.out.println ("false")
It's easy to see that else and the first if are on the same indent, so nothing is output.
We see the output of false, which seems to go into else and the second loop together.
Else is with its nearest if.
In the idea compiler, hit enter and automatically indent in the second if.
Switchswitch (integer | enumeration | character | string) {case content 1: {execute statement when content is satisfied; [break;]} case content 2: {execute statement when content is satisfied; [break;]} default: {execute statement when none of content is satisfied; [break;]}}
Integers and characters are common, and string types seem to have never been seen before. here are some examples.
Cycle structure while cycle while (a) {} for cycle for (a) do while cycle do {} while (a)
It should be noted that the do while loop executes before judgment, and while judges before execution.
Break and continue
Both let the loop end, but the difference is that
Break will end the whole cycle directly.
Continue ends the current loop and moves on to the next loop.
Input / output output System.out.println (data); / / output a string with newline System.out.print (data); / / output a string without newline System.out.printf (format, data); / / formatted output, similar to printf input in C language
The input in Java is complicated.
Import java.util.Scanner; / / need to import util package Scanner scanner = new Scanner (System.in); System.out.println ("Please enter your name:"); String name = scanner.nextLine (); System.out.println ("Please enter your age:"); int age = scanner.nextInt (); System.out.println ("Please enter your salary:"); float salary = scanner.nextFloat (); System.out.println ("your information is as follows:") System.out.println ("name:" + name+ "\ n" + "Age:" + age+ "\ n" + "salary:" + salary); sc.close (); / / Note, remember to call the close method
If multiple sets of inputs are required
Scanner sc = new Scanner (System.in); double sum = 0.0Brit num = 0Both while (sc.hasNextDouble ()) {double tmp = sc.nextDouble (); sum + = tmp; num++;} System.out.println ("sum =" + sum); System.out.println ("avg =" + sum / num); sc.close ();? Method
The methods in Java are essentially functions in the C language.
Let's have a summation method.
Public static int addSum (int n) {int sum = 0; for (int iTunes 1)