java迴圈樣本

來源:互聯網
上載者:User

標籤:print   個數   char   sgd   oid   tor   []   bsp   add   

用while迴圈計算100之內的奇數和偶數和

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("100之內偶數的和:"+sum);

System.out.println("100之內奇數的和:"+num);

}

}

 

用迴圈輸出1000之內所有被5整除的數,並且最多輸出3個

 

public void Test{

int i=0;

int j=0;

while(i<1000){//條件

if(i%5==0){//能被5整除的數

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

j++;

if(j%3==0){//三個

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

j=0;

}

}

i++;

}

}

 

 

99乘法表

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();

}

 

階乘:

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);

}

 

 

/**
  * 編程求: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);
 }
}

 

輸入一個字串,統計大寫字母,小寫字母的個數;

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("大寫字母個數:%d",upper);

System.out.printf("小寫字母個數:%d",lowe);

}

 

java迴圈樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.