java基础 逻辑
1, 用for循环打印一个4*5的矩形
public class Test { public static void main(String[] args){ for (int i = 1; i < 5; i++) { for (int j = 1; j < 6; j++) { System.out.print("*");//print不换行; } System.out.println();//println自动换行; } } }
2, 1-100以内前5个能被3整除的整数
public class Test { public static void main(String[] args) { int counter = 0;//counter是计数器 for (int i = 1; i <= 100; i++) { if (i % 3 == 0) { System.out.println(i);//能被3整除打印一次记一次 counter++; } if (counter == 5) { break;//记录5次中断循环 } } } }
3, 101-200以内的质数(质数只能被1和自身整除)
public class Test { public static void main(String[] args) { for (int i = 101; i <= 200; i++) { int t = 0; for (int j = 2; j < i; j++) { if (i % j == 0) { t = 1; break; } } if (t == 0) { System.out.println(i); }
} } }