java第三次作业

1.分别使用for循环,while循环,do循环求1100之间所有能被3整除的整数的和。(知识点:循环语句)

for循环

package aaa;

public class bbb {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a,b=0;
        for(a=1;a<100;a++) {
            if(a%3==0)
            b=b+a;
        }
        System.out.println("和为"+b);
    }

}

 

while循环

package aaa;

public class bbb {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a=1,b=0;
        while(a<100) {
            if(a%3==0) {
            b=b+a;
            }
            a++;
        }
        System.out.println("和为"+b);
    }

}

 

 

 

do循环

package aaa;

public class bbb {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a=1,b=0;
        do {
            if(a%3==0) {
                b=b+a;
            }
            a++;
        }while(a<100);
        System.out.println("和为"+b);
    }

}

 

 

 

 

2.输出0-9之间的数,但是不包括5。(知识点:条件、循环语句)

package aaa;

public class bbb {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a=1;
        do {
            if(a==5) 
                a++;
            System.out.println(""+a);
            a++;
        }while(a<10);
    }

}

 

 

 

 

3.编写一个程序,求整数n的阶乘,例如5的阶乘是1*2*3*4*5(知识点:循环语句)

package aaa;

import java.util.Scanner;

public class bbb {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a=1,b=1;
        System.out.println("请输入n");
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();        
        do {
            b=b*a;
            a++;
        }while(a<=n);
        System.out.println("阶乘为"+b);
    }

}

 

 

 

 

4.编写一个程序,输入任意学生成绩,如果输入不合法(<0或者>100),提示输入错误,重新输入,直到输入合法程序结束(知识点:循环语句)

package aaa;

import java.util.Scanner;

public class bbb {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("请输入成绩");
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();    
        while(n<0||n>100) {
            System.out.println("输入错误");
            n=in.nextInt();
        }
        System.out.println("成绩为"+n);
    }

}

 

 

 

 

5.假设某员工今年的年薪是30000元,年薪的年增长率6%。编写一个Java应用程序计算该员工10年后的年薪,并统计未来10年(从今年算起)总收入。(知识点:循环语句)

package aaa;

import java.util.Scanner;

public class bbb {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int n=30000,a=1,b=0;
        while(a<=10) {
            n=n*106/100;
            b=b+n;
            a++;            
        }
        System.out.println("10年后年薪"+n);
        System.out.println("总收入"+b);
    }

}

 

 

 

 

1.打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)

package aaa;

import java.util.Scanner;

public class bbb {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int n=100;
        while(n<1000) {
            int a=n/100,b=n%100/10,c=n%100%10;
            if(a*a*a+b*b*b+c*c*c==n) 
                System.out.println(""+n);
            n++;
        }
    }

}

 

 

 

 

2.输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)

import java.util.Scanner;

public class Ty {
    public static void main(String[] args) {
        //2.输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
        Scanner sc = new Scanner(System.in);
        int sum = 0;
        System.out.println("输入年");
        int year = sc.nextInt();
        System.out.println("输入月");
        int mon = sc.nextInt();
        System.out.println("输入日");
        int day = sc.nextInt();
        int[] yue = new int[]{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            for (int i = 0; i < mon - 1; i++) {
                sum += yue[i];
            }
        } else {
            for (int i = 0; i < mon - 1; i++) {
                sum += yue[i];
            }
            sum -= 1;
        }
        sum += day;
        System.out.println("今天是这一年的第" + sum + "");
    }
}

 

 

3.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)

import java.util.Scanner;

public class Ty {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入数");
        int shu = sc.nextInt();
        int dao = 0;
        while(shu != 0){
            int  yu = shu % 10;
            shu = shu / 10;
            dao *= 10;
            dao += yu;
        }
        System.out.println(dao);
   }
}

 

 

 

 

 

 

 

 

posted @ 2023-04-05 13:17  刘赓  阅读(23)  评论(0)    收藏  举报