Java循環語句練習問題(2)


1、循環出力1から100までの間に、3または4で割り切れるすべての数。
package com.hz.loop02;

/**
 * 1、    1 100      3   4    。
 * @author ztw
 *
 */
public class Practice01 {

public static void main(String[] args) {

    for(int i=1;i<=100;i++){
        //      3   4  ,     
        if(i%3==0||i%4==0){

            System.out.println(i);
        }
    }
}
}
2、循環出力200から300までの間に全部5で割り切れるか、あるいは2で割り切れるか、3で割り切れる数。
package com.hz.loop02;

/**
 * 2、    200 300      5  ,   2      3    。
 * @author ztw
 *
 */
public class Practice02 {

public static void main(String[] args) {
    //  200 300  
    for(int i=200;i<=300;i++){
        //      5  ,   2      3    
        if(i%5==0||(i%2==0&&i%3==0)){
            System.out.println(i);
        }
    }
}
}
3、循環出力1から2000の中で全部4で割り切れるが、100で割り切れない数、または400で割り切れる数。
package com.hz.loop02;

/**
 * 3、    1 2000    4      100    ,   400    。
 * @author ztw
 *
 */
public class Practice03 {

public static void main(String[] args) {
    //    1 2000
    for(int i=1;i<=2000;i++){
        //     4      100    ,   400    
        if((i%4==0&&1%100!=0)||i%400==0){
            System.out.println(i);
        }
    }
}
}
4、1+2+3+…+100の結果を計算します。
package com.hz.loop02;

/**
 * 4、  1+2+3+……+100   。
 * @author ztw
 *
 */
public class Practice04 {

public static void main(String[] args) {
    //           0
    int sum =0;
    //i  +1
    for(int i=1;i<=100;i++){
        //1-100  
        sum+=i;
    }
    System.out.println("1+2+3+……+100    :"+sum);
}
}
5、1*2*3***10の結果を計算します。
package com.hz.loop02;

/**
 * 5、  1*2*3*……*10   。
 * @author ztw
 *
 */
public class Practice05 {

public static void main(String[] args) {
    ////           1
    int sum = 1;
    //i  +1
    for(int i=1;i<=10;i++){
        //     +1  
        sum=sum*i;
    }
    System.out.println("1*2*3*……*10    :"+sum);
}
}
6、1+1/4+1/9+を計算します。+1/(20*20)
package com.hz.loop02;

/**
 * 6、  1+1/4+1/9+....+1/(20*20)
 * @author ztw
 *
 */
public class Practice06 {

public static void main(String[] args) {
    //      
    int number = 1;
    double sum = 0; 
    /*
     *    +1,          ?.0   
     */
    while(number<=20){

        sum +=1.0/(number*number);
        number++; 
    }
    //    
    System.out.println(sum);

}
}
7、1つの整数を入力して変数nに入れます。この整数が0より大きい場合、1+2+3+…+(n−1)+nを計算します。そうでないと、「入力されたデータはエラーがあります。」
package com.hz.loop02;

import java.util.Scanner;

/**
 * 
 * 7、           n ,        0,
 *     1+2+3+……+(n-1)+n   ,    “        
 * @author ztw
 *
 */
public class Practice07 {

public static void main(String[] args) {

    int sum = 0;
    Scanner sc = new Scanner(System.in);
    System.out.println("      :");
    int n = sc.nextInt();
    if(n>0){
        for(int i=0;i<=n;i++){
            sum+=i;
        }
    }else{
        System.out.println("        !");
    }
    System.out.println(sum);
}
}
8、5人の学生の成績を循環的に入力して、この5人の学生の合計点と平均点を計算します。
package com.hz.loop02;

import java.util.Scanner;

/**
 * 8、    5      ,   5      ,    
 * @author ztw
 *
 */
public class Practice08 {

public static void main(String[] args) {

    float sum = 0;
    float avg = 0;
    Scanner sc = new Scanner(System.in);
    /*
     *     5      
     *      
     */
    for(int i=1;i<=5;i++){
        System.out.println("       :");
        float sroce = sc.nextFloat();
        sum+=sroce;
    }
    //     
    avg = sum/5;
    System.out.println("  :"+sum+"   :"+avg);
}
}
9、まずユーザーに学生の数を入力して変数nに入れるように要求します。この数が0より大きい場合、n回の成績を受けて合計点と平均点を計算します。さもなければ、「学生の人数はマイナス数ではいけません。」
package com.hz.loop02;

