chapter 02循环结构习题

百知教育学习 - 胡鑫喆 - chapter 02循环结构习题

题目链接

提取码:a53a
1.代码
package chapter_02;
public class Practice_02_01{
 public static void main(String[] args) {
  int sum = 0; 
  for(int i = 1; i < 101; i++) {
   sum += i;
  }
  System.out.println("1+2+3+...+100="+sum);
 }
}
2.代码
package chapter_02;
public class Practice_02_02{
 public static void main(String[] args) {
  int sum = 0;
  for(int i = 1; i < 100; i += 2) {
   sum += i;
  }
  System.out.println("1+3+5+...+99="+sum);
 }
}
3.代码
package chapter_02;
public class Practice_02_03{
 public static void main(String[] args) {
  int sum = 0, i = 1;
  while(i < 101) {
   sum += i;
   i++;
  }
  System.out.println("1+2+3+...+100="+sum);
  sum = 0;
  i = 1;
  do {
   sum += i;
   i +=2;
  }while(i < 100);
  System.out.println("1+3+5+...+99="+sum);
 }
}
4.代码
package chapter_02;
import java.util.*;
public class Practice_02_04{
 public static void main(String[] args) {
  System.out.println("请输入一个小于10的整数:");
  Scanner s = new Scanner(System.in);
  int n = s.nextInt(), factorial = 1;
  for(int i = n; i > 0; i--) {
   factorial *= i;
  }
  System.out.println(n+"的阶乘为:"+factorial);
 }
}
5.代码
package chapter_02;
public class Practice_02_05{
 public static void main(String[] args) {
  int sum = 0;
  for(int i = 3; i < 100; i += 3) {
   if(i%3 == 0 && i%5 != 0) {
    sum += i;
   }
  }
  System.out.println("100以内能被3整除但不能被5整除的数之和为:"+sum);
 }
}
6.代码
package chapter_02;
public class Practice_02_06{
 public static void main(String[] args) {
  int a, b, c;
  for(c = 0; c < 100; c += 3) {
   int lefta = (100-c/3)/3;
   for(a = 0; a <= lefta; a++) {
    b = (100-a*3-c/3)/2;
    if(c+b+a == 100)
     System.out.println(a+"  "+b+"  "+c);
   }
  }
 }
}
7.代码
package chapter_02;
public class Practice_02_07{
 public static void main(String[] args) {
  int men, women, kids;
  for(men = 0; men < 10; men++) {
   int maxwomen = (36-men*4)/3;
   for(women = 0; women <= maxwomen; women++) {
    kids = (36-men*4-women*3)*2;
    if(women+men+kids == 36)
     System.out.println(men+"  "+women+"  "+kids);
   }
  }
 }
}
8.代码
package chapter_02;
public class Practice_02_08{
 public static void main(String[] args) {
  int a, b, c, d;
  for(int i = 1000; i <= 9999; i++) {
    a = i/1000;
    b = i/100%10;
    c = i/10%10;
    d = i%10;
   if((a*b+c*d)*(a*b+c*d) == i)
    System.out.println(i);
  }
 }
}
9.代码
package chapter_02;
import java.util.*;
public class Practice_02_09{
 public static void main(String[] args) {
  System.out.println("请输入一个整数n:");
  Scanner s = new Scanner(System.in);
  int n = s.nextInt();
  for(int i = 1; i <= n; i++) {
   for(int j = 0; j < n-i; j++) {
    System.out.print(" ");
   }
   for(int j = 0; j < 2*i-1; j++) {
    System.out.print("*");
   }
   System.out.println();
  }
 }
}
10.代码
package chapter_02;
public class Practice_02_10{
 public static void main(String[] args) {
  for(int i = 1; i < 10; i++) {
   for(int j = 1; j <= i; j++) {
    System.out.print(j+"*"+i+"="+i*j+"  ");
   }
   System.out.println();
  }
 }
}
11.代码
package chapter_02;
public class Practice_02_11{
 public static void main(String[] args) {
  int a, b, c;
  for(int i = 100; i < 999; i++) {
   a = i/100;
   b = i/10%10;
   c = i%10;
   if(a*a*a+b*b*b+c*c*c == i) {
    System.out.println(i);
   }
  }
 }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值