当前位置:网站首页>12.24 day exercise -- Programming summation, 99 multiplication table, while loop and for loop exercises

12.24 day exercise -- Programming summation, 99 multiplication table, while loop and for loop exercises

2022-06-12 04:59:00 なんでもないゃ

Programming summation : ∑1+∑2+∑3+……+∑100 =1 +(1+2)+(1+2+3)+(...)+(1+2+3+4+5+…+100)

package Play;
// Programming summation :
//∑1+∑2+∑3+……+∑100
//=1 +(1+2)+(1+2+3)+(...)+(1+2+3+4+5+…+100)
public class Sum {
    public static void main(String[] args) {
        int sum=0;
        int SUM=0;
        for(int i=1;i<=3;i++){
            for(int j=0;j<=i;j++){
                sum+=j;
            }
           // SUM+=sum;  If you put it here, the answer is 15
        }
        SUM+=sum;
        System.out.println("∑1+∑2+∑3+……+∑100:"+SUM);// Output 10

        }
    }

Be confused :SUM+=sum Must be on for Outside of the loop , Unresolved

multiplication table

public class JiuJiu {
    public static void main(String[] args) {
        //1*1=1  xy
        //1*2=2  2*2=4  x y+1  x+1 y+1
        //1*3=3  2*3=6  3*3=9  x y+2  x+1 y+2  x+2 y+2
        //1*4=4  2*4=8  3*4=12
        //······
        //1*9=9  2*9=18  3*9=27······9*9=81
        for(int i=1;i<=9;i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + "*" + i + "=" + j * i+"  ");
            }
            System.out.println();
        }

    }

 while Circulation and for Circulation practice

public class Thousand {
    public static void main(String[] args) {
    // use  while  and  for  Cyclic output  1-1000  Can be  5  Divisible number , And each line outputs  3  individual 
        System.out.println("*************while loop *************");
        int i=1;
        int j=0;
        while(i<=1000){
            if(i%5==0){
                System.out.print(i+"\t");
                j++;
                if(j%3==0){
                    System.out.println();
                }
            }
            i++;
        }
        System.out.println();
        System.out.println("**************for loop **************");
        for(int m=1,n=0;m<=1000;m++){
           if(m%5==0){
               System.out.print(m+"\t");
               n++;
               if(n%3==0){
                   System.out.println();
               }
           }

        }
        }
    }

原网站

版权声明
本文为[なんでもないゃ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010622133957.html