import java.util.Scanner;

/**
 * 9、                  n ,
 *        0,     n   n      ,
 *         。    “          
 * @author ztw
 *
 */
public class Practice09 {

public static void main(String[] args) {

    int n = 0;
    float sum=0;
    Scanner sc = new Scanner(System.in);
    System.out.println("       :");
    n = sc.nextInt();
    /*
     *     n    0
     *     0,          ,    ”          “
     */
    if(n>0){
        for(int i=1;i<=n;i++){
            System.out.println("       :");
            float sroce = sc.nextFloat();
            sum+= sroce;
        }
        //      
        float avg = sum/n;
        System.out.println("  :"+sum+"    :"+avg);
    }else{
        System.out.println("          ");
    }
}
}
10、循環的に「妻、私を愛していますか?」と聞きます。「愛」と答えたら、ループを終了します。そうでなければ、この物語をプログラムで説明します。
package com.hz.loop02;

import java.util.Scanner;

/**
 * 10、   “  ,    ?”,
 *       “ ”,       ,
 *       。         
 * @author ztw
 *
 */
public class Practice10 {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    /*
     *    “  ,    ?”,
     *       “ ”,       ,
     *       。
     */
    for(;;){
    System.out.println("  ,    ?");
    String choice = sc.next();
    if(choice.equals(" ")){

        System.out.println("    ");
        //  ,    
        break;

    }else{

    }
    }
}
}
11、循環入力文字列は、これらの入力文字列を接続し、入力された文字列が「Esc」であるとループが終了し、最後にこの接続された文字列を表示します。例えば、abc入力def入力Escはabcdefを出力します。
package com.hz.loop02;

import java.util.Scanner;

/**
 * 
 *11、       ,              ,         “Esc”     ,
 *              。
 *  :  abc        def          Esc
 *   abcdef
 * @author ztw
 *
 */
public class Practice11 {

public static void main(String[] args) {

    String str = "";
    Scanner sc = new Scanner(System.in);
    //                 ,      16    。
    StringBuffer sbuffer = new StringBuffer();
    //        
    while(true){
        System.out.println("     :");
        str = sc.next();
        //    str  "Esc"
        if(str.equals("Esc")){

            break;
        }
        /*
         *      str           StringBuffer  ,
         *    StringBuffer             。
         */
        sbuffer.append(str);
    }
    //            
    System.out.println("        :"+sbuffer.toString());
}
}
12、年と月を入力し、この月のカレンダーを印刷します。例えば、2011年9月を入力して、2011年9月のカレンダーを印刷します。
package com.hz.loop02;

import java.util.Scanner;

/**
 *
 *12、       ,         ,  :  2011 9 ,   2011 9    
 * @author ztw
 *
 */
public class Practice12 {

public static void main(String[] args) {
    //            
    int year,month;
    Scanner sc = new Scanner(System.in);
    System.out.println("     :");
    year = sc.nextInt();
    System.out.println("     :");
    month = sc.nextInt();
    //          
    if(month<=12&&month>=1){
    /*
     *             
     */

    if(month==1||month==3||month==5||month==7||month==8){

            for(int i=1;i<=31;i++){
                System.out.print("  "+i+"  ");
                if(i%7==0){
                    System.out.println();
                }

            }
        }else if(month==2){   
            /*
             *             
             *     29 ,  28 
             */
            if((year%4==0&&year%100!=0)||year%400==0){
                for(int i=1;i<=29;i++){
                    System.out.print("  "+i+"  ");
                    //    7,   
                    if(i%7==0){
                        System.out.println();
                    }

                }

            }else{

                for(int i=1;i<=28;i++){
                    System.out.print("  "+i+"  ");
                    if(i%7==0){
                        System.out.println();
                    }

                }
            }

        }else{

            for(int i=1;i<=30;i++){
                System.out.print("  "+i+"  ");
                if(i%7==0){
                    System.out.println();
                }

            }

        }
    }else{
        System.out.println("        !!!");
    }
}
}