Java第五周作业

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

1.

for:

import java.util.Scanner;

public class text2 {

 

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

      Scanner input = new Scanner("System.in");

      int sum=0;

      int i=1;

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

      if (i%3==0)

       sum+=i;

          i++;

          System.out.println(sum);

       

}

 

}

}

 

 

 while:

 package boke4月2日;
 2 
 3 public class Q2 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         int i = 1;
11         int sum = 0;
12         while (i <= 100) {
13             if (i % 3 == 0) {
14                 sum += i;
15             }
16             i++;
17         }
18         System.out.println("1到100之间所有能被3整除的整数的和" + sum);
19     }
20 }

 

 

 

 do-while:

 1 package boke4月2日;
 2 
 3 public class Q3 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         int i = 1;
11         int sum=0;
12         do {
13             if (i % 3 == 0){
14                 sum+=i;
15             }
16             i++;
17         } while (i <= 100);
18         System.out.println("1到100之间所有能被3整除的整数的和" + sum);
19     }
20 
21 }

 

 

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

 import java.util.Scanner;

public class text3 {

 

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

       Scanner input = new Scanner(System.in);

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

        if (i!=5){

        System.out.println(i);

        }

        else {

        }

       }

}

 

 

 

 

 

 

 

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

import java.util.Scanner;

public class text4 {

 

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

      Scanner s = new Scanner(System.in);

      int n = 5;

      int result = 1;

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

       result*=i;

      }

      System.out.println(result);

}

}

 

 

 

 

 

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

 import java.util.Scanner;

public class text5 {

 

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

       Scanner input = new Scanner(System.in);

      for (int s=1;;s++ ){

     System.out.println("请输入任意学生成绩,合法后停止");  

        double a = input.nextDouble();

        if(a<0||a>100){

        System.out.println("输入成绩不合法!");

        

        }else{

        System.out.println("成绩正确,结束输入");

       break;

        }

      }

       }

      }

 

 

 

 

 

 

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

import java.util.Scanner;

public class text6 {

 

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

      Scanner input = new Scanner(System.in);

      double year= 1;

      double pay=30000;

      double growth=0.06;

 

      for(;year<=10;year++){

       pay=pay*(1+0.06);

      }

      System.out.println("该员工未来10年的总收入是");

      System.out.println(String.format("%.2f",pay));

}

}

 

 

作业

1.

打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。

 

1 package boke4月2日;
 2 
 3 public class Q8 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         for (int s = 100; s <= 999; s++) {
11             int ge = s % 10;
12             int shi = s / 10 % 10;
13             int bai = s / 100;
14             if (ge * ge * ge + shi * shi * shi + bai * bai * bai == s)
15                 System.out.println(s);
16         }
17     }
18 
19 }

 

 

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

import java.util.Scanner;
public class text7 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int year,month,date;
System.out.println("请输入年份:");
year=input.nextInt();
System.out.println("请输入月份:");
month=input.nextInt();
System.out.println("请输入日期:");
date=input.nextInt();
switch(month-1){
case 11:
date+=30;
case 10:
date+=31;
case 9:
date+=30;
case 8:
date+=31;
case 7:
date+=31;
case 6:
date+=30;
case 5:
date+=31;
case 4:
date+=30;
case 3:
date+=361;
case 2:
// 闰年判断
if (year%400==0||year%4==0||year%100!=0){
date+=29;
}
else //不是
//天数加28
date+=28;
case 1:
date+=31;
default:
System.out.println("输入正确月份");
break;
}
//判断月份是否有误

if(month>1&&month<=12){
System.out.println("该天是"+year+"年的第"+date+"天");
}
}

}

复制代码     

 

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

import java.util.Scanner;
public class text8 {

/**
* @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();
if(x>999&&x<=9999){
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);
}else{
System.out.println("错误");}
}

posted @ 2022-07-03 21:54  欧阳晨  阅读(22)  评论(0编辑  收藏  举报