Java Note 4-do While loop, break, modifier, method call

Source: Internet
Author: User
Tags modifiers

Do and loop
Grammar:
do{
Loop body
}while (conditional expression);

Note: It is the loop structure that executes the loop body before it is judged.

Such as:
int i = 0;
do{
System.out.println ("i =" +i);
i++;
}while (I < 10);

---
The Do While is commonly used in "interactive" interface loops.
Case:

--------------------------
Break statement and Continue statement
Break can be used in:
1). switch statement that exits a case block
2). In the loop, it means breaking the loop closest to where you are.

Continue statements are used in:
1). In the loop, means the end of the cycle [closest to the layer of your own], start the next round

Such as:
for (int i=1;i<=9;i++) {
if (i% 3 = = 0) {
Break i=1,i=2
Continue i=1,2,4,5,7,8
}
System.out.println ("i =" +i);
}
------------
Class assignments:
1. Design a program that prompts the user to enter an integer and calculates the factorial of the integer.

2. Design a program that outputs all primes within 200 [prime number]

3. Design a program to print out all the complete numbers within 1000 [perfect number]
A complete number is the sum of all the approximations of a number that is exactly equal to it [does not contain itself]
Example: 6 = 1+2+3

--------------------------------------------------------------
Formatted output [printf method]
Method:
Normal output
SYSTEM.OUT.PRINTLN (String output);
System.out.print (String output);
Formatted output
System.out.printf (String ouput,object ... args);

Such as:
int i = 5;
int j = 17;
//
System.out.println ("i =" +i+ ", j =" +j ");
Formatted output
System.out.printf ("i =%d,j =%d\n", i,j);

==
The syntax of the placeholder:
%[argument_index$][flag][width][.precision]conversion
The commonly used conversion are:
D stands for integers
F stands for floating-point numbers
B for Boolean type
s represents a character or string
T represents the date or time
...

Width represents the bit width that the placeholder occupies.

The. Precision represents the decimal place.

Flag stands for flag
-Indicates left alignment.
...

--
Case:

====================
Methods [Method]
Definition: is a collection of small pieces of functionality, each of which has its input and output, so a
A good method is a relatively independent, complete small function.

The constituent parts of a method: [Syntax]
[modifier] Returns the type method name ([parameter list]) [throws exception type] {
Method body
}
--
modifier [modifier]
1. Access Control modifiers
All class sub-packages themselves
Publicyyyy
Protectednyyy
[Default] Nnyy
Privatennny

2. Special modifiers
Static statics
Abstract abstraction
Final eventually
Synchronized synchronization
Native Local
...
Method can add more than one modifier, and there is no order between the modifiers.
So:
public static void Main (string[] args) {}
Or
static public void Main (stirng[] args) {}

---
return type ' return '
All valid data types can be used as the return type of the method.
In addition, there is a special type: Void can also be used as the return type of the method.

If the return type of the method is designed to void, this method does not have a return value, which means
That, in the method body, there can be no return statement.
If the return type of the method is not void, it means that the method must have a return value, that is,
In the method, be sure to have a return statement.

Thinking: How to determine the return type of a method when designing a method?

--
Method Name "Methods name"
is the name code of a method.
The method name should be able to express the main function of this method.

--
Parameter list "parameter list"
The parameter of the method, also called the formal parameter, is essentially a local variable [local variable]

Parameter syntax:
Data type parameter name

Separate multiple parameters with commas.

Note: Formal parameters are not actual values for formal parameter. It has only types.
The parameter is assigned by argument only when this method is called [Invoke].


Such as:
public class hehe{
public int Add (int a, int b) {
int C = a + B;
return C;
}
public static void Main (string[] args) {
Hehe h = new Hehe ();
int result = H.add (5,19); Ok
}
}
Method is called:
1). Called by the class name. Method name, which requires that this method must use the static modifier
Such as:
Double d = math.random (); Class name. Method Name

2). Called by object. Method Name, this method is not modified with static
Such as:
Scanner sc = new Scanner (system.in); Creating objects
int i = Sc.nextint (); Object. Method Name
--------------

Cases:
Complete Job 2, with the method of thinking to complete:
Design the first method to determine whether a given integer is a prime number
public boolean isprime (int n) {
//...
}
Design a method that is responsible for the number of primes in the specified range of printouts
public void printprime (int max) {
//....
...
if (IsPrime (i)) {//Call directly

}
}
Finally, in the main method, call the
public static void Main (string[] args) {
xxx x = new xxx ();
int a = 200;
X.printprime (a);
}

-------------
Homework.
Complete the Guess number game

Java note 4-do while loop, break, modifier, method call

Related Article

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.