Java第五次作业

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

import java.util.Scanner;
public class t1 {

    /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            
            int x=0;
            int y;
            for(y=1;y<101;y++){
                if(y%3==0){
                    x=x+y;
                }
            }
            System.out.println(x);

            }

        
        }

import java.util.Scanner;
public class t1 {

    /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            
            int x=0;
            int y=1;
            while(y<=100){
                if(y%3==0){
                    x=x+y;

                }
                    y++;
            }

            System.out.println(x);

            }

        
        }

import java.util.Scanner;
public class t1 {

    /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            
            int x=0;
            int y=1;
            do{
                if(y%3==0){
                    x=x+y;
                }
                y++;
            }while(y<=100);
            
            System.out.println(x);

            }

        
        }

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

 import java.util.Scanner;
public class t1 {

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

            }

        
        }

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

import java.util.Scanner;
public class t1 {

    /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner input=new Scanner(System.in);
            System.out.println("输入一个整数");
            int n=input.nextInt();
            int x;
            int y=1;
            for(x=1;x<=n;x++){
             y=y*x;
            }
            System.out.println(y);


            }

        
        }

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

 import java.util.Scanner;
public class t1 {

    /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner input=new Scanner(System.in);
            System.out.println("输入成绩");
            int x=input.nextInt();
                while(x>100||x<0){
                    System.out.println("输入错误从新输入");
                    x=input.nextInt();
                }
                System.out.println("输入正确");

            }

        
        }

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

import java.util.Scanner;
public class t1 {

    /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
             double n=30000;
               int x=1;
               double y=0;
               while(x<=10){
                   y=y+n;
                   n=(1+0.06)*n;
                   x++; 
               }
               System.out.println("十年总收入"+y);
               System.out.println("十年后的年薪"+n);


            }

        
        }

  1. 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次
     import java.util.Scanner;
    public class t1 {
    
        /**
             * @param args
             */
            public static void main(String[] args) {
                // TODO Auto-generated method stub
                int x = 100;
                while (x <= 999) {
                    int a = x % 10;
                    int b = x / 10 % 10;
                    int c = x / 100;
    
                    if (a * a * a + b * b * b + c * c * c == x)
                        System.out.println(x);
                    x++;
                }
    
                }
    
            
            }

  2. 输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
    import java.util.Scanner;
    public class t1 {
    
        /**
             * @param args
             */
            public static void main(String[] args) {
                // TODO Auto-generated method stub
                Scanner input = new Scanner(System.in);
                  int x, y, z;
                  /*x为年份,y为月份,z为日期*/
                  System.out.println("请输入年份:");
                  x=input.nextInt();
                  System.out.println("请输入月份:");
                  y=input.nextInt();
                  System.out.println("请输入日期:");
                  z=input.nextInt();
                  switch(y-1){
                  case 11:
                   z+=30;
                  case 10:
                   z+=31;
                  case 9:
                   z+=30;
                  case 8:
                   z+=31;
                  case 7:
                   z+=31;
                  case 6:
                   z+=30;
                  case 5:
                   z+=31;
                  case 4:
                   z+=30;
                  case 3:
                   z+=31;
                  case 2:
                   if((x%400==0)||(x%4==0&&x%100!=0)){
                    z+=29;
                   }
                   else{
                    z+=28;
                   }
                  case 1:
                    z+=31;
                default:
                    System.out.println("输入月份");
                    break;
                  }
                  System.out.println("今天是"+x+"年的第"+z+"天");
    
    
                }
    
            
            }

  3.  

    由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句
     import java.util.Scanner;
    public class t1 {
    
        /**
             * @param args
             */
            public static void main(String[] args) {
                // TODO Auto-generated method stub
                Scanner input = new Scanner(System.in);
                  System.out.println("输入四位整数");
                          int x=input.nextInt();
                          if(x>=1000 && x<=9999){
                           int a=x%10;           /*a为个位数*/
                           int b=x % 100 / 10;   /*b为十位数*/
                           int c=x%1000/100;     /*c为百位数*/
                           int d=x/1000;         /*d为千位数*/
                           int y;
                           y=d+c*10+b*100+a*1000;
                           System.out.println(y);
                          }
                          else{
                           System.out.println("逆序");
                          }
    
    
    
                }
    
            
            }

posted @ 2021-04-07 12:57  子桑  阅读(29)  评论(0)    收藏  举报