Java Learning Note 12

Source: Internet
Author: User

Cycle

Print a string (for example: "Welcome to java!") 100 times, you need it. The following output statements are repeated 100 times, which is quite tedious:

System.out.println ("Welcome to java!");

Loop to control the number of times a statement block repeats execution

int count = 0;
while (Coun < 100) {
System.out.println ("Welcome to java!");
count++;
}

Java provides three types of loop statements: While loop, Do-while Loop, for loop
Syntax for While loop:
while (loop continuation condition) {
Loop body
Statement (group);
}

When the loop continuation condition is true, the while loop repeats the loop weight statement.


Guess number Game
Randomly generating integers between 0 and 100 and containing 0 and 100, the program prompts the user to enter a number consecutively until it matches the random number generated by the computer. The program tells the user that the input value is too big or too small for the user to enter the number each time, so that the user can make the next round of guessing wisely.

Pre-coding considerations:
What do you do first?
The first is to produce a random number between 0 and 100 and containing 0 and 100.
And do what?
The user is then prompted to enter a number of guesses
What's the last thing to do?
Finally, we compare this guessing number with the random number.

For programs that involve writing loops, if you don't know how to write loops immediately, you can write the code that loops only once, and then plan how to repeat the code in the loop.

Loop Design Strategy:
First step: Identify the statements that need to be duplicated
Step Two: Place these statements in a loop, as follows:
while (true) {
Statement Group;
}

Step three: Code for the Loop continuation condition and add the appropriate statement for the control loop
while (loop continuation condition) {
Statement Group;
An additional statement used to control the loop;
}

 PackageWelcome; Public classLoopdemo { Public Static voidMain (string[] args) {//print "Welcome to java!" String 100 times        intCount = 0;  while(Count < 100) {System.out.println ("Welcome to Java!"); Count++; }                //Calculate 1+2+3+4+5+6+7+8=9        intsum = 0, i = 1;  while(I < 10) {sum= Sum +i; I++; } System.out.println ("Sum is" +sum); }}
 PackageWelcome;ImportJava.util.Scanner; Public classGuessnumberonetime { Public Static voidMain (string[] args) {//generate a random number        intNumber = (int) (Math.random () * 101); //Create a Scanner objectScanner in =NewScanner (system.in); System.out.println ("Guess magic number between 0 and 100"); intGuess =-1;  while(Guess! =Number ) {        //prompting the user to enter a number of guessesSystem.out.print ("Enter Your guess:"); Guess=In.nextint (); //compare random and guess numbers        if(Guess = =Number ) {System.out.println ("Yes, the number is" +Number ); }Else if(Guess >Number ) {System.out.println ("Your guess is too"); }Else{System.out.println ("Your guess is too low"); }    } }}

Java Learning Note 12

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.