Java Basics Exercise [i]

Source: Internet
Author: User

Moka Classmate Java Study notes

Package Moka.hello;

public class HelloWorld {
public static void Main (string[] args) {
Initial Java
int a = 1;
int b = 3;
int sum = a+b;

If statement
if (a<b) {
SYSTEM.OUT.PRINTLN (sum);
}else{
SYSTEM.OUT.PRINTLN ("I want to learn java!" +sum);
}

For Loop statement
for (int i=1;i<sum;i++) {
System.out.println (a+sum);
}

Op-Expressions
System.out.println ((11+3*8)/4%3);

If multiple statements
int age = 70;
if (age>=60) {
SYSTEM.OUT.PRINTLN ("elderly");
}else if (<= && age <60) {
System.out.println ("middle age");
}else if (<= && age < 40) {
System.out.println ("juvenile");
}else{
System.out.println ("Children");
}

If statement nesting [requirements: If it is a weekend to stroll West Lake, cloudy indoor play, weeks to work]
String today= "Weekend";
String wether= "Clear";
if (Today.equals ("Weekend")) {
if (Wether.equals ("clear")) {
System.out.println ("Come to Hangzhou to stroll West Lake!") :):)");
}else{
System.out.println ("Play board games Indoors!") ");
}

}else{
System.out.println ("Go to work, can't come." :(");
}

If nesting [requirements: Finals with a preliminary score of more than 80 points, followed by gender re-division of the men's group finals and women's final]
int score = 94;
String sex = "female";
if (score >= 80) {
if (Sex.equals ("female")) {
SYSTEM.OUT.PRINTLN ("Women's Group Finals");
}else{
System.out.println ("Men's Team Finals");
}
}

Switch statement [according to the rank of the exam, give the top 4 different prizes, notebooks, IPAD2, mobile power, U disk]
int rank = 3;
Switch (rank) {
Case 1:
System.out.println ("First prize Notebook");
Break
Case 2:
System.out.println ("Second place, bonus ipad");
Break
Case 3:
System.out.println ("Third place, reward mobile power");
Break
Default
System.out.println ("Other, reward U disk");
}
switch practice [If it is Monday, five, breakfast buns, 246 fritters, Sunday Chairman Package]
Char nowday= ' Day ';
String x= "eat steamed buns";
String y= "eat fritters";
String z= "Eat Chairman's Package";
Switch (nowday) {
Case ' one ':
Case ' three ':
Case ' V ':
SYSTEM.OUT.PRINTLN (x);
Break
Case ' II ':
Case ' four ':
Case ' VI ':
System.out.println (y);
Break
Default
System.out.println (z);
}

While loop [Judge first, then execute]
int i = 1;
while (i<=10) {
System.out.println (i+ "I'm learning Java");
i++;
}

for (i=1;i<=5;i++) {
System.out.println (i+ "for Loop");
}

Do While loop [execute first, then judge---output 5 times]
int j = 1;
do{
SYSTEM.OUT.PRINTLN ("I want to learn Java");
j + +;
}while (j<=5);

Does while calculates an even number within 50 and
int sum1 = 0;
int num = 2;
do{
SUM1 + = num;
num = num + 2;
}while (NUM&LT;=50);
System.out.println ("The sum of even numbers within 50:" +SUM1);
[Exercise: Additive Expressions]
for (int a1=1,b1=5;a1<=b1;a1++,b1--) {
System.out.println (a1+ "+" +b1+ "=" + (A1+B1));
}
[Exercise: Calculate the sum of numbers that cannot be divisible by 3 between 1 and 100]
int sum3 = 0;
for (int i3=1;i3<=100;i3++) {
if (i3% 3!=0) {
SUM3 = Sum3+i3;
}
}
System.out.println ("1-100 cannot be divisible by 3 of the sum of the numbers:" +sum3);


}



}

Java Basics Exercise [i]

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.