Java第三次 上机练习+作业

上机练习:

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

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

    }

public class test11 {

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

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

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

public static void main(String[] args) {
        // TODO Auto-generated method stub
        for(int a=0;a<=9;a++) {
            if(a==5) {
                continue;
            }else {
                System.out.println(a);
            }
        }
    }

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

import java.util.Scanner;

public class text3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scan=new Scanner(System.in);
        System.out.println("输入一个数:");
        int a=scan.nextInt();
        
        int sum=1;
        for(int i=a;i>0;i--) {
            sum=i*sum;
        }
        System.out.println("阶乘="+sum);
    }
}

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

import java.util.Scanner;

public class text4 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scan=new Scanner(System.in);
        System.out.println("输入一个数:");
        int a=scan.nextInt();
        
        while(a<0||a>100) {
            System.out.println("输入错误,请重新输入");
            a=scan.nextInt();
        }
    }
}

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

public class text5 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        double sum=0;
        double a=30000;
        for(int i=1;i<=10;i++) {
            a=a*(1+0.06);
            sum+=a;
        }
        System.out.println("总工资为="+sum);
    }
}

作业:

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

import java.util.Scanner;

public class test6 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("请输入一个三位数");
        int a = input.nextInt();

        int b, c, d;
        b = a / 100;
        c = a % 100 / 10;
        d = a % 10;
        int x;
        x = b * b * b + c * c * c + d * d * d;
        if (a == x) {
            System.out.println("是水仙花数");
        } else {
            System.out.println("不是水仙花数");
        }
    }

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

import java.util.Scanner;

public class text7 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a,b;
        Scanner input = new Scanner(System.in);
        System.out.println("请输入年份");
        int year = input.nextInt();
        
        System.out.println("请输入月份");
        int month = input.nextInt();
        
        System.out.println("请输入日期");
        int day = input.nextInt();
        
        switch(month){
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            if(day>31){
                System.out.println("输入错误");
            }
            break;
        case 2:
            if(year%4==0&&year%100!=0||year%400==0){
                if(day>29){
                    System.out.println("输入错误");
                }
            }else{
                if(day>28){
                    System.out.println("输入错误");
                }
            }break;
        case 4:
        case 6:
        case 9:
        case 11:
            if(day>30){
                System.out.println("输入错误");
            }
            break;
        default:System.out.println("输入错误");
        break;
        }
        switch(month-1){
        case 11:
            day+=30;
        case 10:
            day+=31;
        case 9:
            day+=30;
        case 8:
            day+=31;
        case 7:
            day+=31;
        case 6:
            day+=30;
        case 5:
            day+=31;
        case 4:
            day+=30;
        case 3:
            day+=31;
        case 2:
            if(year%4==0&&year%100!=0||year%400==0){
                day+=29;
            }else{
                day+=28;
            }
        case 1:
            day+=31;
        case 0:
            day=day;
            System.out.println("年积日是"+day);
            break;
        default:System.out.println("输入错误");
        break;
        }
        
    }

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

import java.util.Scanner;

public class text8 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("请输入一个四位数");
        int a = input.nextInt();
        while (a < 1001 || a > 9999) {
            System.out.println("输入错误,请重新输入");
            a = input.nextInt();
        }

        int b, c, d, e;
        b = a / 1000;
        c = a % 1000 / 100;
        d = a % 100 / 10;
        e = a % 10;

        int x = e * 1000 + d * 100 + c * 10 + b;
        System.out.println("反转后的数为" + x);

    }

posted @ 2023-04-03 22:47  祝运红  阅读(43)  评论(0)    收藏  举报