zoukankan      html  css  js  c++  java
  • java第五次作业

    1.   分别使用for循环,while循环,do循环求1到100之间所有能被3整除的整数的和

    package one;
    public class one1 {
        public static void main(String[] args) {
            int sum = 0;
            for(int i =1; i <=100; i++) {
                if(i %3 == 0) {
                sum += i;
            }
        }
            System.out.println("1-100之间能被3整除的数的和: " + sum);
    }
    }
    
    package one;
    public class one1 {
        public static void main(String[] args) {
            int i = 1;
            int sum = 0;
            while(i < 101) {
                i++;
                if(i %3 == 0) {
                    sum += i;
                }
            }
            System.out.println("1-100能被3整除的和是: " + sum);
        }
    }
    
    package one;
    public class one1 {
        public static void main(String[] args) {
            int i = 0;
            int sum = 1;
            do {
                i++;
                if(i %3 == 0){
                    sum += i;
                }
            }while(i < 101);
            System.out.println("1-100能被3 整除的数的和: " +sum);
        }
    }

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

    package one;
    public class one1 {
        public static void main(String[] args) {
            int i;
            for(i = 1;i < 10; i++ ) {
                if(i!=5) {
            System.out.println( i );
                } else {
                }
                }
        }
    }

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

    package one;
    import java.util.*;
    public class one1 {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int i =sc.nextInt();
            int sum =1;
            for(int x=1; x <= i; x++) {
                sum=sum * x;
            }
            System.out.println( sum);
        }
    }

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

    package one;
    import java.util.*;
    public class one1 {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            for(int i = 0; ; i++) {
                System.out.println("请输入学生成绩: ");
                int score = input.nextInt();
                if(score < 0 || score > 100) {
                    System.out.println("输入有误,请重新输入: ");
                } else {
                    System.out.println("该学生成绩为: " + score);
                    break;
                }
            }
        }
    }

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

    package one;
    import java.util.*;
    public class one1 {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            double salary = 0;
            double tenyear = 30000;
            for(int i = 1; i <=10; i++){
                tenyear +=tenyear * 0.06;
                salary += tenyear;
            }
            System.out.println("该程序员十年后年薪: " +tenyear);
            System.out.println("该程序员未来十年的总收入: " +salary);
        }
    }
  • 相关阅读:
    PHP 大小写转换、首字母大写、每个单词首字母大写转换相关函数
    【论文学习4】BiSample: Bidirectional Sampling for Handling Missing Data with Local Differential Privacy
    【论文学习3】Local Differential Privacy for Deep Learning
    【论文学习2】 Differential Privacy Reinforcement Learning
    深度学习中的优化算法
    Spatial crowdsourcing
    “pip install tensorflow ”出现错误
    python或pip'不是内部或外部命令”
    pip install torch出现错误
    打不开gitHub的解决方法
  • 原文地址:https://www.cnblogs.com/zjzj123/p/12597859.html
Copyright © 2011-2022 走看看