Java Loop Example

Source: Internet
Author: User

Calculates the odd and even numbers within 100 with a while loop and

public class test{

public static void Main (string[] args) {

int sum=0;

int num=0;

for (int i=0;i<100;i++) {

if (i%2==0) {

Sum+=i;

}else{

Num+=i;

}

}

System.out.println ("The number of even-numbered and:"+sum);

System.out.println ("The number of odd-numbered and:"+num);

}

}

All the numbers divisible by 5 in the loop output 1000, and output up to 3

public void test{

int i=0;

int j=0;

while (i<1000) {//condition

if (i%5==0) {//Can be divisible by 5

System.out.println (i+ "\ t");

j + +;

if (j%3==0) {//three X

System.out.println ("\ n");

j=0;

}

}

i++;

}

}

99 Multiplication Table

public void Test () {

for (int i=1;i<10;i++) {

for (int j=1;j<=i;j++) {

SYSTEM.OUT.PRINTLN (j+ "*" +i+ "=" + (I*J<10))

}

}

System.out.println ();

}

Factorial:

public void num () {

int sum=0;

for (int i=1;i<=100;i++) {

int temp=0;

for (int j=1;j<=i,j++) {

Temp+=j;

}

Sum+=temp;

}

SYSTEM.OUT.PRINTLN (sum);

}

/**
* Programming: 1! +2! + ... +15!
*/
void Addfactorial () {
Long result = 0;
for (int i=1;i<=15;i++) {
int temp = 1;
for (int j=1;j<=i;j++) {
Temp *=j;
}
Result +=temp;
}
SYSTEM.OUT.PRINTLN (result);
}
}

Enter a string that counts the number of uppercase and lowercase letters;

public static void Main (string[] args) {

String str= "SADASWQRTGDSGDFHGDF";

int lowe = 0;

int upper = 0;

for (int i=0;i<str.lenth (); i++) {

Char c =str.charat (i);

if (c>= ' a ' &&c<= ' Z ') {

lowe++;

}

if (c>= ' A ' &&c<= ' Z ') {

upper++;

}

}

System.out.printf (" Number of capital letters:%d", upper);

System.out.printf (" number of lowercase letters:%d", Lowe);

}

Java Loop Example

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.