1.分别使用for循环,while循环,do循环求1到100之间所有能被3整除的整数的和。(知识点:循环语句)
while:
public class lwm1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int i = 0; int x = 1; while (x <= 100) { if (x % 3 == 0) i = i + x; x++; } System.out.println(i); } }
for:
public class lwm2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int i = 0; for (int x = 0; x <= 100; x++) { if (x % 3 == 0) { i += x; } } System.out.println(i); } }
do while:
public class lwm3 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int i = 0; int x = 0; do { x++; if (x % 3 == 0) { i += x; } } while (x <= 100); System.out.println(i); } }
2.输出0-9之间的数,但是不包括5。(知识点:条件、循环语句)
public class lwm4 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int i = 0; while (i < 10) { if (i != 5) { System.out.println(i); } i++; } } }
3.编写一个程序,求整数n的阶乘,例如5的阶乘是1*2*3*4*5(知识点:循环语句)
public class lwm5 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int i = 1; int x = 2; while (x <= 5) { i *= x; x++; } System.out.println(i); } }
4.编写一个程序,输入任意学生成绩,如果输入不合法(<0或者>100),提示输入错误,重新输入,直到输入合法程序结束(知识点:循环语句)
import java.util.Scanner; public class lwm6 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); int a = input.nextInt(); while (a > 100 || a < 0) { System.out.println("错误"); a = input.nextInt(); } System.out.println("正确"); } }
5.假设某员工今年的年薪是30000元,年薪的年增长率6%。编写一个Java应用程序计算该员工10年后的年薪,并统计未来10年(从今年算起)总收入。(知识点:循环语句)
public class lwm7 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub double a=30000; double b=0; int i=1; while(i<=10){ a=a+a*0.6; b+=a; i++; } System.out.println("十年后的年薪"+a); System.out.println("十年后的总收入"+b); } }
作业:
1.打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
public class lwm1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int i = 100; while (i < 1000) { int a = i / 100; int b = i % 100 / 10; int c = i % 10; if (i == a * a * a + b * b * b + c * c * c) { System.out.println(i); } i++; } } }
2.输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
import java.util.Scanner; public class lwm2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); int year; int mouth; int day=0; int days; int d=0; int e=0; do { System.out.println("输入年月日"); year=input.nextInt(); mouth=input.nextInt(); days=input.nextInt(); if (year<0||mouth<0||mouth>12||days<0||days>31) { System.out.println("input error!"); e=1; } } while (e==1); for (int i= 1; i< mouth; i++) { switch (i) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: { day=31; break; } case 4: case 6: case 9: case 11: { day=30; break; } case 2: { if ((year % 100 != 0 && year % 4 == 0) || (year % 100 == 0 && year % 400 == 0)) { day=29; }else{ day=28; } } default: break; } d+=day; } System.out.println("这是"+year+"年的"+(d+days)+"天"); } }
3.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
import java.util.Scanner; public class lwm3 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("输入四位数"); int i = input.nextInt(); while (i > 0) { System.out.println(i % 10); i = i / 10; } } }