1. 分别使用for循环,while循环,do循环求1到100之间所有能被3整除的整数的和。
1 public class test { 2 public static void main(String[] args) { 3 int he=0; 4 for (int i = 1; i <=100 ; i++) { 5 if (i%3==0){ 6 he+=i; 7 } 8 } 9 System.out.println(sum); 10 } 11 12 }
1 public class test { 2 public static void main(String[] args) { 3 int a=1,he=0; 4 do { 5 if(a%3==0) { 6 he+=a; 7 } 8 a++; 9 } while (a<=100); 10 11 System.out.println("和是"+he); 12 } 13 }
1 public class test { 2 3 public static void main(String[] args) { 4 int i=1; 5 int he=0; 6 do { 7 if (i%3==0){ 8 he+=i; 9 } 10 i++; 11 }while (i<=100); 12 System.out.println("和为"+he); 13 } 14 15 }
2. 输出0-9之间的数,但是不包括5。
1 public class test { 2 3 public static void main(String[] args) { 4 for (int i = 0; i <10 ; i++) { 5 if (i!=5){ 6 System.out.println("输出的值为"+i); 7 } 8 } 9 } 10 11 }
3. 编写一个程序,求整数n的阶乘,例如5的阶乘是1*2*3*4*5
1 import java.util.Scanner; 2 3 public class test { 4 5 public static void main(String[] args) { 6 Scanner sc=new Scanner(System.in); 7 int a = sc.nextInt(); 8 int Jc=1; 9 for (int b = a; b >0 ; b--) { 10 Jc*=b; 11 } 12 System.out.println(a+"的阶乘为:"+Jc); 13 } 14 15 }
4. 编写一个程序,输入任意学生成绩,如果输入不合法(<0或者>100),提示输入错误,重新输入,直到输入合法程序结束
public class test { public static void main(String[] args) { Scanner sc=new Scanner(System.in); while (true){ System.out.println("输入学生成绩:"); int num = sc.nextInt(); if (num<0||num>100){ System.out.println("不合法,请重新输入成绩:"); } else{ System.out.println("结束"); break; } } } }
5. 假设某员工今年的年薪是30000元,年薪的年增长率6%。编写一个Java应用程序计算该员工10年后的年薪,并统计未来10年(从今年算起)总收入。
1 public class test { 2 3 public static void main(String[] args) { 4 double nian=30000; 5 double sum=0; 6 for (int n = 0; n <10 ; n++) { 7 nian*=0.06; 8 sum+=nian; 9 } 10 System.out.println("十年后的工资是:"+nian+"总收入为"+sum); 11 } 12 13 }