Java第五次作业

上机练习

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

package fhg.fgh.dfh;

 

public class test1 {

 

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

int sum=0;

   int i=1;

      for(;i<101;i++){

       if(i%3==0){

       sum=sum+i;

       }

      else{

       

      }

      }

      System.out.println(sum);

}

 

}

package fhg.fgh.dfh;

 

import java.util.Scanner;

 

public class test2 {

 

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

   int sum=0;

   int i=1;

   while(i<101){

   if(i%3==0){

   sum=sum+i;

   i++;

   }

   else{

   i++;

   }

   }

   System.out.println(sum);

}

 

}

package fhg.fgh.dfh;

 

public class test5 {

 

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

int sum=0;

int i=3;

do{

if(i%3==0){

sum+=i;

}

i++;

}while(i<=100);

System.out.println(sum);

}

 

}

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

package fhg.fgh.dfh;

 

 

public class test3 {

 

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

  for(int i=1;i<10;i++){

  if(i!=5){

  System.out.println(i);

  }

  }

}

 

}

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

package fhg.fgh.dfh;

 

import java.util.Scanner;

 

public class test4 {

 

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

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 fhg.fgh.dfh;

 

import java.util.Scanner;

 

public class test6 {

 

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

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 fhg.fgh.dfh;

 

import java.util.Scanner;

 

public class test7 {

 

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

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);

}

 

}

作业

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

    package kdhg.jag.ga;

     

    public class test1 {

     

    /**

     * @param args

     */

    public static void main(String[] args) {

    // TODO Auto-generated method stub

      for(int i = 100;i<1000;i++){

                int gewei = i%10;

                int shiwei = i%100/10;

                int baiwei = i/100;

                if(gewei*gewei*gewei+shiwei*shiwei*shiwei+baiwei*baiwei*baiwei==i){

                    System.out.println(i);

                }

            }

     

    }

     

    }

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

  3. package kdhg.jag.ga; 

  1. import java.util.Scanner;

     

    public class test2 {

     

    /**

     * @param args

     */

    public static void main(String[] args) {

    // TODO Auto-generated method stub

     

            int year,month,day,days=0;

            Scanner s = new Scanner(System.in);

            year = s.nextInt();

            month = s.nextInt();

            day = s.nextInt();

            switch(month){

            case 12:

                days=days+31;

            case 11:

                days=days+31;

            case 10:

                days=days+30;

            case 9:

                days=days+31;

            case 8:

                days=days+31;

            case 7:

                days=days+30;

            case 6:

                days=days+31;

            case 5:

                days=days+30;

            case 4:

                days=days+31;

            case 3:

                days=days+30;

            case 2:

                days=days+28;

                if(year%4==0 && year%100!=0||year%100==0){

                    days++;

                }

            

            }

     

            System.out.println(days+day);

    }

     

    }

 

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

package kdhg.jag.ga;

 

import java.util.Scanner;

 

public class test3 {

 

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

 Scanner sc = new Scanner(System.in);

         System.out.println("请输入一个四位数");

         int x = sc.nextInt();

         int gewei = x%10;

         int shiwei = x%100/10;

         int baiwei = x%1000/100;

         int qianwei = x/1000;

         int sum = qianwei + baiwei*10 +shiwei*100 +gewei*1000;

         System.out.println(sum);

}

 

}

 

posted @ 2021-04-07 17:05  董澳  阅读(52)  评论(0编辑  收藏  举报