Java第四次作业

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

(1)for循环

package com.itheima.practice;
public class Test18 {
public static void main(String[] args) {
int sum = 0;
int a = 1;
for (a = 1; a <= 100; a++) {
if (a % 3 == 0) {
sum += a;
}
}
System.out.println("1到100之间全能被3整除的整数和为:" + sum);
}
}

 

 

(2)while循环
package com.itheima.practice;
public class Test19 {
public static void main(String[] args) {
int sum=0;
int a=1;
while(a<=100){
if(a%3==0) {
sum+=a;
}
a++;
}
System.out.println("1到100之间全能被3整除的整数和为:"+sum);
}
}

 

 (3)do.......while循环

package com.itheima.practice;
public class Test20 {
public static void main(String[] args) {
int sum=0;
int a=1;
do{
if(a%3==0) {
sum+=a;
}
a++;
}while(a<=100);
System.out.println("1到100之间全能被3整除的整数和为:"+sum);
}
}

 

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

package com.itheima.practice;
public class Test21 {
public static void main(String[] args) {
for (int a = 0; a <= 9; a++) {
if (a == 5) {
continue;
}
System.out.println("输出的数值为:"+a);
}
}
}

 

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

package com.itheima.practice;
import java.util.Scanner;
public class Test22 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int sum=1;
System.out.println("请输入数值:");
int a=input.nextInt();
for(int b=1;a>0;a--) {
sum*=b;
b++;
}
System.out.println("结果为:"+sum);
}
}

 

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

package com.itheima.practice;
import java.util.Scanner;
public class Test23 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("输入学生成绩:");
double score = input.nextDouble();
while (score < 0 || score > 100) {
System.out.println("输入错误,请重新输入");
score = input.nextDouble();
}
if (score >= 0 && score <= 100) {
System.out.println("输入正确");
}
}
}

 

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

package com.itheima.practice;
public class Test24 {
public static void main(String[] args) {
double ys=30000;
double as=30000;
for (int year = 1; year <= 10; year++) {
ys+=ys*0.06;
as+=ys;
}
System.out.println("该员工10年后的年薪为:"+ys);
System.out.println("该员工未来10年的年薪为:"+as);
}
}

 

 

作业:

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

package com.itheima.practice;
public class Test25 {
public static void main(String[] args) {
for (int a= 100; a <= 999; a++) {
int ge = a % 10;
int shi = a / 10 % 10;
int bai = a/ 100;
if (ge * ge * ge + shi * shi * shi + bai * bai * bai == a) {
System.out.print("水仙花数为:"+a + " ");
}
}
}
}

 

 

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

package com.itheima.practice;
import java.util.Scanner;
public class Test26 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int sum = 0;
System.out.println("输入年");
int year = input.nextInt();
System.out.println("输入月");
int mon = input.nextInt();
System.out.println("输入日");
int day = input.nextInt();
int[] yue = new int[]{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
for (int b = 0; b < mon - 1; b++) {
sum += yue[b];
}
} else {
for (int c = 0; c < mon - 1; c++) {
sum += yue[c];
}
sum -= 1;
}
sum += day;
System.out.println("今天是这一年的第" + sum + "天");
}
}

 

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

package com.itheima.practice;
import java.util.Scanner;
public class Test27 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入一个4位整数");
int num=input.nextInt();
int num_a=num/1000;
int num_b=num/100%10;
int num_c=num/10%10;
int num_d=num%10;
System.out.println("原数字:"+num+"调换后:"+(num_d*1000+num_c*100+num_b*10+num_a));
}
}
posted @ 2023-04-05 12:11  清风饮露ㅤ  阅读(21)  评论(0编辑  收藏  举报