1. for循环: package finish; import java.util.*; public class csy { public static void main(String[] args) { int sum=0; for (int i = 1; i <=100 ; i++) { if (i%3==0){ sum+=i; } } System.out.println("1-100之间能被3整除的和是"+sum); } }
while循环: package finish; import java.util.*; public class csy { public static void main(String[] args) { int sum=0; int i=0; while(i<=100){ if(i%3==0){ sum+=i; } i++; } System.out.println("1-100之间能被3整除的和是"+sum); } }
do while 循环: package finish; import java.util.*; public class csy { public static void main(String[] args) { int sum=0; int i=0; do{ if(i%3==0){ sum+=i; } i++; }while(i<=100); System.out.println("1-100之间能被3整除的和是"+sum); } }
2.package finish; import java.util.*; public class csy { public static void main(String[] args) { for(int i=0;i<=9;i++){ if(i==5){ continue; } System.out.print(i); } } }
3.package finish; import java.util.*; public class csy { public static void main(String[] args) { Scanner csy=new Scanner(System.in); System.out.println("请输入一个整数n:"); int n = csy.nextInt(); int x=1; int y=n; for(int i=0;i<n;i++){ y=n-i; x=y*x; } System.out.println("n的阶乘为:"+x); } }
4.package finish; import java.util.*; public class csy { public static void main(String[] args) { Scanner csy=new Scanner(System.in); for(int i=1;;i++){ System.out.println("请输入一位学生成绩"); int score=csy.nextInt(); if(score<0||score>100){ System.out.println("输入成绩有误,请重新输入:"); }else{ System.out.println("该学生成绩为:"+score); break; } } } }
5.package finish; import java.util.*; public class csy { public static void main(String[] args) { double x=30000; double sum=0; for(int year=1;year<=10;year++){ x=x*1.06; sum+=x; } System.out.println("员工10年后的年薪为:"+x); System.out.println("员工未来10年总收入为:"+sum); } }