Java Learning notes-select structure

Source: Internet
Author: User

If statement

Dice Game (Dicegame):

public class Dicegame {
public static void Main (string[] args) {
Double s = math.random ();
int d = 1+ (int) (6*s);
if (d==6) {
System.out.println ("Your points are:" +d+ "Your luck is so good!" ");
}else if (d>=4) {
System.out.println ("Your points are:" +d+ "good luck." But it's already awesome! ");
}else if (d>=2) {
System.out.println ("Your points are:" +d+ "luck is a bit poor ha!") We need to work harder! ");
}else{
System.out.println ("Your points are:" +d+ "OK, you are very back, give you a chance!" ");
}
}
}

Switch statement

It is important to note that the switch statement has a case-penetrating phenomenon. So in normal use to pay attention to this, but there are some special circumstances we can also use this so-called case penetration phenomenon to achieve our goal.

eg://Letter Vowel Consonant judging procedure

public class test{

public static void Main (string[] args) {

Char letter = ' a ';

int randomnumber = (int) (26* (Math.random ()));

Char letter2 = (char) (letter + Randomnumber);

System.out.println (LETTER2);

Switch (letter2) {

Case ' a ':

Case ' E ':

Case ' I ':

Case ' O ':

Case ' U ':

System.out.println ("vowel Letter");

Break

Case ' Y ':

Case ' W ':

SYSTEM.OUT.PRINTLN ("semivowels letter");

Break

Default

SYSTEM.OUT.PRINTLN ("consonant letters");

break;

}

}

}

Java Learning notes-select structure

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.